我有一个包含大约50到60张的Google电子表格。我正在尝试获取所有这些工作表的名称,并将它们放入另一个电子表格中。
到目前为止,我已经编写了以下代码
function myfunction(){
var ss = SpreadsheetApp.openById("abck12345");
var sheetCount = ss.getSheets().length;
var i=0;
var sheetlist;
while(i<sheetCount){
sheetlist = ss.getSheets()[i];
Logger.log(sheetlist);
}
}
执行此操作时,我收到错误&#34;超过最长执行时间&#34;
答案 0 :(得分:3)
一次获得所有表单效率更高。您的代码在每个循环中获取所有表单。此代码获取所有工作表名称并将其放入新的电子表格中:
function myfunction() {
var allSheets,i,sheetCount,sheetlist,sheetName,ss,
targetSh,targetSS,thisSheet;
ss = SpreadsheetApp.openById("abck12345");
targetSS = SpreadsheetApp.openById("999999");
allSheets = ss.getSheets();
sheetCount = allSheets.length;
sheetlist = [];
for (i=0;i<sheetCount;i++) {
thisSheet = allSheets[i];
sheetName = thisSheet.getName();
sheetlist.push([sheetName]);//Put an inner array of each sheet tab name into
//an outer array named sheetlist
//Creates a 2D array for setting the values later
}
Logger.log(sheetlist);
targetSh = targetSS.getSheetByName('target sheet tab name');
targetSh.getRange(targetSh.getLastRow()+1, 1, sheetlist.length, sheetlist[0].length).setValues(sheetlist);
}
答案 1 :(得分:1)
你没有增加我。更好的方法是:
function myfunction(){
var ss = SpreadsheetApp.openById("1cmUWU_nbE6idJUmpNtKnhq0KUekVXnW9OYVZr93TSNs");
var sheets = ss.getSheets();
var i;
for(i in sheets){
Logger.log(sheets[i].getName())
}
}