Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

google apps script - "SpreadsheetApp.getUi() cannot be called from this context"

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 = '
'
// 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...