简体   繁体   English

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

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

I've been working through the example code as best I can, and I've got an addon that when you click the custom menu, it pops up a dialog based on an html file (the HtmlService class stuff).我一直在尽我所能完成示例代码,我有一个插件,当您单击自定义菜单时,它会弹出一个基于 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');
}

I can't however seem to load any external javascript libraries from this html file.但是,我似乎无法从此 html 文件加载任何外部 javascript 库。 If they're included with the script tag, no errors are thrown, but any attempt to execute javascript within the html file fails silently.如果它们包含在脚本标记中,则不会抛出任何错误,但任何在 html 文件中执行 javascript 的尝试都会以静默方式失败。 I understand that there is a security model, but the documentation is unenlightening.我知道有一个安全模型,但文档没有启发性。

Is this actual html file (an iframe? I can't find it in Dev Tools, assuming that Sheets is rendering everything with a canvas or something like that)?这是实际的 html 文件(一个 iframe?我在开发工具中找不到它,假设 Sheets 正在使用画布或类似的东西渲染所有内容)?

I believe I've read the documentation thoroughly at (and linked from): https://developers.google.com/apps-script/reference/html/我相信我已经彻底阅读了文档(并链接自): https : //developers.google.com/apps-script/reference/html/

... yet I'm not seeing anything I recognize as relevant to what I'm attempting. ......但我没有看到任何我认为与我正在尝试的东西相关的东西。

Specifically, I'm hoping to be able to use the Dracula graphing library (or perhaps another similar one) to implement a new type of chart.具体来说,我希望能够使用 Dracula 图形库(或者可能是另一个类似的)来实现一种新型图表。 I'll need to be able to execute the methods contained in that library.我需要能够执行该库中包含的方法。

To load external javascript , you have to usescriplets in templated HTML (partially taken from GAS documentation)加载外部 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>

And in your gs code you would have something like this在你的 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.

相关问题 如何使用 eval(UrlFetchApp.fetch()) 在我的谷歌应用程序脚本中加载外部 javascript? - How do I use eval(UrlFetchApp.fetch()) to load external javascript in my google apps script? 将Tabulator等外部库加载到Google Apps脚本中 - Loading an external library such as Tabulator into Google Apps Script 如何从外部源加载javascript并在Google Apps脚本中执行 - How to load javascript from external source and execute it in Google Apps Script Google Apps脚本:如何使用电子表格中的数据查询外部数据库? - Google Apps Script: how to use data in a spreadsheet to query a external database? 如何从javascript下载外部javascript库依赖关系? - How do you download an external javascript library dependency from javascript? 将外部JavaScript导入谷歌应用脚​​本 - importing external javascript to google apps script 如何将参数传递给Google Apps脚本调试器? - How do you pass an argument to the Google Apps Script debugger? 如何使用外部Javascript库(如jQuery)重命名Google Closure样式表? - How do I use Google Closure Stylesheet renaming with an external Javascript Library such as jQuery? 如何在Google Apps脚本中使用Dictionary javascript类? - How to use the Dictionary javascript class in Google Apps Script? 如何使用 javascript 变量分配 Google Apps 脚本变量? - How do I assign Google Apps Script variable with javascript variable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM