Trình đơn tuỳ chỉnh cho tiện ích bổ sung của Trình chỉnh sửa
Tiện ích bổ sung cho trình chỉnh sửa đã xuất bản có thể tạo các mục trong trình đơn tuỳ chỉnh trong trình đơn Extensions (Tiện ích) của trình chỉnh sửa. Bạn có thể chèn trình đơn tiện ích bằng cách sử dụng phương thức Ui.createAddonMenu() và thêm các mục vào trình đơn đó bằng phương thức Menu.addItem(). Trình đơn thường được tạo trong phương thức onOpen(e) của tiện ích bổ sung.
Bạn có thể tạo trình đơn động thay đổi dựa trên hoạt động tương tác của người dùng hoặc trạng thái của tiện ích bổ sung. Tuy nhiên, tiện ích bổ sung phải tạo một trình đơn ban đầu trước khi người dùng uỷ quyền cho tiện ích bổ sung. Do đó, bạn phải kiểm tra chế độ uỷ quyền của tiện ích bổ sung trước khi tạo trình đơn trong onOpen(e). Đừng cố gắng thực hiện bất kỳ hành động nào yêu cầu uỷ quyền (chẳng hạn như kiểm tra tập lệnh Properties) khi tiện ích bổ sung đang ở trạng thái ScriptApp.AuthMode.NONE. Hãy xem phần vòng đời uỷ quyền để biết thêm thông tin chi tiết về các chế độ uỷ quyền và vòng đời.
Ví dụ sau đây cho biết cách tạo trình đơn tiện ích bổ sung động cho nhiều chế độ uỷ quyền:
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,["Cập nhật lần gần đây nhất: 2024-12-18 UTC."],[[["Published Editor Add-ons can create custom menu items under the Extensions menu using `Ui.createAddonMenu()` and `Menu.addItem()`, typically within the add-on's `onOpen(e)` method."],["While unpublished add-ons can create top-level menus, it's recommended to use `Ui.createAddonMenu()` for published add-ons to ensure consistent user experience."],["Add-ons must create an initial menu before user authorization and adjust menu items dynamically based on the authorization mode (`ScriptApp.AuthMode`) to avoid errors."],["The provided example demonstrates building a dynamic add-on menu that adapts to different authorization modes, using `ScriptApp.AuthMode.NONE` to control actions requiring authorization."]]],["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"]]