5

There's this Q&A: "SpreadsheetApp.getUi() cannot be called from this context"

As you can see, the question is huge, and the answer is correct but incomplete, so both need editing, and the question body needs to be massively edited to help newbies.

Specifically, the error is in the title: SpreadsheetApp.getUi() cannot be called from this context.
Therefore, the question body could say:

I have this function that works ok if I call it from a google app (like sheets, docs, forms, etc.):

function showDialog(){
   var ui = SpreadsheetApp.getUi();
   ui.alert("Something");
}

but it triggers error SpreadsheetApp.getUi() cannot be called from this context when I run it via script triggers.
How to solve the issue?

This is a common error, and the reason is the .getUi method got called from places where the UI is not available. As the person who answered said, one example (and most common) is called by time triggered functions, which doesn't have the UI.

Therefore, the correct solution to prevent this failure is to just wrap the call towards the UI in a try catch, or even implement a method like this:

function isUIAvailable(){
  try {
    SpreadsheetApp.getUi();
    return true;
  } catch (e) {
    return false
  }
}

And alert or use the ui only if (isUIAvailable()) .


How should I approach this? Edit both the question and the answer? That seems a bit intrusive, to massively edit it like that.

However, the alternative that I think of doesn't seem right either: re-asking it properly. That is a clear duplicate.

3
  • 8
    "and the question body needs to be massively edited to help newbies." - no, it doesn't. Entirely the wrong motivation. It needs a little editing to make it better readable, for everyone. What this question can probably use is a new better answer, not a lot of editing.
    – Gimby
    Oct 28, 2019 at 9:53
  • hmm isn't it TL;DR? should have minimal details to reproduce the issue, not exponentially more to confuse newbies...
    – Adelin
    Oct 28, 2019 at 9:53
  • 2
    @Gimby Agree on the wrong motivation (I don't see any reason to single out newbies as the group that would benefit from a rewrite), but inclined to disagree on the conclusion. I don't know Google Apps Script and won't personally touch this, but my first impression is that a total rewrite along the lines of what Adelin suggests would be a massive improvement. The question as it stands is tediously long, a good example of failing the "minimal" aspect of crafting an MCVE. Replacing it with a slimmed down example will save man-days, maybe man-weeks, of wasted reading over its next 10k views.
    – Mark Amery
    Oct 28, 2019 at 16:35

1 Answer 1

7

I do not believe the question is particularly unclear. I attempted to perform a minimally invasive edit to remove unnecessary formatting and make the question slightly more to the point.

There is still room for improvement, but I still do not find it hard to read or follow. It clearly shows:

  • what the user is trying to do
  • how they are trying to do it
  • and what output they get.

The answer is short and succinct, but otherwise correct. You are right that a more complete, in depth answer could be provided. No need to edit the accepted answer, though. Just post your own, better answer.

From your question here, it looks like you want to convert this question in a broader, canonical-like Q&A. Personally, I believe that if you write a complete enough answer for this one there is no need for that.

You can address the original issue, explain its origin, offer solutions to go around the problem (when and if possible), and it can very nicely be used as a dupe target for this family of questions.

2
  • 1
    "From your question here, it looks like you want to convert this question in a broader, canonical-like Q&A. Personally, I believe that if you write a complete enough answer for this one there is no need for that." - inclined to disagree. The question as written is long enough to be offputting, and, if it indeed boils down to the same issue as shown in the alternate wording in the question, there's no good reason for it to be that way. Relieving its thousands of readers of the need to wade through all that irrelevant cruft surely must be a good thing.
    – Mark Amery
    Oct 28, 2019 at 16:40
  • 1
    Obviously, I disagree. I find it easy enough to "wade" through that question. And I gather anyone actually having a similar issue would as well. But if you think otherwise, I believe it would be more useful for you to post an answer instead of posting comments.
    – yivi
    Oct 28, 2019 at 18:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .