List names of sheets in Google Sheets and skip the first two

牧云@^-^@ 提交于 2019-11-30 02:41:51

问题


I found code to list the names of all the sheets in Google Sheets (from here):

function SheetNames() { // Usage as custom function: =SheetNames( GoogleClock() )
try {
  var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets()
  var out = new Array( sheets.length+1 ) ;
  //out[0] = [ "Name" , "gid" ];
  for (var i = 1 ; i < sheets.length+1 ; i++ ) out[i] = [sheets[i-1].getName()];
  return out
}
catch( err ) {
  return "#ERROR!" 
}
}

My question is how can I modify the script to skip the first two sheet names and begin populating the list in the cell where the script is called?

I tried changing the var i = 1 to var i = 3 and that did skip the first two sheet names but it also created to blank cells. How do I skip the first two sheet names and not create any blank cells?


回答1:


You had the right idea, but your output array was placing the first item in position 3. Instead, do this:

for (var i = 3 ; i < sheets.length+1 ; i++ ) out[i-2] = [sheets[i-1].getName()];


来源:https://stackoverflow.com/questions/30219679/list-names-of-sheets-in-google-sheets-and-skip-the-first-two

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!