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

16,881

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()];
Share:
16,881

Related videos on Youtube

Nathan
Author by

Nathan

Most of the programming I do is related to Khmer, the language of Cambodia. I am not much of a programmer, but I try my best, and have found this site and the people here invaluable.

Updated on September 13, 2022

Comments

  • Nathan
    Nathan over 1 year

    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?

  • Nathan
    Nathan almost 9 years
    Perfect, thanks! I would have replied on the the original thread but it was closed - nice to get an answer from you!
  • Zig Mandel
    Zig Mandel almost 9 years
    note that it will die if the spreadsheet has less than 3 sheets.