Search code examples
google-apps-scriptgoogle-cloud-platformuser-input

Displaying a prompt and getting user input in Google Apps script bound to a Google Sheet


I have an Google sheet with some apps script code in it and I am trying to make it interactive by displaying a prompt and getting some user input. I have added a simple function to test that this works:

function displayPrompt() {

  var ui = SpreadsheetApp.getUi();
  var result = ui.prompt("Please enter a name for your report:");
  
    Logger.log(result.getResponseText());
     
};

When I run it, I get the error: "Exception: Cannot call SpreadsheetApp.getUi() from this context."

What am I doing wrong please?

Thanks Annette


Solution

  • If I run it using a trigger it works as soon as I open the file

    function onOpen() {
      var ui = SpreadsheetApp.getUi();
      var result = ui.prompt("Please enter a name for your report:");
    
      Logger.log(result.getResponseText());
    }