Custom Functions – Get Sheet Names

How to write a custom function and return the sheet names.

Sheet (to copy from the File menu):
https://docs.google.com/spreadsheets/d/19ismgyyfalmi3WedEH1fhs-_Zt8jP4v3QD9HRDUIDdo/edit

Scripts:
/**
* Returns the name of the current tab.
* @return Current tab name.
* @customfunction
*/

function sheetname(){
return SpreadsheetApp.getActive().getActiveSheet().getName();
}

/**
* Returns the names of all tabs in the spreadsheet.
* @return A vertical array of all tab names.
* @customfunction
*/

function sheetnames(){
const sheets = SpreadsheetApp.getActive().getSheets();
let out = [];
for (let i in sheets){
out.push([sheets[i].getName()]);
}
return out;
}

/**
* Returns the names of all tabs in the spreadsheet.
*
* @param {boolean} includes_current Whether to include the current tab in the output. TRUE by default.
* @return A vertical array of all tab names.
* @customfunction
*/

function sheetnames(includes_current){
const sheets = SpreadsheetApp.getActive().getSheets();
let out = [];

if (includes_current == false){
for (let i in sheets){
if (sheets[i].getName() == SpreadsheetApp.getActive().getActiveSheet().getName()) continue;
out.push([sheets[i].getName()]);
}
} else {
for (let i in sheets){
out.push([sheets[i].getName()]);
}
}
return out;
}

Connect with me:
• spencer.farris@gmail.com
• spencerfarris.me
• www.linkedin.com/in/spencer-farris/
• Twitter @FarrisSpencer
• Google Product Expert support.google.com/docs/profile/12305

Leave a Reply

Your email address will not be published. Required fields are marked *