簡體   English   中英

來自庫的 Google Script HTML 表單拋出錯誤未捕獲

[英]Google Script HTML form from Library throws error Uncaught

我有一個像這樣的 HTML 格式的庫:

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 中創建了一個綁定腳本:

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

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

我添加了帶有標識符的庫:TT。

接下來,我使用綁定代碼刷新了我的 Google 表格文件以查看菜單“測試”,運行測試 > 運行。 HTML 窗口出現了。 當我點擊按鈕時,什么也沒發生。 當我打開控制台時,我看到了錯誤:

在此處輸入圖像描述

如果我不使用庫,則不會出現此錯誤。

請幫我解決這個問題。

我和你經歷過同樣的情況。 就我而言,問題的原因是圖書館方面的授權。

  • 當使用庫中范圍的授權過程沒有在庫端完成時,我確認發生了Uncaught的錯誤。
  • 當在庫端完成使用庫中范圍的授權過程時,我確認沒有出現Uncaught的錯誤。

也就是說,在我的環境中,我確認當庫用於您的情況時,需要為客戶端和庫端授權范圍。

因此,作為一種解決方法,我使用了以下流程。

解決方法:

  1. 創建一個 Google Apps 腳本庫。
    • 請將您的code.gsh.html腳本復制並粘貼到獨立腳本或容器綁定腳本。
  2. 將 Google Apps 腳本部署為庫。
  3. 例如,在您的腳本中,請直接在庫端運行hello()並授權范圍。
  4. 將庫安裝到客戶端並從客戶端加載庫。
  5. 請在客戶端運行myFunction()

通過此流程,當您在自定義菜單上運行run並單擊按鈕時,將打開executed對話框。

筆記:

  • 在這種情況下,當我想讓用戶使用客戶端腳本時,需要對客戶端和庫端的范圍進行授權。 我想這可能有點不方便。
  • 那么,如何為 Google 問題跟蹤器報告此問題? Ref不幸的是,我找不到具有相同情況的問題跟蹤器。

添加:

作為從客戶端授權庫端范圍的方法,我想建議使用 Web 應用程序。 我以為在使用Web Apps時,庫端的授權可以在客戶端完成。 至此,我認為不便之處可能會得到一點解決。

請執行以下流程。

1.圖書館方面。

請復制並粘貼以下腳本。

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

<!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 Apps。

請在庫側部署 Web 應用程序。 關於這個方法,可以看官方文檔。 參考詳細設置如下。

  • Execute as: User accessing the web app
  • Who has access: Anyone with Google account

3. 部署為庫。

請部署為庫。 參考

4.客戶端。

請將庫安裝到客戶端。 並且,請復制並粘貼以下腳本。 在這種情況下,請將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. 測試。

首先,請在自定義菜單中運行auth 這樣,您可以授權客戶端和庫端的范圍。 如果運行auth時沒有打開新選項卡,請在腳本編輯器中再次運行auth()

作為下一步,請運行run 這樣,您的對話框就打開了。 並且,當兩個auth (客戶端和庫端)都已經完成時,當您單擊按鈕時,將打開executed對話框。

參考:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM