Trình đơn tuỳ chỉnh cho tiện ích bổ sung của Trình chỉnh sửa
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
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();}
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["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."]]],[]]