Custom menus for Editor Add-ons

Published Editor Add-ons can create custom menu items under their editor's Extensions menu. You can insert an add-on menu by using the Ui.createAddonMenu() method and add items to it using the Menu.addItem() method. Menus are usually created in the add-on's onOpen(e) method.

You can create dynamic menus that change based on user interactions or add-on state. However, add-ons must create an initial menu before the add-on is authorized by the user. Because of this, you must check the add-on's authorization mode prior to constructing menus in onOpen(e). Do not attempt to take any action that requires authorization (such as checking the script Properties) while the add-on is in ScriptApp.AuthMode.NONE. See the authorization lifecycle for more details on the authorization modes and lifecyle.

The following example shows how to build a dynamic add-on menu for different authorization modes:

function onOpen(e) {
  var menu = SpreadsheetApp.getUi().createAddonMenu(); // Or DocumentApp or SlidesApp or FormApp.
  if (e && e.authMode == ScriptApp.AuthMode.NONE) {
    // Add a normal menu item (works in all authorization modes).
    menu.addItem('Start workflow', 'startWorkflow');
  } else {
    // Add a menu item based on properties (doesn't work in AuthMode.NONE).
    var properties = PropertiesService.getDocumentProperties();
    var workflowStarted = properties.getProperty('workflowStarted');
    if (workflowStarted) {
      menu.addItem('Check workflow status', 'checkWorkflow');
    } else {
      menu.addItem('Start workflow', 'startWorkflow');
    }
    // Record analytics.
    UrlFetchApp.fetch('http://www.example.com/analytics?event=open');
  }
  menu.addToUi();
}