Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Skrip dapat memperluas produk Google tertentu dengan menambahkan elemen antarmuka pengguna yang, saat diklik, akan menjalankan fungsi Apps Script. Contoh yang paling umum adalah
menjalankan skrip dari item menu kustom di Google Dokumen, Spreadsheet, Slide,
atau Formulir, tetapi fungsi skrip juga dapat dipicu dengan mengklik gambar dan
gambar di Google Spreadsheet.
Menu kustom di Google Dokumen, Spreadsheet, Slide, atau Formulir
Apps Script dapat menambahkan menu baru di Google Dokumen, Spreadsheet, Slide, atau Formulir, dengan setiap item menu dikaitkan ke fungsi dalam skrip. (Di Google Formulir, menu kustom hanya terlihat oleh editor yang membuka formulir untuk mengubahnya, bukan oleh pengguna yang membuka formulir untuk merespons.)
Skrip hanya dapat membuat menu jika
terikat ke dokumen, spreadsheet, atau formulir.
Untuk menampilkan menu saat pengguna membuka file, tulis kode menu dalam fungsi
onOpen().
Contoh di bawah menunjukkan cara menambahkan menu
dengan satu item, diikuti dengan
pemisah visual, lalu
sub-menu yang berisi
item lain. (Perhatikan bahwa di Google Spreadsheet, kecuali jika Anda menggunakan
versi baru, Anda harus menggunakan sintaksis
addMenu(), dan sub-menu tidak dapat digunakan.) Saat pengguna memilih salah satu item menu, fungsi yang sesuai akan membuka dialog alert. Untuk mengetahui informasi selengkapnya tentang jenis dialog yang dapat Anda buka, lihat panduan untuk dialog dan sidebar.
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!');}
Dokumen, spreadsheet, presentasi, atau formulir hanya dapat berisi satu menu dengan
nama tertentu. Jika skrip yang sama atau skrip lain menambahkan menu dengan nama yang sama, menu baru akan menggantikan menu lama. Menu tidak dapat dihapus saat file terbuka, meskipun Anda dapat menulis fungsi onOpen() untuk melewati menu pada masa mendatang jika properti tertentu ditetapkan.
Gambar dan gambar yang dapat diklik di Google Spreadsheet
Anda juga dapat menetapkan fungsi Apps Script ke gambar atau diagram di Google Spreadsheet,
selama skrip terikat ke
spreadsheet. Contoh di bawah menunjukkan cara menyiapkannya.
Di Google Spreadsheet, pilih item menu Ekstensi>Apps Script untuk membuat
skrip yang terikat ke spreadsheet.
Hapus semua kode di editor skrip dan tempel kode di bawah.
function showMessageBox() {
Browser.msgBox('You clicked it!');
}
Kembali ke Spreadsheet dan sisipkan gambar atau diagram dengan memilih
Sisipkan > Gambar atau Sisipkan > Diagram.
Setelah menyisipkan gambar atau membuat gambar, klik gambar tersebut. Pemilih menu drop-down kecil akan muncul di pojok kanan atas. Klik, lalu pilih
Tetapkan skrip.
Di kotak dialog yang muncul, ketik nama fungsi Apps Script yang ingin Anda jalankan, tanpa tanda kurung — dalam hal ini, showMessageBox.
Klik Oke.
[[["Mudah dipahami","easyToUnderstand","thumb-up"],["Memecahkan masalah saya","solvedMyProblem","thumb-up"],["Lainnya","otherUp","thumb-up"]],[["Informasi yang saya butuhkan tidak ada","missingTheInformationINeed","thumb-down"],["Terlalu rumit/langkahnya terlalu banyak","tooComplicatedTooManySteps","thumb-down"],["Sudah usang","outOfDate","thumb-down"],["Masalah terjemahan","translationIssue","thumb-down"],["Masalah kode / contoh","samplesCodeIssue","thumb-down"],["Lainnya","otherDown","thumb-down"]],["Terakhir diperbarui pada 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."]]