0

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

  • see [1](https://stackoverflow.com/questions/63809904/exception-cannot-call-spreadsheetapp-getui-from-this-context-line-1) and [2](https://stackoverflow.com/questions/65270842/exception-cannot-call-spreadsheetapp-getui-from-this-context-line-2-file) – Kos Jan 25 '22 at 16:19
  • I'm assuming you are trying to run it from the script editor. That' what "context" means. Add a menu item [link](https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet-app#getUi()) and try to run it from the menu while your are in the spreadsheet "context". – TheWizEd Jan 25 '22 at 16:21
  • It runs just fine when run from the script editor – Cooper Jan 25 '22 at 17:13
  • I was trying to run it from the script editor, which is when I got the error, It works if I add it as a menu item so I'm going to do that – Annette Dellevoet Jan 26 '22 at 17:30

1 Answers1

0

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());
}
David Salomon
  • 804
  • 1
  • 7
  • 24