Alert dialogs

Alert dialogs are a useful feature of Google Apps Script that allow you to display a pop-up message to the user when a certain condition is met. This can be particularly helpful for providing feedback or warnings to users when they are using your script.

Here is an example of how to create an alert dialog in Google Apps Script:

function showAlert() {
  var ui = SpreadsheetApp.getUi();
  ui.alert('Alert', 'This is an alert message!', ui.ButtonSet.OK);
}

In this example, we are using the SpreadsheetApp.getUi() method to get the UI service for the active spreadsheet. We then use the ui.alert() method to display a pop-up message to the user. The first parameter is the title of the alert dialog, the second parameter is the message that will be displayed, and the third parameter is the button set that will be displayed. In this case, we are using the OK button set, which will display a single OK button that the user can click to dismiss the dialog.

You can also customize the button set that is displayed by using the ui.ButtonSet class. For example, you can use the YES_NO button set to display two buttons labeled “Yes” and “No”. Here is an example:

function showConfirmation() {
  var ui = SpreadsheetApp.getUi();
  var response = ui.alert('Confirmation', 'Are you sure you want to delete this row?', ui.ButtonSet.YES_NO);
  if (response == ui.Button.YES) {
    // User clicked the "Yes" button
    // Do something...
  } else {
    // User clicked the "No" button
    // Do something else...
  }
}

In this example, we are using the YES_NO button set to display two buttons labeled “Yes” and “No”. We then use an if statement to check which button the user clicked, and perform different actions depending on the response.

Alert dialogs can be a useful tool for providing feedback and warnings to users when they are using your script. With the ui.alert() method, you can easily create custom alert dialogs that fit your specific needs.