Delete Menu Items if sheets are deleted in Google Spreadsheet using Apps Script

Advertisements

My onOpen() has a script that clears content inside dynamic sheets.

Each submenu name has the same name as the Sheet.

function onOpen() {
 
  var ui = SpreadsheetApp.getUi();

  ui.createMenu('Reset')
    .addItem("Meta","resetmetaFunction")
    .addItem("Snapchat","resetsnapFunction")  
    .addItem("TikTok","resettiktokFunction")
    .addItem("Twitter","resettwitterFunction")
    .addItem("Youtube","resetyoutubeFunction")  
    .addItem("Google","resetgoogleFunction")
    .addItem("LinkedIn","resetlinkedinFunction")  
    .addItem("Programmatic","resetprogrammaticFunction")        
    .addItem("Clear All","resetAll")   
    .addToUi();
}

I want those submenus to disappear if a specific sheet is removed.

For example, if the sheet "Snapchat" is removed, the submenu "Snapchat" should also be removed.

How can I execute this in Apps Script ?

Thank you.

>Solution :

In your situation, how about the following modification?

Modified script:

Please copy and paste the following script to the script editor of Spreadsheet, and save the script.

// If you want to dynamically change the custom menu when the sheet is deleted, please install OnChange trigger to `onChange` function.
function onChange(e) {
  if (e.changeType == "REMOVE_GRID") { // or if (["REMOVE_GRID", "INSERT_GRID"].includes(e.changeType)) {
    onOpen(e);
  }
}

function onOpen(e) {
  // This is from your script.
  var obj = [
    { sheetName: "Meta", func: "resetmetaFunction" },
    { sheetName: "Snapchat", func: "resetsnapFunction" },
    { sheetName: "TikTok", func: "resettiktokFunction" },
    { sheetName: "Twitter", func: "resettwitterFunction" },
    { sheetName: "Youtube", func: "resetyoutubeFunction" },
    { sheetName: "Google", func: "resetgoogleFunction" },
    { sheetName: "LinkedIn", func: "resetlinkedinFunction" },
    { sheetName: "Programmatic", func: "resetprogrammaticFunction" },
    { sheetName: "Clear All", func: "resetAll" },
  ];
  var { source } = e;
  var source = SpreadsheetApp.getActiveSpreadsheet();
  var sheetObj = Object.fromEntries(source.getSheets().map(s => [s.getSheetName(), true]));
  var menu = SpreadsheetApp.getUi().createMenu('Reset');
  obj.forEach(({ sheetName, func }) => {
    if (sheetObj[sheetName]) {
      menu.addItem(sheetName, func);
    }
  });
  menu.addToUi();
}
  • In this script, when the Spreadsheet is opened, the custom menu is created using the current sheets.
  • If you want to dynamically change the custom menu when the sheet is deleted, please install OnChange trigger to onChange function. By this, when you delete a sheet, onChange function is run, and onOpen is run, and the custom menu is updated using the current sheets.
  • If you want to dynamically run the script when a sheet is deleted and inserted, please modify (e.changeType == "REMOVE_GRID") to (["REMOVE_GRID", "INSERT_GRID"].includes(e.changeType)).

References:

Leave a ReplyCancel reply