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 tuỳ chỉnh trong trình đơn Tiện ích của trình chỉnh sửa. Bạn có thể chèn một trình đơn bổ sung bằ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 các lượt 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, các tiện ích bổ sung phải tạo một trình đơn ban đầu trước khi người dùng cho phép 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ỳ thao tác nào yêu cầu uỷ quyền (chẳng hạn như kiểm tra Properties của tập lệnh) trong khi tiện ích đang ở trạng thái ScriptApp.AuthMode.NONE. Hãy xem vòng đời uỷ quyền để biết thêm thông tin chi tiết về các chế độ và vòng đời uỷ quyền.
Ví dụ sau đây cho thấy cách tạo một trình đơn tiện ích bổ sung động cho các chế độ uỷ quyền khác nhau:
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: 2025-07-31 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"]]