0

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?

0

2 Answers 2

6

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() {}
Sign up to request clarification or add additional context in comments.

Comments

0

Check this out, latest from the Google Workspace Doc:

// The onOpen function is executed automatically every time a Spreadsheet is loaded
function onOpen() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  ss.addMenu("badMenu", [{name: "remove bad menu", functionName: "removeBadMenu"},
                                       {name: "foo", functionName: "foo"}]);
}
function removeBadMenu() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  ss.removeMenu("badMenu");  // name must match the name used when added the menu
}
function foo(){
// Do nothing
}

But it says:

Removes a menu that was added by addMenu(name, subMenus). The name argument should have the same value as the corresponding call to addMenu(name, subMenus).

Not sure if you use the SpreadsheetApp.getUi().createMenu() method to create.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.