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.

Digiprove sealCopyright secured by Digiprove © 2015