Google Apps Script: Create an Alert

Google Apps Script: Create an Alert

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.”
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’);

Sample Script

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.

var ui = SpreadsheetApp.getUi();
ui.prompt(‘What is your name?’, ui.ButtonSet.YES_NO);

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.

function prompt(){

var ui = SpreadsheetApp.getUi();
var ask = ui.prompt(‘What is your name?’, ui.ButtonSet.YES_NO);
var response = ask.getResponseText();
}

Sample

CLICK HERE for a spreadsheet with an Add-On “Alert sample” to demonstrate alerts and prompts.

© 2024 All Rights Reserved.

❤️ Become a Premium Teacher Tech Member

Discount applies until you cancel!! 
Normally $12 a month, save 58%!! 

Monthly installments of only $4.99 a month.

Exclusive Teacher Tech Content! 
Exclusive Online Courses
Premium Add-ons for Google Workspace
Office Hours with Alice
Weekly Check In Support
Exclusive Webinars
And more! 

Exit this pop up by pressing escape or clicking anywhere off the pop up.

💥 FREE OTIS WORKSHOP

Join Alice Keeler, Thursday Oct24th or register to gain access to the recording.
Create a free OTIS account.

Join Alice Keeler for this session for using FigJam to start every lesson.

Exit this pop up by pressing escape or clicking anywhere off the pop up.