Google Apps Script: Create an Alert

Google Apps Script allows you to customize Google Apps. Part of your script can alert the user to provide them information or inform them that the script ran correctly. Here is the sample code to create an alert.

Script Editor

In a Google Sheets spreadsheet or in a Google Doc use the Tools menu to select “Script editor.”

UI

An alert is part of the user interface (UI). Define a variable to call the UI. Notice that the i in Ui is lowercase.

var ui = SpreadsheetApp.getUi();

After defining the UI, call an alert in the next line. Start with the ui variable you just defined. Type a period and choose alert from the options. Within the parenthesis is the message you wish to display. Remember that all text strings must go in single quotations.

ui.alert(‘text’);

Prompt

A prompt is similar to an alert; however, it allows the the user to input a value. Use prompt instead of alert for the method. Optionally you can define the buttons on the prompt.

Prompt Response

Asking for information is not very useful if you do not collect the response. Define a variable for the prompt. I use var = ask. In the next line define a variable for the response. The method getResponseText() will allow you to use the response to the prompt in your script.

My Referral Links

50% Off February Special

Online Workshops for Google Sheets, Classroom, Slides and more are 50% off for February. Use the discount code february at checkout. Also annual subscriptions for access to all of the Go Slow online workshops are 50% off!