[英]Google Script HTML form from Library throws error Uncaught
I have a library with HTML-form like this:我有一个像这样的 HTML 格式的库:
code.gs
: code.gs
:
function openDialog() {
SpreadsheetApp.getUi().showModalDialog(HtmlService.createHtmlOutputFromFile("h"), "Test" );
}
function hello() {
console.log('booo');
}
h.html
: h.html
:
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<button id="b">Click me</button>
<script>
var b = document.getElementById('b');
b.onclick = function() {
google.script.run
.withSuccessHandler(function(str){window.alert("executed");})
// .withFailureHandler(function(error){window.alert("failed");})
.hello();
}
</script>
</body>
</html>
I shared this script for view and deployd it as a library.我共享了这个脚本以供查看并将其部署为库。 Next I created a bound script in Google Sheet with this code:
接下来,我使用以下代码在 Google Sheet 中创建了一个绑定脚本:
function onOpen() {
SpreadsheetApp.getUi().createMenu('test').addItem('run', 'myFunction').addToUi();
}
var hello = function() {};
function myFunction() {
TT.openDialog();
}
I've added the library with identifier: TT.我添加了带有标识符的库:TT。
Next I refreshed my Google Sheet file with bound code to see the menu "test", ran test > run.接下来,我使用绑定代码刷新了我的 Google 表格文件以查看菜单“测试”,运行测试 > 运行。 The HTML-window appeared.
HTML 窗口出现了。 When I clicked the button, nothing happened.
当我点击按钮时,什么也没发生。 When I opened console, I saw the error:
当我打开控制台时,我看到了错误:
This error does not appear if I do not use library.如果我不使用库,则不会出现此错误。
Please help me to resolve this.请帮我解决这个问题。
I have experienced the same situation with you.我和你经历过同样的情况。 In my case, the reason of the issue was due to the authorization at the library side.
就我而言,问题的原因是图书馆方面的授权。
Uncaught
occurred.Uncaught
的错误。Uncaught
didn't occur.Uncaught
的错误。 Namely, in my environment, I confirmed that when the library is used for your situation, it was required to authorize the scopes for both the client side and the library side.也就是说,在我的环境中,我确认当库用于您的情况时,需要为客户端和库端授权范围。
So, as a workaround, I used the following flow.因此,作为一种解决方法,我使用了以下流程。
code.gs
and h.html
to the standalone script or the container-bound script.code.gs
和h.html
脚本复制并粘贴到独立脚本或容器绑定脚本。hello()
at the library side, and authorize the scopes.hello()
并授权范围。myFunction()
at the client side.myFunction()
。 By this flow, when you run run
at the custom menu and click the button, the dialog of executed
is opened.通过此流程,当您在自定义菜单上运行
run
并单击按钮时,将打开executed
对话框。
As the method for authorizing the scopes at the library side from the client side, I would like to propose to use Web Apps.作为从客户端授权库端范围的方法,我想建议使用 Web 应用程序。 I thought that when the Web Apps is used, the authorization of the library side can be done at the client side.
我以为在使用Web Apps时,库端的授权可以在客户端完成。 By this, I thought that the inconvenience may be resolved a little.
至此,我认为不便之处可能会得到一点解决。
Please do the following flow.请执行以下流程。
Please copy and paste the following scripts.请复制并粘贴以下脚本。
code.gs
code.gs
function openDialog() {
SpreadsheetApp.getUi().showModalDialog(HtmlService.createHtmlOutputFromFile("h"), "Test" );
}
function hello() {
console.log('booo');
}
function doGet() {
return HtmlService.createHtmlOutput("ok");
}
h.html
h.html
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<button id="b">Click me</button>
<script>
var b = document.getElementById('b');
b.onclick = function() {
google.script.run
.withSuccessHandler(function(str){window.alert("executed");})
// .withFailureHandler(function(error){window.alert("failed");})
.hello();
}
</script>
</body>
</html>
Please deploy Web Apps at the library side.请在库侧部署 Web 应用程序。 About the method for this, you can see the official document.
关于这个方法,可以看官方文档。 Ref The detail setting is as follows.
参考详细设置如下。
Execute as: User accessing the web app
Who has access: Anyone with Google account
Please deploy as the library.请部署为库。 Ref
参考
Please install the library to the client side.请将库安装到客户端。 And, please copy and paste the following scripts.
并且,请复制并粘贴以下脚本。 In this case, please replace
https://script.google.com/macros/s/###/exec
with your Web Apps URL.在这种情况下,请将
https://script.google.com/macros/s/###/exec
替换为您的 Web 应用程序 URL。
function onOpen() {
SpreadsheetApp.getUi().createMenu('test').addItem('auth', 'auth').addItem('run', 'myFunction').addToUi();
}
var hello = function() {};
function myFunction() {
TT.openDialog();
}
function auth() {
const html = HtmlService.createHtmlOutput(`<input type="button" value="Authorize" onclick="window.open('https://script.google.com/macros/s/###/exec', '_blank');google.script.host.close()">`);
SpreadsheetApp.getUi().showDialog(html);
}
At first, please run auth
at the custom menu.首先,请在自定义菜单中运行
auth
。 By this, you can authorize the scopes of both the client side and the library side.这样,您可以授权客户端和库端的范围。 When the new tab is not opened when
auth
is run, please run auth()
at the script editor again.如果运行
auth
时没有打开新选项卡,请在脚本编辑器中再次运行auth()
。
As the next step, please run run
.作为下一步,请运行
run
。 By this, your dialog is opened.这样,您的对话框就打开了。 And, when both authorizations (client and library side) with
auth
has already been finished, when you click the button, the dialog of executed
is opened.并且,当两个
auth
(客户端和库端)都已经完成时,当您单击按钮时,将打开executed
对话框。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.