This post goes over how to use alert, confirm, and prompt with Google Sheets Macros.
Alert
Open a dialog box with an OK
button:
SpreadsheetApp.getUi().alert('Message');
See the documentation.
Confirm
Open a dialog box with Yes
and No
buttons:
const ui = SpreadsheetApp.getUi();
const response = ui.alert('Title', 'Message', ui.ButtonSet.YES_NO);
switch (response) {
case ui.Button.YES:
console.log('Yes');
break;
case ui.Button.NO:
console.log('No');
break;
default:
console.log('Clicked outside');
break;
}
SpreadsheetApp.getActive().toast(response);
See the documentation.
Prompt
Open an input dialog box with an OK
button:
const ui = SpreadsheetApp.getUi();
const response = ui.prompt('Input:');
if (response.getSelectedButton() === ui.Button.OK) {
const text = response.getResponseText();
console.log(text);
SpreadsheetApp.getActive().toast(text);
}
Open an input dialog box with Yes
, No
, and Cancel
buttons:
const ui = SpreadsheetApp.getUi();
const response = ui.prompt('Input:');
const text = response.getResponseText();
switch (response.getSelectedButton()) {
case ui.Button.YES:
console.log('Yes', text);
break;
case ui.Button.NO:
console.log('No', text);
break;
case ui.Button.CANCEL:
console.log('Cancel', text);
break;
default:
console.log('Clicked outside', text);
break;
}
SpreadsheetApp.getActive().toast(text);
See the documentation.
Alternatively, there’s the deprecated Browser.inputBox
and Browser.msgBox
, which are no longer recommended.