Class Menu

  • Custom menus are part of the user interface for a Google App and can only be interacted with by container-bound scripts.

  • The provided example demonstrates how to create a custom menu with items, a separator, and a submenu using Apps Script.

  • Key methods for creating a custom menu includeaddItem to add menu options,addSeparator for visual separation,addSubMenu to add nested menus, andaddToUi to display the menu.

  • Each menu method returns theMenu object for chaining except foraddToUi, which returnsvoid.

Menu

A custom menu in an instance of the user interface for a Google App. A script can only interactwith the UI for the current instance of an open document or form, and only if the script iscontainer-bound to the document or form. For moreinformation, see theguide to menus.

// Add a custom menu to the active spreadsheet, including a separator and a// sub-menu.functiononOpen(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();}

Methods

MethodReturn typeBrief description
addItem(caption, functionName)MenuAdds an item to the menu.
addSeparator()MenuAdds a visual separator to the menu.
addSubMenu(menu)MenuAdds a sub-menu to the menu.
addToUi()voidInserts the menu into the instance of the editor's user interface.

Detailed documentation

addItem(caption, functionName)

Adds an item to the menu. The label for a menu item should be in sentence case (only the firstword capitalized).

Parameters

NameTypeDescription
captionStringThe label for the menu item, with only the first word capitalized.
functionNameStringThe name of the function to invoke when the user selects the item. You can use functions from included libraries, such asLibrary.libFunction1.

Return

Menu — ThisMenu, for chaining.


addSeparator()

Adds a visual separator to the menu.

Return

Menu — ThisMenu, for chaining.


addSubMenu(menu)

Adds a sub-menu to the menu.

Parameters

NameTypeDescription
menuMenuThe sub-menu, constructed like a top-level menu.

Return

Menu — ThisMenu, for chaining.


addToUi()

Inserts the menu into the instance of the editor's user interface.

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-12-11 UTC.