List the sheet names in Google Sheets and skip the first two

I found code for listing the names of all 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 to change the script to skip the first two sheet names and start filling out the list in the cell where the script is being called?

I tried changing var i = 1 to var i = 3 and skipped the first two sheet names, but also created empty cells. How to skip the first two sheet names and not create empty cells?

+8
google-spreadsheet google-apps-script google-spreadsheet-api
source share
1 answer

You had the right idea, but your output array put the first element at position 3. Instead, do the following:

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

All Articles