Let Me Tink About It
Let Me Tink About It

Reputation: 16102

How to trigger UI class for Google Sheets?

My goal

Here is the documentation I'm trying to follow.

// Display a dialog box with a title, message, input field, and "Yes" and "No" buttons. The
// user can also close the dialog by clicking the close button in its title bar.
var ui = SpreadsheetApp.getUi();
var response = ui.prompt('Getting to know you', 'May I know your name?', ui.ButtonSet.YES_NO);

// Process the user's response.
if (response.getSelectedButton() == ui.Button.YES) {
  Logger.log('The user\'s name is %s.', response.getResponseText());
} else if (response.getSelectedButton() == ui.Button.NO) {
  Logger.log('The user didn\'t want to provide a name.');
} else {
  Logger.log('The user clicked the close button in the dialog\'s title bar.');
}

What I expect to happen and what happens instead

I expect the UI to display a dialog box with a title, message, input field, and "Yes" and "No" buttons. The user can also close the dialog by clicking the close button in its title bar.

But instead, nothing happens.

Steps to reproduce.

  1. Open a Google Sheet.
  2. Open Script Editor: Tools > Script editor
  3. Paste above code into script editor.
  4. Close script editor.
  5. Navigate to Google Sheet.

But as I mentioned, nothing actually happened.

How do I trigger the UI instance to initiate? What am I doing wrong?

Upvotes: 0

Views: 337

Answers (1)

Cooper
Cooper

Reputation: 64032

Run this function:

function runIt() {
  var ui = SpreadsheetApp.getUi();
  var response = ui.prompt('Getting to know you', 'May I know your name?', ui.ButtonSet.YES_NO);
  if (response.getSelectedButton() == ui.Button.YES) {
    Logger.log('The user\'s name is %s.', response.getResponseText());
  } else if (response.getSelectedButton() == ui.Button.NO) {
    Logger.log('The user didn\'t want to provide a name.');
  } else {
    Logger.log('The user clicked the close button in the dialog\'s title bar.');
  }
}

After getting the function to run go look at the spreadsheet.

enter image description here

Upvotes: 1

Related Questions