14

In a Google Sheets spreadsheet, I want to show a modal dialog created from HTML, then run a function, then close that HTML prompt automatically.

The dialog should stay until the function finishes executing, then automatically disappear.

This process has to be repeated every 3 hours, and the script needs to run as me (as I have edit permissions that other users do not) so simple triggers probably won't work (I've read that you must create an installable trigger if you want the function to run as you and not whoever the current user is at the given time)

I currently have:

  1. A .gs function Magic_Telling, that creates a modal dialog by using an HTML file
  2. An HTML file, Prompt_Styling, that contains the css / html styling for the prompt. This HTML file then calls a .gs function All_In that processes the rows

My code:

Magic_Telling Creates the modal dialog from HTML file.

function Magic_Telling() {
var UI = SpreadsheetApp.getUi();
var newline = '\n'
// Display a modal dialog box with custom HtmlService content.
var htmlOutput = HtmlService.createHtmlOutputFromFile('PromptStyling')
    .setSandboxMode(HtmlService.SandboxMode.IFRAME)
    .setWidth(300)
    .setHeight(100);
UI.showModalDialog(htmlOutput, ' ');
}


Prompt_Styling HTML file for styling prompt + script that runs the function All_In that will process rows

<html>
<head>
// some irrelevant stuff here
</head>

<script>
window.onload = function() {    
google.script.run
    .withSuccessHandler(closeDialog)
    .All_In();
    };

window.closeDialog = function() {
    google.script.host.close();
    };

</script>
</html>

All_In Function to process rows

function All_In() {

UnlockRowBlocks();
UnhideRowBlocks();
LockRowBlocks();
HideRowBlocks();

}

When I run MagicTelling from the script editor, it works beautifully. The entire sequence executes (prompt shown, All_In executed, prompt disappeared). Perfect.

I then created an installable trigger by going to Script Editor > Resources > Current project's triggers and added a trigger to run Magic_Telling every 3 hours. (I presume this is an "installable trigger")

But I get this error message:

Cannot call SpreadsheetApp.getUi() from this context.

...when the function reaches the first line of Magic_Telling

What should I do to get around this?

Rubén
  • 29,320
  • 9
  • 61
  • 145
Manit Shah
  • 185
  • 1
  • 2
  • 10

3 Answers3

19

Ui Dialogs can not be called by time triggered functions, they have to be triggered by a user action, that's to say a click on a menu item or some sort of button that calls the function showing the UI.

Serge insas
  • 44,113
  • 6
  • 95
  • 123
  • Thanks @Sergeinsas that was helpful information that I didn't know. Couple of follow-ups: 1. I have to manually click something _every 3 hours_ if I want this to work!? Can you think of any workaround that will avoid this? 2. If I understand you correctly, if I create a button and assign Magic_Telling to it, then the three functions should at least execute? (but of course it would be done manually, which sucks) – Manit Shah Mar 20 '16 at 17:55
  • 1
    You could use a sidebar with client side JavaScript that could update every 3 hours automatically... this would be less obvious than a modal dialog but could meet your requirements. – Serge insas Mar 20 '16 at 18:01
  • 1
    Ah, thanks for that (though I have no clue how to do that). How about this for a potential workaround? Can I store a 0 value somewhere in spreadsheet cell A1, store a timestamp in cell A2, then calculate (using a formula) the duration between now() and the timestamp value, and if that duration exceeds 3 hours, then edit the 0 value to 1? Then run the function Magic_Telling every time the value in cell A1 == 1? Can you think of any more elegant way to do this? P.S. - Checked out your book, looks very useful – Manit Shah Mar 20 '16 at 18:03
  • 2
    Wanted to add to the discussion. I was getting this error, but also when triggered from the context menu I got an error like `no permission to call showSidebar`. This is kinda unrelated but I ended up in this thread multiple times. For me the solution was this: I was setting explicit scopes in appsscript.json and needed to add `https://www.googleapis.com/auth/script.container.ui` to the `oauthScopes` array. Thought it might help someone! – kapoko Aug 04 '18 at 17:30
3

Simple case getting the 'Cannot call SpreadsheetApp.getUi() from this context.'-Error for everybody who just got started with scripting using the Tools/Scripteditor Menu.

In this case you work with a standalone script only, meaning, your script is just attached to one document or spreadsheet. The standalone script allows i.e. to simply call doc = DocumentApp.getActiveDocument(), the active Document the script is attached to.

It happened to me that I used ' var ui = SpreadsheetApp.getUi();' while getting the ERROR message in quest here... and it took me hours to find out what went wrong with this simple line as I went down all the way to Oauth Scopes and the Developer Console.

So, it might be helpful for some beginners to know that I actually used the var ui = SpreadsheetApp.getUi(); within a Document-Script. Very clear I got the error, but ... Hope this is helpful for some simple scripters!

PS. I hope it is needless to mention that using a 'var ui = DocumentApp.getUi();' within a SpreadSheet will produce a similar Error message.

1

if this error happened then check the triggers or close the script editor tab and refresh google sheets then open the script project .

make sure that the appscript is bound script and not standalone script . or the getUi() wont work .

check all my courses about appscript here http://playlist.mashoun.com/

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 28 '21 at 23:16
  • 1
    Your answer could be improved with additional supporting information. Please [edit](https://stackoverflow.com/posts/70510511/edit) to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](https://stackoverflow.com/help/how-to-answer). – taylor.2317 Dec 29 '21 at 17:48
  • Just to add to this information... close the script editor tab because it **might have been detached** from your associated spreadsheet tab, and reopened the script again. – Giraldi Feb 09 '22 at 15:13