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.
Tập lệnh có thể mở rộng một số sản phẩm của Google bằng cách thêm các phần tử giao diện người dùng. Khi được nhấp vào, các phần tử này sẽ thực thi một hàm Apps Script. Ví dụ phổ biến nhất là chạy một tập lệnh từ một mục trong trình đơn tuỳ chỉnh trong Google Tài liệu, Trang tính, Trang trình bày hoặc Biểu mẫu, nhưng các hàm tập lệnh cũng có thể được kích hoạt bằng cách nhấp vào hình ảnh và bản vẽ trong Google Trang tính.
Trình đơn tuỳ chỉnh trong Google Tài liệu, Trang tính, Trang trình bày hoặc Biểu mẫu
Apps Script có thể thêm trình đơn mới vào Google Tài liệu, Trang tính, Trang trình bày hoặc Biểu mẫu, trong đó mỗi mục trong trình đơn được liên kết với một hàm trong tập lệnh. (Trong Google Biểu mẫu, chỉ những người chỉnh sửa mở biểu mẫu để sửa đổi mới thấy được trình đơn tuỳ chỉnh, chứ không phải người dùng mở biểu mẫu để trả lời.)
Một tập lệnh chỉ có thể tạo trình đơn nếu được liên kết với tài liệu, bảng tính hoặc biểu mẫu.
Để trình đơn xuất hiện khi người dùng mở một tệp, hãy viết mã trình đơn trong hàm onOpen().
Ví dụ dưới đây cho thấy cách thêm một trình đơn có một mục, theo sau là một đường phân cách trực quan, sau đó là một trình đơn con có chứa một mục khác. (Xin lưu ý rằng trong Google Trang tính, trừ phi bạn đang sử dụng phiên bản mới, nếu không, bạn phải sử dụng cú pháp addMenu() và không thể dùng các trình đơn con.) Khi người dùng chọn một trong hai mục trong trình đơn, một hàm tương ứng sẽ mở hộp thoại cảnh báo. Để biết thêm thông tin về các loại hộp thoại mà bạn có thể mở, hãy xem hướng dẫn về hộp thoại và thanh bên.
functiononOpen(){varui=SpreadsheetApp.getUi();//OrDocumentApp,SlidesApporFormApp.ui.createMenu('Custom Menu').addItem('First item','menuItem1').addSeparator().addSubMenu(ui.createMenu('Sub-menu').addItem('Second item','menuItem2')).addToUi();}functionmenuItem1(){SpreadsheetApp.getUi()//OrDocumentApp,SlidesApporFormApp..alert('You clicked the first menu item!');}functionmenuItem2(){SpreadsheetApp.getUi()//OrDocumentApp,SlidesApporFormApp..alert('You clicked the second menu item!');}
Một tài liệu, bảng tính, bản trình bày hoặc biểu mẫu chỉ có thể chứa một trình đơn có tên cụ thể. Nếu cùng một tập lệnh hoặc một tập lệnh khác thêm một trình đơn có cùng tên, thì trình đơn mới sẽ thay thế trình đơn cũ. Bạn không thể xoá trình đơn khi tệp đang mở, mặc dù bạn có thể viết hàm onOpen() để bỏ qua trình đơn trong tương lai nếu bạn đặt một thuộc tính nhất định.
Hình ảnh và bản vẽ có thể nhấp vào trong Google Trang tính
Bạn cũng có thể chỉ định một hàm Apps Script cho hình ảnh hoặc bản vẽ trong Google Trang tính, miễn là tập lệnh đó được liên kết với bảng tính. Ví dụ dưới đây cho thấy cách thiết lập việc này.
Trong Google Trang tính, hãy chọn mục Tiện ích>Apps Script để tạo một tập lệnh được liên kết với bảng tính.
Xoá mọi mã trong trình chỉnh sửa tập lệnh rồi dán mã bên dưới vào.
function showMessageBox() {
Browser.msgBox('You clicked it!');
}
Quay lại Trang tính rồi chèn hình ảnh hoặc bản vẽ bằng cách chọn Chèn > Hình ảnh hoặc Chèn > Bản vẽ.
Sau khi chèn hình ảnh hoặc bản vẽ, hãy nhấp vào hình ảnh hoặc bản vẽ đó. Một bộ chọn trình đơn thả xuống nhỏ sẽ xuất hiện ở góc trên cùng bên phải. Nhấp vào biểu tượng này rồi chọn Chỉ định tập lệnh.
Trong hộp thoại xuất hiện, hãy nhập tên của hàm Apps Script mà bạn muốn chạy, không có dấu ngoặc đơn — trong trường hợp này là showMessageBox.
Nhấp vào OK.
Nhấp lại vào hình ảnh hoặc bản vẽ. Hàm hiện thực thi.
[[["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-08-31 UTC."],[[["\u003cp\u003eApps Script allows you to extend Google products like Docs, Sheets, Slides, and Forms by adding custom menus and clickable elements that trigger script functions.\u003c/p\u003e\n"],["\u003cp\u003eCustom menus can be created within these Google products, with each menu item linked to a specific function in your script, enhancing user interaction and functionality.\u003c/p\u003e\n"],["\u003cp\u003eIn Google Sheets, you can assign Apps Script functions to images and drawings, making them interactive elements that execute code when clicked in a web browser.\u003c/p\u003e\n"]]],[],null,["# Custom Menus in Google Workspace\n\nScripts can extend certain Google products by adding user-interface elements\nthat, when clicked, execute an Apps Script function. The most common example is\nrunning a script from a custom menu item in Google Docs, Sheets, Slides,\nor Forms, but script functions can also be triggered by clicking on images and\ndrawings in Google Sheets.\n\nCustom menus in Google Docs, Sheets, Slides, or Forms\n-----------------------------------------------------\n\nApps Script can add new menus in Google Docs, Sheets, Slides,\nor Forms, with\neach menu item tied to a function in a script. (In Google Forms, custom menus\nare visible only to an editor who opens the form to modify it, not to a user who\nopens the form to respond.)\n\nA script can only create a menu if it is\n[bound](/apps-script/scripts_containers) to the document, spreadsheet, or form.\nTo display the menu when the user opens a file, write the menu code within an\n[`onOpen()`](/apps-script/understanding_triggers) function.\n\nThe example below shows how to add a [menu](/apps-script/reference/base/menu)\nwith one item, followed by a\n[visual separator](/apps-script/reference/base/menu#addSeparator()), then a\n[sub-menu](/apps-script/reference/base/menu#addSubMenu(Menu)) that contains\nanother item. (Note that in Google Sheets, unless you're using the\n[new version](https://support.google.com/drive/answer/3541068), you must use the\n[`addMenu()`](/apps-script/reference/spreadsheet/spreadsheet#addMenu(String,Object))\nsyntax instead, and sub-menus are not possible.) When the user selects either\nmenu item, a corresponding function opens an\n[alert](/apps-script/reference/base/ui#alert(String)) dialog. For more\ninformation on the types of dialogs you can open, see the\n[guide to dialogs and sidebars](/apps-script/guides/dialogs). \n\n function onOpen() {\n var ui = SpreadsheetApp.getUi();\n // Or DocumentApp, SlidesApp or FormApp.\n ui.createMenu('Custom Menu')\n .addItem('First item', 'menuItem1')\n .addSeparator()\n .addSubMenu(ui.createMenu('Sub-menu')\n .addItem('Second item', 'menuItem2'))\n .addToUi();\n }\n\n function menuItem1() {\n SpreadsheetApp.getUi() // Or DocumentApp, SlidesApp or FormApp.\n .alert('You clicked the first menu item!');\n }\n\n function menuItem2() {\n SpreadsheetApp.getUi() // Or DocumentApp, SlidesApp or FormApp.\n .alert('You clicked the second menu item!');\n }\n\nA document, spreadsheet, presentation, or form can only contain one menu with\na given name. If the same script or another script adds a menu with the same\nname, the new menu replaces the old. Menus cannot be removed while the file\nis open, although you can write your `onOpen()` function to skip the menu in\nthe future if a certain [property](/apps-script/guides/properties) is set.\n| **Note:** [Editor add-ons](/workspace/add-ons/concepts/types#editor_add-ons) can have menu items as well, but use [special rules](/workspace/add-ons/concepts/menus) they are defined.\n\nClickable images and drawings in Google Sheets\n----------------------------------------------\n\nYou can also assign an Apps Script function to an image or drawing in Google Sheets,\nso long as the script is [bound](/apps-script/scripts_containers) to the\nspreadsheet. The example below shows how to set this up.\n\n1. In Google Sheets, select the menu item **Extensions** \\\u003e **Apps Script** to create a script that is bound to the spreadsheet.\n2. Delete any code in the script editor and paste in the code below.\n\n function showMessageBox() {\n Browser.msgBox('You clicked it!');\n }\n\n3. Return to Sheets and insert an image or drawing by selecting\n **Insert \\\u003e Image** or **Insert \\\u003e Drawing**.\n\n4. After inserting the image or drawing, click it. A small drop-down menu\n selector appears in the top right-hand corner. Click it and choose\n **Assign script**.\n\n5. In the dialog box that appears, type the name of the Apps Script function\n that you want to run, without parentheses --- in this case, `showMessageBox`.\n Click **OK**.\n\n6. Click the image or drawing again. The function now executes.\n\n| **Note:** The script execution is only triggered by clicking the image or drawing in a web browser. The script doesn't execute if the image or drawing is clicked on mobile."]]