Javascript 在google apps脚本中错误声明当前单元格变量

Javascript 在google apps脚本中错误声明当前单元格变量,javascript,google-apps-script,google-sheets,Javascript,Google Apps Script,Google Sheets,我对在谷歌表单中编写公式非常有经验,可以自动从谷歌表单中输入数据 我现在通过学习应用程序脚本,试图以此为基础。我正试图写一个脚本,将 打开图纸时开始 在后台连续运行,检查当前单元格是否为A1。(我试图通过使用while循环来实现这一点,因为条件始终为true。) 删除F1的内容 目前,无论哪个单元格处于活动状态,代码都在运行。通过调试,我认为这是因为if语句编写不正确,并且变量CurrentCell声明不正确 这是我目前拥有的代码: function DeleteCell() { va

我对在谷歌表单中编写公式非常有经验,可以自动从谷歌表单中输入数据

我现在通过学习应用程序脚本,试图以此为基础。我正试图写一个脚本,将

  • 打开图纸时开始
  • 在后台连续运行,检查当前单元格是否为A1。(我试图通过使用while循环来实现这一点,因为条件始终为true。)
  • 删除F1的内容
  • 目前,无论哪个单元格处于活动状态,代码都在运行。通过调试,我认为这是因为if语句编写不正确,并且变量CurrentCell声明不正确

    这是我目前拥有的代码:

        function DeleteCell() {
     var ss = SpreadsheetApp.getActiveSpreadsheet();
      var sheet = ss.getActiveSheet();
      var num1 = 1
    
    
      // run the following loop
      do {
    
    
        // add 1 to num1
        num1++;
    
    var CurrentCell = sheet.getCurrentCell();    
        // if the current cell is A1
        if(CurrentCell==1,1)
           {
    
      sheet.getCurrentCell().offset(0, 5, 1, 1).activate();
      sheet.getActiveRangeList().clear({contentsOnly: true, skipFilteredRows: true});
      var CurrentCell = sheet.getCurrentCell()
           }
    
      }while (num1 > 5); // continue running the loop while num1 is greater than five
    
    
    };
    

    也许这就是我的意思:

    function clearRange() {
      var ss=SpreadsheetApp.getActive();
      var sheet=ss.getActiveSheet();
      var num1=1;
      do {
        num1++;
        var CurrentCell=sheet.getCurrentCell();//Current cell is a range    
        if(CurrentCell.getA1Notation()=='A1') {
          sheet.getCurrentCell().offset(0, 5, 1, 1).clear({contentsOnly: true, skipFilteredRows: true});
        }
      }while(num1<5);
    }
    
    函数clearRange(){
    var ss=SpreadsheetApp.getActive();
    var sheet=ss.getActiveSheet();
    var num1=1;
    做{
    num1++;
    var CurrentCell=sheet.getCurrentCell();//当前单元格是一个范围
    if(CurrentCell.geta1表示法()=='A1'){
    sheet.getCurrentCell().offset(0,5,1,1).clear({contentsOnly:true,skipFilteredRows:true});
    }
    
    }当(Num1谢谢。我将很快将代码移到另一个位置并进行测试。该链接也非常有用,我现在意识到我所尝试的不会起作用。我希望A1单元格中有一个删除按钮,这样当Sheets检测到它被点击时,它就会运行该代码。我知道你可以将代码链接到图片真的要做一个按钮,但我相信一旦图片被分配到一个单元格,那么就不可能再为图片分配代码了。