I'm using the following code to list all the names of sheets in a particular sheet.
function listSheetNames() {
var names = SpreadsheetApp.getActive().getSheets()
.map(function(s) {
return [s.getName()];
})
//Sheet index where list needs to go
SpreadsheetApp.getActive().getSheets()[1]
.getRange(1, 1, names.length, names[0].length).setValues(names)
}
However I want to be able to skip the first few names so they dont get listed. How can this be done.
There is another article which shows this, but the code in that doesn't auto update, or is there a way of combining the two, without using the in cell formula from the one below.
And is there a way of inserting a link to the sheet when it lists.
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 = 3 ; i < sheets.length+1 ; i++ ) out[i-2] = [sheets[i-1].getName()];
return out
}
catch( err ) {
return "#ERROR!"
}
}
UPDATE
So I managed to club the two together as follows
function listSheetNames() {
var ss = SpreadsheetApp.getActiveSpreadsheet().getSheets();
var sheets = SpreadsheetApp.getActive().getSheets()
.map(function(s) {
return [s.getName()];
})
var out = new Array( ss.length+1 ) ;
//out[0] = [ "Name" , "gid" ];
for (var i = 3 ; i < ss.length+1 ; i++ ) out[i-2] = [ss[i-1].getName()];
SpreadsheetApp.getActive().getSheets()[1]
.getRange(1, 1, sheets.length, sheets[0].length).setValues([out])
}
Now I get an error, If I make .setValues([out])
incorrect range height, was 1 but should be 22 (line 46)
If I leave is as .setValues(out)
Then I get the error
Cannot convert Array to Object[][]. (line 46)