In Google Apps Script for Google Sheets, there are three main ways to show popups (dialogs) to users:
Browser.msgBox()
SpreadsheetApp.getUi().alert()
SpreadsheetApp.getUi().prompt()
These functions allow you to notify users, request confirmations, or gather simple input.
Best for: Simple alerts or confirmations.
Example:
Browser.msgBox("Edit detected!", "You changed a cell.", Browser.Buttons.OK);
Use Case: Quick notification or decision request without needing text input.
Best for: Alerts with more flexible button sets, Sheet-specific behavior.
Key Features:
Returns which button the user clicked
Example:
SpreadsheetApp.getUi().alert("Proceed?", "Do you want to continue with this change?", ui.ButtonSet.YES_NO);
Use Case: When you want structured feedback from users (like Yes or No).
Best for: Collecting short user input (like a comment or name).
Key Features:
Displays a prompt with a text field
Example:
SpreadsheetApp.getUi().prompt("Comment Required", "What is the reason for your edit?", ui.ButtonSet.OK_CANCEL);
Use Case: When you need users to enter something in response to an action.
Note:
To use the examples given below, simply rename the specific function you're using so that it's just "onEdit", without the underscore and number suffix.
These functions will work when the file is edited while the user is logged in.
/**
* 1. Browser.msgBox – Simple Alert
*/
function onEdit_1(e) {
Browser.msgBox("Edit detected!", "You changed a cell.", Browser.Buttons.OK);
}
/**
* 2. SpreadsheetApp.getUi().alert() – Custom Alert
*/
function onEdit_2(e) {
const ui = SpreadsheetApp.getUi();
ui.alert("Change detected in the spreadsheet!");
}
/**
* 3. SpreadsheetApp.getUi().alert() - Custom Alert with Buttons
*/
function onEdit_3(e) {
const ui = SpreadsheetApp.getUi();
const result = ui.alert("Proceed?", "Do you want to continue with this change?", ui.ButtonSet.YES_NO);
if (result == ui.Button.YES) {
Logger.log("User said YES.");
} else {
Logger.log("User said NO.");
}
}
/**
* 4. SpreadsheetApp.getUi().prompt() – Simple Input
*/
function onEdit_4(e) {
const ui = SpreadsheetApp.getUi();
const response = ui.prompt("Please enter a note for this change:");
if (response.getSelectedButton() === ui.Button.OK) {
Logger.log("User input: " + response.getResponseText());
} else {
Logger.log("User cancelled.");
}
}
/**
* 5. SpreadsheetApp.getUi().prompt() – Title, Message, and Input
*/
function onEdit_5(e) {
const ui = SpreadsheetApp.getUi();
const response = ui.prompt("Comment Required", "What is the reason for your edit?", ui.ButtonSet.OK_CANCEL);
if (response.getSelectedButton() === ui.Button.OK) {
const comment = response.getResponseText();
Logger.log("Comment entered: " + comment);
} else {
Logger.log("User cancelled input.");
}
}