Stay organized with collections Save and categorize content based on your preferences.
Scripts can extend certain Google products by adding user-interface elements that, when clicked, execute an Apps Script function. The most common example is running a script from a custom menu item in Google Docs, Sheets, Slides, or Forms, but script functions can also be triggered by clicking on images and drawings in Google Sheets.
Apps Script can add new menus in Google Docs, Sheets, Slides, or Forms, with each menu item tied to a function in a script. (In Google Forms, custom menus are visible only to an editor who opens the form to modify it, not to a user who opens the form to respond.)
A script can only create a menu if it is bound to the document, spreadsheet, or form. To display the menu when the user opens a file, write the menu code within an onOpen()
function.
The example below shows how to add a menu with one item, followed by a visual separator, then a sub-menu that contains another item. (Note that in Google Sheets, unless you're using the new version, you must use the addMenu()
syntax instead, and sub-menus are not possible.) When the user selects either menu item, a corresponding function opens an alert dialog. For more information on the types of dialogs you can open, see the guide to dialogs and sidebars.
function onOpen() {
var ui = SpreadsheetApp.getUi();
// Or DocumentApp, SlidesApp or FormApp.
ui.createMenu('Custom Menu')
.addItem('First item', 'menuItem1')
.addSeparator()
.addSubMenu(ui.createMenu('Sub-menu')
.addItem('Second item', 'menuItem2'))
.addToUi();
}
function menuItem1() {
SpreadsheetApp.getUi() // Or DocumentApp, SlidesApp or FormApp.
.alert('You clicked the first menu item!');
}
function menuItem2() {
SpreadsheetApp.getUi() // Or DocumentApp, SlidesApp or FormApp.
.alert('You clicked the second menu item!');
}
A document, spreadsheet, presentation, or form can only contain one menu with a given name. If the same script or another script adds a menu with the same name, the new menu replaces the old. Menus cannot be removed while the file is open, although you can write your onOpen()
function to skip the menu in the future if a certain property is set.
You can also assign an Apps Script function to an image or drawing in Google Sheets, so long as the script is bound to the spreadsheet. The example below shows how to set this up.
Delete any code in the script editor and paste in the code below.
function showMessageBox() {
Browser.msgBox('You clicked it!');
}
Return to Sheets and insert an image or drawing by selecting Insert > Image or Insert > Drawing.
After inserting the image or drawing, click it. A small drop-down menu selector appears in the top right-hand corner. Click it and choose Assign script.
In the dialog box that appears, type the name of the Apps Script function that you want to run, without parentheses — in this case, showMessageBox
. Click OK.
Click the image or drawing again. The function now executes.
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-08-04 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-04 UTC."],[[["Apps Script allows you to extend Google products like Docs, Sheets, Slides, and Forms by adding custom menus and clickable elements that trigger script functions."],["Custom 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."],["In 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."]]],[]]
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4