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...');
}
google sheets alert

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);
}
google apps script alert

The buttons you can define:

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!');
  }
}
google apps script alert confirmation

The buttons: