简体   繁体   English

来自库的 Google Script HTML 表单抛出错误未捕获

[英]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.就我而言,问题的原因是图书馆方面的授权。

  • 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.当使用库中范围的授权过程没有在库端完成时,我确认发生了Uncaught的错误。
  • 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.当在库端完成使用库中范围的授权过程时,我确认没有出现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.因此,作为一种解决方法,我使用了以下流程。

Workaround:解决方法:

  1. Create a Google Apps Script library.创建一个 Google Apps 脚本库。
    • Please copy and paste your script of code.gs and h.html to the standalone script or the container-bound script.请将您的code.gsh.html脚本复制并粘贴到独立脚本或容器绑定脚本。
  2. Deploy the Google Apps Script as the library.将 Google Apps 脚本部署为库。
  3. In your script, for example, please directly run hello() at the library side, and authorize the scopes.例如,在您的脚本中,请直接在库端运行hello()并授权范围。
  4. Install the library to the client side and load the library from the client side.将库安装到客户端并从客户端加载库。
  5. Please run 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对话框。

Note:笔记:

  • 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?那么,如何为 Google 问题跟踪器报告此问题? Ref Unfortunately, I couldn't find the issue tracker with the same situation. Ref不幸的是,我找不到具有相同情况的问题跟踪器。

Added:添加:

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.请执行以下流程。

1. Library side. 1.图书馆方面。

Please copy and paste the following scripts.请复制并粘贴以下脚本。

Google Apps Script: code.gs Google Apps 脚本: code.gs

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

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

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

HTML: h.html 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. Deploy Web Apps at library side. 2、在库端部署Web Apps。

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

3. Deploy as library. 3. 部署为库。

Please deploy as the library.请部署为库。 Ref 参考

4. Client side. 4.客户端。

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

5. Testing. 5. 测试。

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对话框。

References:参考:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 google.script.run 从 html 调用库 function - Call Library function from html with google.script.run 当 HTML 表单输入字段为空时,Google Apps 脚本出错 - Error in Google Apps Script when HTML form input field is empty function google 脚本和 javascript 未捕获的错误 - Uncaught Error at function google script and javascript Google Apps 脚本 HTML 表单 - Google Apps Script HTML Form 未捕获的引用错误:未定义 jQuery | 从 HTML 按钮触发 python 脚本 | 谷歌应用引擎 - Uncaught ReferenceError: jQuery is not defined | Trigger python script from HTML button | Google App Engine 从谷歌脚本到 html - from google script to html Javascript未捕获类型错误:无法从HTML表单读取未定义的属性“值” - Javascript Uncaught Type Error: Cannot read property 'value' of undefined from HTML Form 从HTML表单使用Angular JS发布数据。 未捕获的错误:[$ injector:modulerr] - Post data with Angular JS from HTML form. Uncaught Error: [$injector:modulerr] 从带有脚本标签的已发布 Google 表格中读取数据会引发 `net::ERR_TOO_MANY_REDIRECTS` 错误 - Reading data from a published Google Sheets with a script tag throws a `net::ERR_TOO_MANY_REDIRECTS` error ZeroClipboard JavaScript库引发未捕获的错误:悬停在Flash对象上时,在NPObject上调用方法出错 - ZeroClipboard JavaScript library throws Uncaught Error: Error calling method on NPObject while hovering on the flash object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM