Skip to content

Instantly share code, notes, and snippets.

@pfelipm
Last active November 22, 2021 20:08
Show Gist options
  • Save pfelipm/829ed62ab0c03b2beb0cfc2934f30b1e to your computer and use it in GitHub Desktop.
Save pfelipm/829ed62ab0c03b2beb0cfc2934f30b1e to your computer and use it in GitHub Desktop.
/**
* Builds a TOC in the spreadsheet.
*
* Pablo Felip (@pfelipm).
*
* @param {true} includeId Include sheet ID
* @param {true} includeUrl Include link to sheet
* @return List of [Sheet name, Sheet ID, Sheet URL]
* @customfunction
*/
function TOC(includeId, includeUrl) {
const ss = SpreadsheetApp.getActive();
const baseUrl = ss.getUrl();
const toc = [];
ss.getSheets().forEach(sheet => {
const rowResult = [];
rowResult.push(sheet.getName());
if (includeId) rowResult.push(sheet.getSheetId());
if (includeUrl) rowResult.push(baseUrl + '#gid=' + sheet.getSheetId());
toc.push(rowResult);
});
return toc;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment