Google Sheets – Insert Sheet Names into Cells

See the linked video to learn how to insert the sheet names into cells in Google Sheets. This page is the for the text that accompanies the YouTube video.

The code for the regular expression is =IF(TODAY()=TODAY(), REGEXREPLACE(CELL("address",'Detail 1'!A1),"'?([^']+)'?!.*","$1"), ""). The text in the box below is meant to be copy and pasted for the custom functions that were used in this sheet. The text in this Doc is the same, grab it from here if it’s easier.

//Return the current sheet name.
function SheetName() {
return SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getName();
}

//Return all current sheet names.
function SheetNames() {
var out = new Array()
var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
for (var i=0 ; i<sheets.length ; i++) out.push( [ sheets[i].getName() ] )
return out 
}

//Return specified sheet name
function SheetNumber(idx) {
if (!idx)
return SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getName();
else {
var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
var idx = parseInt(idx);
if (isNaN(idx) || idx < 1 || sheets.length < idx)
throw “Invalid parameter (it should be a number from 0 to “+sheets.length+”)”;
return sheets[idx-1].getName();
}
}