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

查看:19
本文介绍了库中的 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 Sheet 中创建了一个绑定脚本:

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 Sheet 文件以查看菜单测试",运行测试 >跑.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.

请帮我解决这个问题.

推荐答案

我和你遇到了同样的情况.在我的情况下,问题的原因是由于图书馆方面的授权.

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.gsh.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并点击按钮时,会打开executed对话框.

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

  • 在这种情况下,当我想让用户使用客户端脚本时,需要对客户端和库端的范围进行授权.我想这可能有点不方便.
  • 那么,如何向 Google 问题跟踪器报告此问题?Ref 不幸的是,我找不到相同情况的问题跟踪器.
  • 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.关于这个方法,可以看官方文档.Ref 详细设置如下.

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

请部署为库.参考

请将库安装到客户端.并且,请复制并粘贴以下脚本.在这种情况下,请将 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 的授权(客户端和库端)都已经完成时,当你点击按钮时,executed 对话框被打开.

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