Class Menu
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
เมนู
เมนูที่กำหนดเองในอินสแตนซ์ของอินเทอร์เฟซผู้ใช้สำหรับแอป Google สคริปต์จะโต้ตอบกับ UI สำหรับอินสแตนซ์ปัจจุบันของเอกสารหรือแบบฟอร์มที่เปิดอยู่ได้ก็ต่อเมื่อสคริปต์เชื่อมโยงกับคอนเทนเนอร์ของเอกสารหรือแบบฟอร์มเท่านั้น ดูข้อมูลเพิ่มเติมได้ที่คู่มือเกี่ยวกับเมนู
// 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
นี้สําหรับการต่อเชื่อม
addSeparator()
เพิ่มตัวแบ่งภาพลงในเมนู
รีเทิร์น
Menu
— Menu
นี้สําหรับการต่อเชื่อม
addToUi()
แทรกเมนูลงในอินสแตนซ์ของอินเทอร์เฟซผู้ใช้ของเครื่องมือแก้ไข
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-07-26 UTC
[null,null,["อัปเดตล่าสุด 2025-07-26 UTC"],[[["\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."]]