Menu personalizzati per i componenti aggiuntivi Editor
Mantieni tutto organizzato con le raccolte
Salva e classifica i contenuti in base alle tue preferenze.
I componenti aggiuntivi Editor pubblicati
possono creare voci di menu personalizzate nel menu Estensioni dell'editor. Puoi
inserire un menu dei componenti aggiuntivi utilizzando il
metodo
Ui.createAddonMenu()
e aggiungere elementi utilizzando il
metodo
Menu.addItem(). I menu vengono in genere creati nel metodo onOpen(e) del componente aggiuntivo.
Puoi creare menu dinamici che cambiano in base alle interazioni degli utenti o allo stato del componente aggiuntivo. Tuttavia, i componenti aggiuntivi devono creare un menu iniziale prima che vengano autorizzati dall'utente. Per questo motivo, prima di creare i menu in onOpen(e), devi controllare la
modalità di autorizzazione
del componente aggiuntivo. Non tentare di eseguire alcuna azione
che richieda l'autorizzazione (ad esempio il controllo dello script
Properties)
mentre il componente aggiuntivo è in ScriptApp.AuthMode.NONE. Per ulteriori dettagli sulle modalità di autorizzazione e sul ciclo di vita, consulta la sezione
Ciclo di vita dell'autorizzazione.
L'esempio seguente mostra come creare un menu dei componenti aggiuntivi dinamico per diverse modalità di autorizzazione:
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,["Ultimo aggiornamento 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 }"]]