Mantenha tudo organizado com as coleções
Salve e categorize o conteúdo com base nas suas preferências.
Os complementos do editor publicados
podem criar itens de menu personalizados no menu Extensões do editor. Você pode
inserir um menu de complementos usando o
método Ui.createAddonMenu()
e adicionar itens a ele usando o
método Menu.addItem(). Os menus geralmente são criados no método onOpen(e) do complemento.
É possível criar menus dinâmicos que mudam com base nas interações do usuário ou no estado do complemento. No entanto, os complementos precisam criar um menu inicial antes de serem
autorizados pelo usuário. Por isso, verifique o modo de autorização do complemento antes de criar menus em onOpen(e). Não tente realizar nenhuma ação
que exija autorização (como verificar o script
Properties)
enquanto o complemento estiver em ScriptApp.AuthMode.NONE. Consulte o
ciclo de vida da autorização
para mais detalhes sobre os modos e o ciclo de vida da autorização.
O exemplo a seguir mostra como criar um menu de complemento dinâmico para diferentes
modos de autorização:
functiononOpen(e){varmenu=SpreadsheetApp.getUi().createAddonMenu();//OrDocumentApporSlidesApporFormApp.if(e && e.authMode==ScriptApp.AuthMode.NONE){//Addanormalmenuitem(worksinallauthorizationmodes).menu.addItem('Start workflow','startWorkflow');}else{//Addamenuitembasedonproperties(doesn't work in AuthMode.NONE).varproperties=PropertiesService.getDocumentProperties();varworkflowStarted=properties.getProperty('workflowStarted');if(workflowStarted){menu.addItem('Check workflow status','checkWorkflow');}else{menu.addItem('Start workflow','startWorkflow');}//Recordanalytics.UrlFetchApp.fetch('http://www.example.com/analytics?event=open');}menu.addToUi();}
[null,null,["Última atualização 2025-07-31 UTC."],[[["\u003cp\u003ePublished Editor Add-ons can create custom menu items under the Extensions menu using \u003ccode\u003eUi.createAddonMenu()\u003c/code\u003e and \u003ccode\u003eMenu.addItem()\u003c/code\u003e, typically within the add-on's \u003ccode\u003eonOpen(e)\u003c/code\u003e method.\u003c/p\u003e\n"],["\u003cp\u003eWhile unpublished add-ons can create top-level menus, it's recommended to use \u003ccode\u003eUi.createAddonMenu()\u003c/code\u003e for published add-ons to ensure consistent user experience.\u003c/p\u003e\n"],["\u003cp\u003eAdd-ons must create an initial menu before user authorization and adjust menu items dynamically based on the authorization mode (\u003ccode\u003eScriptApp.AuthMode\u003c/code\u003e) to avoid errors.\u003c/p\u003e\n"],["\u003cp\u003eThe provided example demonstrates building a dynamic add-on menu that adapts to different authorization modes, using \u003ccode\u003eScriptApp.AuthMode.NONE\u003c/code\u003e to control actions requiring authorization.\u003c/p\u003e\n"]]],["Editor add-ons create custom menu items under the **Extensions** menu using `Ui.createAddonMenu()` and `Menu.addItem()`, typically within the `onOpen(e)` method. Menus must be defined *before* user authorization, necessitating a check of the add-on's authorization mode. Dynamic menus can change based on user interactions. Actions requiring authorization should not be performed when `AuthMode.NONE`. The provided example shows a dynamic menu construction for different modes, adding either \"Start workflow\" or \"Check workflow status\".\n"],null,["# Custom menus for Editor add-ons\n\nPublished [Editor add-ons](/workspace/add-ons/concepts/types#editor_add-ons)\ncan create custom menu items under their editor's **Extensions** menu. You can\ninsert an add-on menu by using the\n[`Ui.createAddonMenu()`](/apps-script/reference/base/ui#createAddonMenu()) method\nand add items to it using the\n[`Menu.addItem()`](/apps-script/reference/base/menu#addItem(String,String))\nmethod. Menus are usually created in the add-on's `onOpen(e)` method.\n| **Caution:** Unpublished add-ons can also [create custom top-level menus](/apps-script/guides/menus#custom_menus_in_google_docs_sheets_slides_or_forms), but these are automatically moved under the **add-ons** menu if the add-on is published and may not result in the user experience you intended. If you intend to publish the add-on, always use [`Ui.createAddonMenu()`](/apps-script/reference/base/ui#createAddonMenu()) to define the add-on menu.\n\nYou can create dynamic menus that change based on user interactions or add-on\nstate. However, add-ons must create an initial menu *before* the add-on is\nauthorized by the user. Because of this, you must check the add-on's\n[authorization mode](/workspace/add-ons/concepts/editor-auth-lifecycle#authorization_modes)\nprior to constructing menus in `onOpen(e)`. Do not attempt to take any action\nthat requires authorization (such as checking the script\n[`Properties`](/apps-script/reference/properties))\nwhile the add-on is in `ScriptApp.AuthMode.NONE`. See the\n[authorization lifecycle](/workspace/add-ons/concepts/editor-auth-lifecycle#the_complete_lifecycle)\nfor more details on the authorization modes and lifecycle.\n| **Warning:** If you attempt to take actions that require authorization when the authorization mode is `ScriptApp.AuthMode.NONE`, it results in an error. This may prevent your add-on menus from being displayed.\n\nThe following example shows how to build a dynamic add-on menu for different\nauthorization modes: \n\n function onOpen(e) {\n var menu = SpreadsheetApp.getUi().createAddonMenu(); // Or DocumentApp or SlidesApp or FormApp.\n if (e && e.authMode == ScriptApp.AuthMode.NONE) {\n // Add a normal menu item (works in all authorization modes).\n menu.addItem('Start workflow', 'startWorkflow');\n } else {\n // Add a menu item based on properties (doesn't work in AuthMode.NONE).\n var properties = PropertiesService.getDocumentProperties();\n var workflowStarted = properties.getProperty('workflowStarted');\n if (workflowStarted) {\n menu.addItem('Check workflow status', 'checkWorkflow');\n } else {\n menu.addItem('Start workflow', 'startWorkflow');\n }\n // Record analytics.\n UrlFetchApp.fetch('http://www.example.com/analytics?event=open');\n }\n menu.addToUi();\n }"]]