Alert Dialog Box with Apps Script
The alert method allows you to create dialog boxes, choose the buttons to display, add a title, and retrieve the user's choice.
This method is slightly more complex than using Browser.msgBox but also more functional.
Simple Display
To display an informative dialog box (without seeking to know which button the user clicked) enter:
function example() {
SpreadsheetApp.getUi().alert('Informative message...');
}
Title and Buttons
You can add 2 more arguments to define the title and the buttons to display:
function example() {
const ui = SpreadsheetApp.getUi();
ui.alert('Example', 'Informative message...', ui.ButtonSet.OK_CANCEL);
}
The buttons you can define:
- OK: Ok button
- OK_CANCEL: Ok, Cancel buttons
- YES_NO: Yes, No buttons
- YES_NO_CANCEL: Yes, No, Cancel buttons
Clicked Button
To know which button the user clicked, you need to retrieve the value returned by the alert method, for example, to perform an action in case of a click on the Yes button:
function example() {
const ui = SpreadsheetApp.getUi();
const click = ui.alert('Deletion', 'Are you sure about this?', ui.ButtonSet.YES_NO);
if (click == ui.Button.YES) {
ui.alert('Deletion confirmed!');
}
}
The buttons:
- OK: click on the Ok button
- YES: click on the Yes button
- NO: click on the No button
- CANCEL: click on the Cancel button
- CLOSE: click on the window's close button