Create a Custom Menu Option


The onOpen trigger is mainly used to add in additional menu options that can help you execute scripts without having to open up your Script Editor. In the sample code below, we'll show you how to add in a Custom Menu Item and also link one of the options to a script that options up a dialog box.

The following code will create the Custom Menu Item and set it so that when the user clicks on the 'Open Dialog' option, a function named openDialog() will be executed.

function onOpen() { // FormApp.getUi() // For Google Forms // DocumentApp.getUi() // For Google Docs // SlidesApp.getUi() // For Google Slides SpreadsheetApp.getUi() // For Google Sheets .createMenu('Custom Menu Option') .addItem('Open Dialog', 'openDialog') .addToUi() }



The openDialog() function below opens up a custom dialog box which simply renders the text "Hello world!" in bold and sets the dialog box's title to "A custom dialog box"

function openDialog() { // FormApp.getUi() // For Google Forms // DocumentApp.getUi() // For Google Docs // SlidesApp.getUi() // For Google Slides SpreadsheetApp.getUi() // For Google Sheets .showModalDialog(HtmlService.createHtmlOutput('<b>Hello world!</b>'), 'A custom dialog box') }
thumbnail

NitroGAS Chrome Extension

Want to reference this code snippet right from your Google Apps Script Project? Check out our NitroGAS Chrome Extension. This tool will help you build your scripts faster than you could imagine. The tool itself and access to the code snippets are 100% FREE - Happy Coding!

Get the Extension