您如何在Google Apps插件脚本中使用外部javascript库? [英] How do you use an external javascript library in a Google Apps addon script?

查看:86
本文介绍了您如何在Google Apps插件脚本中使用外部javascript库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尽最大努力研究示例代码,并且有一个插件,当您单击自定义菜单时,它会弹出一个基于html文件的对话框(HtmlService类的东西). /p>

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).

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库.如果它们包含在script标记中,则不会引发任何错误,但是在html文件中执行javascript的任何尝试都将失败,并且会以静默方式失败.我知道有一个安全模型,但是文档没有启发性.

I can't however seem to load any external javascript libraries from this html file. If they're included with the script tag, no errors are thrown, but any attempt to execute javascript within the html file fails silently. I understand that there is a security model, but the documentation is unenlightening.

这是实际的html文件(一个iframe吗?假设Sheets使用画布或类似的东西渲染所有内容,我在开发工具中找不到它吗?)

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)?

我相信我已经在以下位置(并链接到)彻底阅读了该文档: https://developers.google.com/apps-script/reference/html/

I believe I've read the documentation thoroughly at (and linked from): https://developers.google.com/apps-script/reference/html/

...但是我看不到任何与我尝试的内容相关的东西.

... yet I'm not seeing anything I recognize as relevant to what I'm attempting.

具体地说,我希望能够使用Dracula图形库(或也许另一个类似的库)来实现一种新型的图表.我需要能够执行该库中包含的方法.

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. I'll need to be able to execute the methods contained in that library.

推荐答案

scriplets 在HTML模板中(部分取自GAS文档)

To load external javascript, you have to use scriplets in templated HTML (partially taken from GAS documentation)

<!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代码中,您将遇到类似的情况

And in your gs code you would have something like this

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

这篇关于您如何在Google Apps插件脚本中使用外部javascript库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆