库中的Google Script HTML表单抛出未捕获的错误 [英] Google Script HTML form from Library throws error Uncaught

查看:49
本文介绍了库中的Google Script HTML表单抛出未捕获的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的HTML格式的库:

I have a library with HTML-form like this:

code.gs :

function openDialog() {
  SpreadsheetApp.getUi().showModalDialog(HtmlService.createHtmlOutputFromFile("h"), "Test" );
}

function hello() {
  console.log('booo');
}

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>

我共享了此脚本以供查看,并将其部署为库.接下来,我使用以下代码在Google表格中创建了绑定脚本:

I shared this script for view and deployd it as a library. Next I created a bound script in Google Sheet with this code:

function onOpen() {
  SpreadsheetApp.getUi().createMenu('test').addItem('run', 'myFunction').addToUi();
}

var hello = function() {};
function myFunction() {
  TT.openDialog();
}

我添加了标识符为TT的库.

I've added the library with identifier: TT.

接下来,我用绑定代码刷新了Google表格文件,以查看菜单测试",然后运行测试>跑步.HTML窗口出现.当我单击按钮时,什么也没发生.当打开控制台时,我看到了错误:

Next I refreshed my Google Sheet file with bound code to see the menu "test", ran test > run. The HTML-window appeared. 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 错误.
  • 当在库侧完成使用库中的范围的授权过程时,我确认没有发生 Uncaught 的错误.
  • When the authorization process for using the scopes in the library is NOT done at the library side, I confirmed that the error of Uncaught occurred.
  • When the authorization process for using the scopes in the library is done at the library side, I confirmed that the error of Uncaught didn't occur.

也就是说,在我的环境中,我确认当您使用库时,需要同时授权客户端和库端的作用域.

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.

  1. 创建一个Google Apps脚本库.
    • 请将 code.gs h.html 的脚本复制并粘贴到独立脚本或容器绑定脚本中.
  1. Create a Google Apps Script library.
    • Please copy and paste your script of code.gs and h.html to the standalone script or the container-bound script.

通过此流程,当您在自定义菜单上运行 run 并单击按钮时,将打开 execute 对话框.

By this flow, when you run run at the custom menu and click the button, the dialog of executed is opened.

  • 在这种情况下,当我想让用户使用客户端脚本时,需要授权客户端和库双方的作用域.我认为这可能有点不方便.
  • 那么,如何为Google问题跟踪器报告此问题呢?参考不幸的是,我找不到具有相同情况的问题跟踪器.
  • In this case, when I wanted to make users use the client script, it was required to authorize the scopes for both the client side and the library side. I thought that this may be a little inconvenient.
  • So, how about reporting this for the Google issue tracker? Ref Unfortunately, I couldn't find the issue tracker with the same situation.

作为从客户端在库端授权范围的方法,我想建议使用Web Apps.我以为使用Web Apps时,可以在客户端进行库侧的授权.由此,我认为不便之处可以解决.

As the method for authorizing the scopes at the library side from the client side, I would like to propose to use Web Apps. I thought that when the Web Apps is used, the authorization of the library side can be done at the client side. By this, I thought that the inconvenience may be resolved a little.

请执行以下流程.

请复制并粘贴以下脚本.

Please copy and paste the following scripts.

function openDialog() {
  SpreadsheetApp.getUi().showModalDialog(HtmlService.createHtmlOutputFromFile("h"), "Test" );
}

function hello() {
  console.log('booo');
}

function doGet() {
  return HtmlService.createHtmlOutput("ok");
}

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>

2.在库侧部署Web应用.

请在库侧部署Web Apps.关于此方法,您可以查看官方文档.参考详细设置如下.

  • 执行为:用户访问网络应用程序
  • 有权访问的人:具有Google帐户的任何人

请作为库部署.参考

请将该库安装到客户端.并且,请复制并粘贴以下脚本.在这种情况下,请用您的Web Apps URL替换 https://script.google.com/macros/s/###/exec .

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.

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);
}

5.测试.

首先,请在自定义菜单上运行 auth .这样,您可以授权客户端和库双方的范围.如果在运行 auth 时未打开新选项卡,请再次在脚本编辑器中运行 auth().

5. Testing.

At first, please run auth at the custom menu. 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.

下一步,请运行 run .这样,您的对话框将打开.并且,当具有 auth 的两个授权(客户端和库侧)都已经完成时,单击按钮时,将打开 execute 的对话框.

As the next step, please 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.

这篇关于库中的Google Script HTML表单抛出未捕获的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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