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();}
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["没有我需要的信息","missingTheInformationINeed","thumb-down"],["太复杂/步骤太多","tooComplicatedTooManySteps","thumb-down"],["内容需要更新","outOfDate","thumb-down"],["翻译问题","translationIssue","thumb-down"],["示例/代码问题","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-07-31。"],[[["\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,["Published [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 }"]]