Im new to Apps Script and need some assistance with the code required to get a list of all sheet names (current and those to be created) that will start with "Week of" in the title. Ideally this would update as new sheets are created (either on edit or on open)
The code below is what I have so far, but need it to filter the sheets and only show those starting with "week of" as well as have it update when new sheets are added
/**
* Returns the names of all sheets.
*
* @return The name of all sheets in the Google Sheets spreadsheet.
* @customfunction
*/
function ALLSHEETNAMES() {
let ss = SpreadsheetApp.getActive();
let sheets = ss.getSheets();
let sheetNames = [];
sheets.forEach(function (sheet) {
sheetNames.push(sheet.getName());
});
return sheetNames;
}
If found some code (see below) that will not count the first x number of sheets, but i’m not certain that this will remain the case as the document grows so would rather find something that will use the "week of" filter vs a set number of sheets to ignore.
function sheetNameArray() {
var out = new Array()
var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
for (var i=6; i<sheets.length ; i++) out.push( [ sheets[i].getName() ] )
return out
}
3
Answers
Get the Value of ‘Week Of’
Your Code is almost there, You just add a condition where you use
toLowerCase()
andincludes()
to get the desired output.Script used:
Reference:
Get the value of "Week of" | Not Case sensitive
I have different approach getting the value of
Week of
.It’s
not case sensitive
whether you wanted to get the value of "Week Of", "WEEk OF" ,"week of" or etcI used
regex
here’s the code:
Reference:
To get the sheetNames starting with "Week of":