Google表格脚本 - > .setActiveSheet()不起作用

时间:2016-09-01 05:10:54

标签: google-sheets

我有以下脚本:

function UpdateAllSheets() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheets = ss.getSheets();
  var sheetslength = sheets.length;
  for (var n=0; n < sheetslength; n++) {
  SpreadsheetApp.setActiveSheet(ss.getSheets()[n]);
  TotalDayUpdate();
  }
}

上面的脚本应该遍历电子表格的所有工作表,并为每个工作表运行TotalDayUpdate()函数。

但是它给出了一个错误:

指定的工作表必须是电子表格的一部分。 (换行:SpreadsheetApp.setActiveSheet(ss.getSheets()[n]);)

我有一个较旧的电子表格,相同的功能可以正常工作。

1 个答案:

答案 0 :(得分:1)

maybe not a solution, but should be more clear:

function UpdateAllSheets() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheets = ss.getSheets();
  for (var n=0; n < sheets.length; n++) {
    TotalDayUpdate(sheets[n]); // directly give the sheet as argument to the function
  }
}

so in your TotalDayUpdate function as you have the parsed sheet in parameter you could use it. For me it's better because you don't call SpreadsheetApp... again what is time consuming