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());
}
google sheets prompt

Dialog Box with Title and Buttons

You can define a title and the buttons to display among the following button sets:

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

Buttons: