Inconsistent Apps Script behavior

1,246 views
Skip to first unread message

Kevin -

unread,
Aug 21, 2022, 7:09:24 AM8/21/22
to Google Apps Script Community
Sometimes the following code works fine when the calling function is invoked from the editor:
function alertMsg(titlepromptbuttons) {
  var ui = SpreadsheetApp.getUi();
  switch (buttons) {
    case "OK"buttons = ui.ButtonSet.OKbreak;
    case "YES_NO"buttons = ui.ButtonSet.YES_NObreak;
    case "YES_NO_CANCEL" : buttons = ui.ButtonSet.YES_NO_CANCELbreak;
    case "OK_CANCEL"buttons = ui.ButtonSet.OK_CANCEL;
  }
  return ui.alert(titlepromptbuttons);
}
From the code editor, I click on the browser tab for the Sheet and there's the alert waiting form me to click Ok, Yes, whatever I'm testing. Then other times (no indication whatsoever that anything has changed), I get the error " Exception: Cannot call SpreadsheetApp.getUi() from this context". --- Really?? I just did yesterday!
The same thing happens with my createMenu function. It's been nice being able to make code updates (like add a new menu item), run the function from the editor to update the menu, then switch over to the Sheet tab where the the new menu item ready to be clicked on. Is there some mystery switch I accidentally tripped that causes this, or is Google playing with the code?

Edward Ulle

unread,
Aug 21, 2022, 11:22:13 AM8/21/22
to Google Apps Script Community
I doubt you can run alertMsg from the script editor since title, prompt and buttons would be undefined.  It must be executed from some other context.  A custom function, onEdit(), menu option?

cbmserv...@gmail.com

unread,
Aug 21, 2022, 2:31:06 PM8/21/22
to google-apps-sc...@googlegroups.com

The spreadsheet UI is only available in some cases and not in others. The code actually stops the software execution until you respond to the prompt. So this is not good software to use when you want to run something on a timer-based trigger because you may not be online to respond to the prompt.

 

As Edward indicated, check what triggers you have turned on and understand how/when this software is being run.

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/fe9fb707-4987-462f-83ba-9c86dd866613n%40googlegroups.com.

Kevin -

unread,
Aug 21, 2022, 2:58:07 PM8/21/22
to google-apps-sc...@googlegroups.com
Ok. Thanks for your replies. Much appreciated.

You received this message because you are subscribed to a topic in the Google Groups "Google Apps Script Community" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-apps-script-community/-5ybw9hEhI4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/003a01d8b58c%24295058d0%247bf10a70%24%40gmail.com.
Reply all
Reply to author
Forward
0 new messages