谷歌工作表脚本给我"对象不允许添加或更改属性"错误。我怀疑是与getRange相关的问题

时间:2017-05-10 22:12:04

标签: google-apps-script google-sheets google-spreadsheet-api

我试图创建一个将在Edit上触发的自定义触发器功能。我的目标是跟踪修改或添加某个工作表范围的时间,并通过在某个单元格中添加一个字符串来捕获另一个工作表中的那个。 该更新标记必须位于第I列,与引用已修改单元格ID的行相交。

现在,当我尝试运行它时,它给了我这个错误,但它没有进一步的解释,所以我不知道如何继续。 你能帮我吗? 这是我的代码:



function onEdit() {
  var ss = SpreadsheetApp.getActive();
  var e = ss.getActiveSheet()
  var ecell= e.getActiveCell()
  var allsheets = ss.getSheets();
  var version = "0.4.0";
  //Making sure that whatever was updated it is not in the first 5 columns nor in the first 2 rows
  if (e.columnStart < 5 || e.rowStart < 2 || e.getSheetName() =="0.4.0") return;
  //Trying to get hlook pointing to the F2 cell
  var hlook= e.getRange('F2');
  // Want to set up a formula in F2 so that I can get de location of the index (manual ID) value of ecell
  hlook.setFormula="HLOOKUP(ecell, 'A'+ ROW(ecell) + ':' + 'Q' + ROW(ecell),3)";
  // Want to retrieve the value returned from the formula
  var eid=hlook.getActiveCell().getValue();
  //Navigting all available sheets to set up updates
  for (var s in allsheets){
    var sheet=allsheets[s];
    // Dismissing the master sheet
    if (sheet.getSheetName()!="0.4.0")
   {
     //Assigning a variable to AA1 (or even further, whith is fine)
     var con=e.getRange(1,28);
     //Setting up a formula
     con.setFormula="IFERROR(VLOOKUP(eid, 'C1:C35',1), false)";
     // Retrieving formula result
     var control=con.getActiveCell().getValue();
     if (control!=false)
     {
       //If the value I am looking for actually existws on this sheet, enter update stamp at column I on the row where the value was found
       var ro=VLOOKUP(eid, "C1:C350",1);
       var cel="I" + ro;
       cel.setValue(version)
      }
    }
  }
}
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

所以我相信你的问题出在你的.setFormula中。当你输入.setFormula =&#34; ... formula ...&#34 ;;该脚本认为您要编辑&#39; setFormula&#39;宾语。实际上你想做动作setFormula,所以你应该像这样格式化:

hlook.setFormula("...formula...");

你想要看的另一件事是你的公式。您会发现,只要在工作表上,Google工作表就不知道ecell是什么。可以使用以下方法在ecell变量中获取ecell的A1表示法:

var ecell = e.getActiveCell().getA1Notation();

然后您可以将其替换为您的公式:

hlook.setFormula("HLOOKUP(" + ecell + ", 'A'+ ROW(" + ecell + ") + ':' + 'Q' + ROW(" + ecell + "),3)");

我刚刚评论了以这种方式修改代码的位置,以便对其进行更正:

function onEdit() {
  var ss = SpreadsheetApp.getActive();
  var e = ss.getActiveSheet();
  var ecell= e.getActiveCell().getA1Notation();
  var allsheets = ss.getSheets();
  var version = "0.4.0";
  //Making sure that whatever was updated it is not in the first 5 columns nor in the first 2 rows

MODIFY HERE - 如果语句需要{},请格式化if(statement){action}如此大括号{return;}

  if (e.columnStart < 5 || e.rowStart < 2 || e.getSheetName() =="0.4.0") return; //why not use version variable here?

  //Trying to get hlook pointing to the F2 cell
  var hlook= e.getRange('F2');
  // Want to set up a formula in F2 so that I can get de location of the index (manual ID) value of ecell

MODIFY SETFORMULA();

   hlook.setFormula="HLOOKUP(ecell, 'A'+ ROW(ecell) + ':' + 'Q' + ROW(ecell),3)";
  // Want to retrieve the value returned from the formula

MODIFY SCRIPT - 你想要hlook的值,所以

var eid = hlook.getValue();
*******************************
  var eid=hlook.getActiveCell().getValue();
  //Navigting all available sheets to set up updates
  for (var s in allsheets){
    var sheet=allsheets[s];
    // Dismissing the master sheet
    if (sheet.getSheetName()!="0.4.0")
   {
     //Assigning a variable to AA1 (or even further, whith is fine)
     var con=e.getRange(1,28);
     //Setting up a formula

MODIFY SETFORMULA();

     con.setFormula="IFERROR(VLOOKUP(eid, 'C1:C35',1), false)";
     // Retrieving formula result
     var control=con.getActiveCell().getValue();
     if (control!=false)
     {
       //If the value I am looking for actually existws on this sheet, enter update stamp at column I on the row where the value was found

修改这里 -

var ro = "VLOOKUP(" + eid + ", "C1:C350",1)";
*******************************

       var ro=VLOOKUP(eid, "C1:C350",1);
       var cel="I" + ro;
       cel.setValue(version)
      }
    }
  }
}

希望通过这些修改可行,但如果您收到错误请告诉我,我会再看看:)祝你好运