2

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();

2 Answers 2

1

One way is to list menu items and their function names like this:

function onOpen() {
  SpreadsheetApp.getActive()
    .addMenu(
      '⚙️Team Admin',
      [
        {
          name: 'Add Agents',
          functionName: 'newSheet',
        },
        {
          name: 'Reset Agents',
          functionName: 'deleteRedundantSheets',
        },
      ]
    );
}
2

managed to sort it by editing as per below:

function onOpen() {
  var ui = SpreadsheetApp.getUi();
  // Or DocumentApp or FormApp.
  ui.createMenu('⚙️ Team Admin ')
      .addItem('Add team members', 'menuItem1')
      .addItem('Reset tracker', 'menuItem2')
      .addToUi();
}

function menuItem1() {

    var ss = SpreadsheetApp.getActiveSpreadsheet();
         var templateSheet = ss.getSheetByName('Template');
    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());
}

function menuItem2() {

  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();
}}}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.