Prompt Dialog Box with Apps Script
The prompt method allows you to create a dialog box in Google Sheets that prompts the user to enter a value.
This method is slightly more complex than using Browser.inputBox but is also more functional (it allows you to retrieve both the text entered and the button clicked by the user).
Simple Dialog Box
To display a simple dialog box and retrieve only the entered text (without worrying about the button clicked), you can enter:
function example() {
const ui = SpreadsheetApp.getUi();
const result = ui.prompt('Enter the value to search for:');
ui.alert('The entered value is: ' + result.getResponseText());
}
Dialog Box with Title and Buttons
You can define a title and the buttons to display among the following button sets:
- OK: Ok button
- OK_CANCEL: Ok, Cancel buttons
- YES_NO: Yes, No buttons
- YES_NO_CANCEL: Yes, No, Cancel buttons
In this example, the second dialog box is displayed only if the user has entered a value and clicked Ok:
function example() {
const ui = SpreadsheetApp.getUi();
const result = ui.prompt('Search', 'Enter the value to search for:', ui.ButtonSet.OK_CANCEL);
const text = result.getResponseText();
if (text != '' && result.getSelectedButton() == ui.Button.OK) {
ui.alert('The entered value is: ' + text);
}
}
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