I'm currently using the below code to create a custom menu called 'Team Admin' in Google sheets which allows a user to activate the function 'newSheet' when the menu item 'Add Agents' is clicked.
function onOpen() {
var menu = [{
name : "Add Agents",
functionName : "newSheet"
}
];
SpreadsheetApp.getActiveSpreadsheet().addMenu("⚙️Team Admin", menu);
}
function newSheet() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var templateSheet = ss.getActiveSheet();
var sheet1 = ss.getSheetByName("SheetNames")
var getNames = sheet1.getRange("A1:A30").getValues().filter(String).toString().split(",");
for (var i = 0; i < getNames.length; i++) {
var copy = ss.getSheetByName(getNames[i]);
if (copy) {
Logger.log("Sheet already exists");
} else {
templateSheet.copyTo(ss).setName(getNames[i]);
ss.setActiveSheet(ss.getSheetByName(getNames[i]));
ss.moveActiveSheet(ss.getNumSheets());
}
}
}
I'm trying to add a second menu item called 'Reset Agents' within the same menu which will allow the below function to run but can't figure out how to edit it, can someone help please?
function deleteRedundantSheets() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheets = ss.getSheets();
for (i = 0; i < sheets.length; i++) {
switch(sheets[i].getSheetName()) {
case "Admin":
case "Template":
case "Stats":
case "Notes":
case "SheetNames":
break;
default:
ss.deleteSheet(sheets[i]);
}
}
}
var sheet = SpreadsheetApp.getActive().getSheetByName('Admin'); sheet.getRange('H24:AL39').clearContent(); sheet.clearNotes();
var sheet = SpreadsheetApp.getActive().getSheetByName('Admin'); sheet.getRange('H67:AL83').clearContent(); sheet.clearNotes();
var sheet = SpreadsheetApp.getActive().getSheetByName('Admin'); sheet.getRange('H88:L104').clearContent(); sheet.clearNotes();