Search code examples
google-apps-scriptgoogle-sheetsmenu

How to programmatically *remove* a Google Sheets menu item in Apps Script


Apps Script folks, how do I programmatically remove a custom menu item in Google Sheets from Apps Script. There are enough ways to add items but I see nothing for removing. Seems like this is a major API omission? Does anyone know how to do this?


Solution

  • There are no methods for removing menu items.

    An alternative approach is building a new menu and using it to replace the existing menu. Note, the replacement menu must have the same name as the existing menu, otherwise an additional menu will be added.

    The following example will initially create a menu with 1 item, wait 5 seconds, then replace it with another menu with one more item and then repeat until there's a menu with 5 items.

    function onOpen() {
      for (var i = 1; i <= 5; i++) {
        addMenu(i);
        Utilities.sleep(5000);
      }
    }
    
    function addMenu(numItems) {
      var menu = SpreadsheetApp.getUi().createMenu('Test Menu');
      for (var i = 0; i < numItems; i++) {
        menu.addItem('Entry ' + i, 'nullFunc');
      }
      menu.addToUi();
    }
    
    function nullFunc() {}