2

What is the difference between these two ways of getting the same thing (a new menu item)?

Version 1

 function onOpen(e) {
   SpreadsheetApp.getUi()
       .createMenu('My Menu')
       .addItem('My Menu Item', 'myFunction')
       .addSeparator()
       .addSubMenu(SpreadsheetApp.getUi().createMenu('My Submenu')
           .addItem('One Submenu Item', 'mySecondFunction')
           .addItem('Another Submenu Item', 'myThirdFunction'))
       .addToUi();
 }

Version 2

function onOpen() {
  var menu = [{name: "My Menu Item", functionName: "myFunction"}]
  SpreadsheetApp.getActiveSpreadsheet().addMenu("My Menu", menu);
}
2

addMenu

This is a method of Spreadsheet class, i.e., it only applies to spreadsheets. All it does is add a new menu item with second-level submenus. On the upside, the syntax is very simple.

getUi

This method is currently available for three apps: SpreadsheetApp, DocumentApp, and FormApp. It returns an instance of user interface which can be used for more than just adding a menu item. One can show alerts, prompts, dialogs, and even add an interactive sidebar with custom content. Also, the menu creating options are richer: they allow separators and nested submenus. There are special add-on menus, too. Accordingly, the syntax is more verbose.

Conclusion

Neither of two methods is deprecated; use whichever fits your goal better.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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