“无法从此上下文调用SpreadsheetApp.getUi()"; [英] "SpreadsheetApp.getUi() cannot be called from this context"

查看:19
本文介绍了“无法从此上下文调用SpreadsheetApp.getUi()";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Google 表格电子表格中,我想显示一个从 HTML 创建的模式对话框,然后运行一个函数,然后自动关闭该 HTML 提示.

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.

这个过程必须每 3 小时重复一次,并且脚本需要像我一样运行(因为我有其他用户没有的编辑权限)所以简单的触发器可能不起作用(我读过你必须创建一个可安装的触发器,如果​​您希望该功能以您的身份运行,而不是在给定时间的当前用户是谁)

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)

我目前拥有:

  1. .gs 函数 Magic_Telling,使用 HTML 文件创建模态对话框
  2. 一个 HTML 文件 Prompt_Styling,其中包含提示的 css/html 样式.这个 HTML 文件然后调用一个 .gs 函数 All_In 来处理行

我的代码:

Magic_Telling从 HTML 文件创建模态对话框.

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 文件 + 运行将处理行的函数 All_In 的脚本


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 All_In() {

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

}

当我从脚本编辑器运行 MagicTelling 时,它运行良好.整个序列执行(提示显示,All_In 执行,提示消失).完美.

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

然后我通过转到创建了一个可安装的触发器脚本编辑器 > 资源 > 当前项目的触发器并添加了一个触发器来每 3 小时运行一次 Magic_Telling.(我认为这是一个可安装的触发器")

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")

但我收到此错误消息:

无法从此上下文调用 SpreadsheetApp.getUi().

Cannot call SpreadsheetApp.getUi() from this context.

...当函数到达 Magic_Telling 的第一行时

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

我应该怎么做才能解决这个问题?

What should I do to get around this?

推荐答案

Ui Dialogs 不能被时间触发函数调用,它们必须被用户操作触发,也就是说点击在菜单项或某种按钮上调用显示 UI 的函数.

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.

这篇关于“无法从此上下文调用SpreadsheetApp.getUi()";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆