簡體   English   中英

您如何在 Google Apps 插件腳本中使用外部 javascript 庫?

[英]How do you use an external javascript library in a Google Apps addon script?

我一直在盡我所能完成示例代碼,我有一個插件,當您單擊自定義菜單時,它會彈出一個基於 html 文件(HtmlService 類內容)的對話框。

function showDialog() {
  var html = HtmlService.createHtmlOutputFromFile('dialog')
      .setSandboxMode(HtmlService.SandboxMode.IFRAME)
      .setWidth(400)
      .setHeight(300);
  SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
      .showModalDialog(html, 'Need a name for this');
}

但是,我似乎無法從此 html 文件加載任何外部 javascript 庫。 如果它們包含在腳本標記中,則不會拋出任何錯誤,但任何在 html 文件中執行 javascript 的嘗試都會以靜默方式失敗。 我知道有一個安全模型,但文檔沒有啟發性。

這是實際的 html 文件(一個 iframe?我在開發工具中找不到它,假設 Sheets 正在使用畫布或類似的東西渲染所有內容)?

我相信我已經徹底閱讀了文檔(並鏈接自): https : //developers.google.com/apps-script/reference/html/

......但我沒有看到任何我認為與我正在嘗試的東西相關的東西。

具體來說,我希望能夠使用 Dracula 圖形庫(或者可能是另一個類似的)來實現一種新型圖表。 我需要能夠執行該庫中包含的方法。

加載外部 javascript ,您必須在模板化 HTML 中使用腳本(部分取自 GAS 文檔)

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
    <?!= include('StylesheetFileName'); ?>
  </head>
  <body>
    <h1>Welcome</h1>
    <p>Please enjoy this helpful script.</p>
    <?!= include('JavaScriptFileName'); ?>
  </body>
</html>

在你的 gs 代碼中你會有這樣的東西

function showDialog() {
var html = HtmlService.createTemplateFromFile('dialog')
            .evaluate()
            .setSandboxMode(HtmlService.SandboxMode.IFRAME)
            .setWidth(400)
            .setHeight(300);
        SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
        .showModalDialog(html, 'Dialog Title');
}

function include(filename) {
  return HtmlService.createHtmlOutputFromFile(filename)
      .getContent();
}

暫無
暫無

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

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