I have written a script in Google Sheets to return the name of a given sheet (see below).
The function is written into a summary sheet, where the input given as the index of the sheet required. I have all the functions pre-written e.g.
A1 = GetSheet(1) B1 = GetSheet(2) C1 = GetSheet(3) ...etc
Obviously, when the sheet with the index in question does not yet exist, I get an error. However, when I do create that sheet, the cell does not automatically update with the name of the new sheet, it still returns an error. To get the name to appear, I have to delete the function and re-paste it in.
Is there a way for the cells to automatically update when a new sheet is added, without re-typing the function?
Apologies if I am missing something obvious, I am very new to this!
function GetSheet(input)
{
var spread = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = SpreadsheetApp.getActiveSheet();
var sourceIndex = sourceSheet.getIndex();
var nextIndex = sourceIndex + 1;
var ss = SpreadsheetApp.getActiveSpreadsheet();
SpreadsheetApp.setActiveSheet(ss.getSheets()[input]);
var active = ss.getActiveSheet();
var name = active.getName();
return name
}