多个onEdit脚本,但第2行出现错误

时间:2018-09-07 12:24:59

标签: google-apps-script google-sheets

我不知道自己在做什么错。我在这方面还很陌生,所以有很多出错的地方。我正在尝试运行同一脚本的两个版本。选中一个框时,如果它在F列中,我希望运行功能7R,但是如果选中该框在K列中,则我希望运行功能8R。我使用了具有类似问题的线程来尝试合并两个脚本,但在第2行中始终出现错误。

我遇到的错误是Missing;声明前:第2行

这是我所拥有的:

function onEdit(e) {
8R();
7R();
}

function 8R() {
//Get the sheet you want to work with. 
var editrange = {
  top : 2,
  bottom : 260,
  left : 11,
  right : 11};
//getRow() and not getrow()
var thisrow = e.range.getRow();
if (thisrow < editrange.top || thisrow > editrange.bottom) return;
//getColumn() and not getcolumn()
var thiscolumn = e.range.getColumn();
if (thiscolumn < editrange.left || thiscolumn > editrange.right) return;
//Line that replaces the erroneous 'var ss = e.range.getSheet()';
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Responsible");
//Grab the entire Range, and grab whatever values you need from it. EX: 
rangevalues
var range8 = sheet.getRange("K3:K90");
var range28 = sheet.getRange("M3:M90");
var range2values8 = range28.getValues();
var rangevalues8 = range8.getValues();
//Loops through range results
for (var i in rangevalues8) {
  Logger.log("rangevalues8["+i+"]["+0+"] is:"+rangevalues8[i][0]);//Added
  //Set the rules logic
  if (rangevalues8[i][0] == true) { //Modified
  //Set the cell 
  range2values8[i][0] += 1; //Directly add 1 to range2values
  Logger.log(range2values8);//Added
  }
}
//copy new information
var destination = ss.getSheetByName('Compiled Data');//whatever page
var destCell8 = destination.getRange("I3:I90");
destCell8.setValues(range2values8);
//clear checkboxes
var cleaning = ss.getSheetByName('Asset Bank');
var cleaningcell8 = cleaning.getRange("A3:A90").getValues();
range8.setValues(cleaningcell8);
}

function 7R() {
//Get the sheet you want to work with. 
var editrange = {
  top : 2,
  bottom : 260,
  left : 6,
  right : 6};
//getRow() and not getrow()
var thisrow7 = e.range.getRow();
if (thisrow7 < editrange.top || thisrow7 > editrange.bottom) return;
//getColumn() and not getcolumn()
var thiscolumn7 = e.range.getColumn();
if (thiscolumn7 < editrange.left || thiscolumn7 > editrange.right) return;
//Line that replaces the erroneous 'var ss = e.range.getSheet()';
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Responsible");
//Grab the entire Range, and grab whatever values you need from it. EX: 
rangevalues
var range7 = sheet.getRange("F3:F90");
var range27 = sheet.getRange("H3:H90");
var range2values7 = range27.getValues();
var rangevalues7 = range7.getValues();
//Loops through range results
for (var i in rangevalues7) {
  Logger.log("rangevalues7["+i+"]["+0+"] is:"+rangevalues7[i][0]);//Added
  //Set the rules logic
  if (rangevalues7[i][0] == true) { //Modified
    //Set the cell 
    range2values7[i][0] += 1; //Directly add 1 to range2values
    Logger.log(range2values7);//Added
  }
}
//copy new information
var destination = ss.getSheetByName('Compiled Data');//whatever page
var destCell7 = destination.getRange("I93:I180");
destCell7.setValues(range2values7);
//clear checkboxes
var cleaning = ss.getSheetByName('Asset Bank');
var cleaningcell7 = cleaning.getRange("A3:A90").getValues();
range7.setValues(cleaningcell7);
}

这里是我正在处理的副本表的链接,其中包含所有相关信息:https://docs.google.com/spreadsheets/d/1PWaWm7AryljOMd5Aq1O2RSADyRFNVZbUDqKO__SzW2w/edit?usp=sharing

2 个答案:

答案 0 :(得分:1)

JavaScript变量和函数名称不能以数字开头。将8R7R替换为valid identifier,例如func8Rfunc7R

除此之外,您的8R7R函数尝试立即访问一个e.range变量,但这在任何地方都没有定义。我假设您正在尝试读取onEdit事件参数,但是您必须将其传递给函数,并定义参数,例如

function onEdit(e) {
  f8R(e);
  f7R(e);
}

function f8R(e) {
  //... continue (remember to also declare on f7R)

如果要手动测试此功能,则必须填写此e参数,就像在电子表格上实际更改某些内容时环境所做的那样。我喜欢通过编写另一个“调用方”函数来做到这一点,就像这样:

function testOnEdit() {
  onEdit({range: SpreadsheetApp.getActiveSheet().getRange('F2')});
}

答案 1 :(得分:0)

最终能够使脚本正常工作。首先,我按照Henrique的建议更改了名称。然后整理出onEdit块。感谢大家的帮助和建议。没有你们,我仍然会对我的机器生气。

function onEdit(e) {
  //don't need entire range, just the column that was modified to figure out which function to call
  var editColumn = e.range.getColumn();

  //confirm edit was a box being checked before running the code (as this seems to run on ANY sheet edit)
  if (e.oldValue === "false" && e.value === "TRUE") {
    if (editColumn === 6)
      func7R(e);
    else if (editColumn === 11)
      func8R(e);
  }
}

function func8R(e) {
  //Get the sheet you want to work with. 
  var editRange = {
    top : 2,
    bottom : 260,
    left : 11,
    right : 11};
  //getRow() and not getrow()
  var thisRow = e.range.getRow();
  if (thisRow < editRange.top || thisRow > editRange.bottom) return;
  //getColumn() and not getcolumn()
  var thisColumn = e.range.getColumn();
  if (thisColumn < editRange.left || thisColumn > editRange.right) return;
  //Line that replaces the erroneous 'var ss = e.range.getSheet()';
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("Responsible");
  //Grab the entire Range, and grab whatever values you need from it. EX: rangevalues
  var range8 = sheet.getRange("K3:K90");
  var range28 = sheet.getRange("M3:M90");
  var range2values8 = range28.getValues();
  var rangevalues8 = range8.getValues();
  //Loops through range results
  for (var i in rangevalues8) {
    Logger.log("rangevalues8["+i+"]["+0+"] is:"+rangevalues8[i][0]);//Added
    //Set the rules logic
    if (rangevalues8[i][0] == true) { //Modified
      //Set the cell 
      range2values8[i][0] += 1; //Directly add 1 to range2values
      Logger.log(range2values8);//Added
    }
  }
  //copy new information
  var destination = ss.getSheetByName('Compiled Data');//whatever page
  var destCell8 = destination.getRange("I3:I90");
  destCell8.setValues(range2values8);
  //clear checkboxes
  var cleaning = ss.getSheetByName('Asset Bank');
  var cleaningcell8 = cleaning.getRange("A3:A90").getValues();
  range8.setValues(cleaningcell8);
}

function func7R(e) {
  //Get the sheet you want to work with. 
  var editRange = {
    top : 2,
    bottom : 260,
    left : 6,
    right : 6};
  //getRow() and not getrow()
  var thisrow7 = e.range.getRow();
  if (thisrow7 < editRange.top || thisrow7 > editRange.bottom) return;
  //getColumn() and not getcolumn()
  var thiscolumn7 = e.range.getColumn();
  if (thiscolumn7 < editRange.left || thiscolumn7 > editRange.right) return;
  //Line that replaces the erroneous 'var ss = e.range.getSheet()';
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("Responsible");
  //Grab the entire Range, and grab whatever values you need from it. EX: rangevalues
  var range7 = sheet.getRange("F3:F90");
  var range27 = sheet.getRange("H3:H90");
  var range2values7 = range27.getValues();
  var rangevalues7 = range7.getValues();
  //Loops through range results
  for (var i in rangevalues7) {
    Logger.log("rangevalues7["+i+"]["+0+"] is:"+rangevalues7[i][0]);//Added
    //Set the rules logic
    if (rangevalues7[i][0] == true) { //Modified
      //Set the cell 
      range2values7[i][0] += 1; //Directly add 1 to range2values
      Logger.log(range2values7);//Added
    }
  }
  //copy new information
  var destination = ss.getSheetByName('Compiled Data');//whatever page
  var destCell7 = destination.getRange("I93:I180");
  destCell7.setValues(range2values7);
  //clear checkboxes
  var cleaning = ss.getSheetByName('Asset Bank');
  var cleaningcell7 = cleaning.getRange("A3:A90").getValues();
  range7.setValues(cleaningcell7);
}