“SpreadsheetApp.getUi()不能从这个上下文中调用” [英] "SpreadsheetApp.getUi() cannot be called from this context"

本文介绍了“SpreadsheetApp.getUi()不能从这个上下文中调用”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用:

Google表格(新)


$ b

AIM:

在电子表格中,显示从HTML创建的模式对话框,然后运行一个函数,然后自动关闭该HTML提示。
对话框一直保留到函数执行完成,然后自动消失。这个过程必须每3小时重复一次,并且脚本需要像我一样运行(因为我编辑了其他用户不允许的权限),所以简单的触发器可能无法工作(我读过您必须创建一个可安装的触发器如果您希望函数按照您的运行方式运行,而不是当前用户在给定时间运行的任何人)



我有什么:


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


我的代码>
这是我第一次使用脚本,所以请不要太反感!



Magic_Telling

从HTML文件创建模态对话框

 函数Magic_Telling(){
var UI = SpreadsheetApp.getUi();
var newline ='\\\
'
//显示带有自定义HtmlService内容的模式对话框。
var htmlOutput = HtmlService.createHtmlOutputFromFile('PromptStyling')
.setSandboxMode(HtmlService.SandboxMode.IFRAME)
.setWidth(300)
.setHeight(100);
UI.showModalDialog(htmlOutput,'');
}



提示类型

用于样式提示的HTML文件+运行函数All_In的脚本,用于处理行


 < html> 
< head>
//一些不相关的东西在这里
< / head>

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

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

< / script>
< / html>



All_In

处理函数行

 函数All_In(){

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

}




当我从脚本编辑器运行MagicTelling,它精美地运行 。整个序列执行(提示显示,All_In执行,提示消失)。完成。

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

问题:
我收到一条错误消息...

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

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

我该怎么做才能解决这个问题?请帮助,并提前感谢您的慷慨。我很乐意提供尽可能详细的信息。真的非常感谢任何帮助,完全失去了这里。

解决方案

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


USING:
Google Sheets (new)

AIM:
In a spreadsheet, show a modal dialog created from HTML, then run a function, then close that HTML prompt automatically. The dialog stays until the function finishes executing, then automatically disappears. 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)

WHAT I 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
This is my first-ever script, so please don't be too disgusted!

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

The problem: I get an 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? PLEASE HELP, and thanks in advance for your generosity. I'm happy to provide as much detail as you need. Really very grateful for any help, completely lost here.

解决方案

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天全站免登陆