Class Menu
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
菜单
Google 应用界面实例中的自定义菜单。脚本只能与打开的文档或表单的当前实例的界面互动,并且只有在脚本绑定到文档或表单的容器时才能执行此操作。如需了解详情,请参阅菜单指南。
// Add a custom menu to the active spreadsheet, including a separator and a
// sub-menu.
function onOpen(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();
}
详细文档
addItem(caption, functionName)
向菜单添加项。菜单项的标签应采用句首字母大写形式(仅首个字母大写)。
参数
名称 | 类型 | 说明 |
caption | String | 菜单项的标签,仅第一个字词采用大写形式。 |
functionName | String | 在用户选择该项时调用的函数的名称。您可以使用所包含的库中的函数,例如 Library.libFunction1 。 |
返回
Menu
- 此 Menu
,用于链接。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-26。
[null,null,["最后更新时间 (UTC):2025-07-26。"],[[["\u003cp\u003eThe \u003ccode\u003eMenu\u003c/code\u003e class allows you to create custom menus in Google Apps Script, adding items, separators, and submenus.\u003c/p\u003e\n"],["\u003cp\u003eMenus can be used to provide users with easy access to script functionalities within the active document or form.\u003c/p\u003e\n"],["\u003cp\u003eMenu items are linked to specific functions within your script using the \u003ccode\u003eaddItem()\u003c/code\u003e method.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003eaddToUi()\u003c/code\u003e inserts the created menu into the user interface of the current Google App instance.\u003c/p\u003e\n"],["\u003cp\u003eThe provided code example demonstrates creating a basic custom menu with a submenu.\u003c/p\u003e\n"]]],[],null,["# Class Menu\n\nMenu\n\nA custom menu in an instance of the user interface for a Google App. A script can only interact\nwith the UI for the current instance of an open document or form, and only if the script is [container-bound](/apps-script/scripts_containers) to the document or form. For more\ninformation, see the [guide to menus](/apps-script/guides/menus).\n\n```javascript\n// Add a custom menu to the active spreadsheet, including a separator and a\n// sub-menu.\nfunction onOpen(e) {\n SpreadsheetApp.getUi()\n .createMenu('My Menu')\n .addItem('My Menu Item', 'myFunction')\n .addSeparator()\n .addSubMenu(\n SpreadsheetApp.getUi()\n .createMenu('My Submenu')\n .addItem('One Submenu Item', 'mySecondFunction')\n .addItem('Another Submenu Item', 'myThirdFunction'),\n )\n .addToUi();\n}\n``` \n\n### Methods\n\n| Method | Return type | Brief description |\n|-----------------------------------------------------------|-------------|--------------------------------------------------------------------|\n| [addItem(caption, functionName)](#addItem(String,String)) | [Menu](#) | Adds an item to the menu. |\n| [addSeparator()](#addSeparator()) | [Menu](#) | Adds a visual separator to the menu. |\n| [addSubMenu(menu)](#addSubMenu(Menu)) | [Menu](#) | Adds a sub-menu to the menu. |\n| [addToUi()](#addToUi()) | `void` | Inserts the menu into the instance of the editor's user interface. |\n\nDetailed documentation\n----------------------\n\n### `add``Item(caption, functionName)`\n\nAdds an item to the menu. The label for a menu item should be in sentence case (only the first\nword capitalized).\n\n#### Parameters\n\n| Name | Type | Description |\n|------------------|----------|---------------------------------------------------------------------------------------------------------------------------------------------------|\n| `caption` | `String` | The label for the menu item, with only the first word capitalized. |\n| `function``Name` | `String` | The name of the function to invoke when the user selects the item. You can use functions from included libraries, such as `Library.libFunction1`. |\n\n#### Return\n\n\n[Menu](#) --- This [Menu](#), for chaining.\n\n*** ** * ** ***\n\n### `add``Separator()`\n\nAdds a visual separator to the menu.\n\n#### Return\n\n\n[Menu](#) --- This [Menu](#), for chaining.\n\n*** ** * ** ***\n\n### `add``Sub``Menu(menu)`\n\nAdds a sub-menu to the menu.\n\n#### Parameters\n\n| Name | Type | Description |\n|--------|-----------|--------------------------------------------------|\n| `menu` | [Menu](#) | The sub-menu, constructed like a top-level menu. |\n\n#### Return\n\n\n[Menu](#) --- This [Menu](#), for chaining.\n\n*** ** * ** ***\n\n### `add``To``Ui()`\n\nInserts the menu into the instance of the editor's user interface."]]