简体   繁体   English

将外部JavaScript导入谷歌应用脚​​本

[英]importing external javascript to google apps script

I am trying to use Trello from a Google Spreadsheet (Google Docs) and am not sure how to import/reference/link the javascript files required to use their library. 我正在尝试使用Google电子表格(Google Docs)中的Trello,我不确定如何导入/引用/链接使用其库所需的javascript文件。 My only other option is using their REST API directly (fine, but I'd rather use their js helper classes). 我唯一的另一个选择是直接使用他们的REST API(很好,但我宁愿使用他们的js助手类)。

This is what Trello needs us to use: 这是Trello需要我们使用的:

 <script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
 <script src="https://api.trello.com/1/client.js?key=substitutewithyourapplicationkey"</script>

How would I import/include these in a Google Apps Script? 如何在Google Apps脚本中导入/包含这些内容?

THANKS!!! 谢谢!!!

基于Cameron Roberts的答案,您可以在appscript UrlFetchApp函数上使用eval()函数。

eval(UrlFetchApp.fetch('http://path.to/external/javascript.js').getContentText());

you actually can . 你真的可以。 In the script project create another new file and simply just paste the JavaScript library copying from the source and save it then start referencing it from other file. 在脚本项目中创建另一个新文件,只需从源中粘贴JavaScript库并保存,然后从其他文件开始引用它。 It is that simple. 就这么简单。

Or you can create another project with the .js lib and publish it and reference that script from the caller project, I wont do that unless that needs to be shared in multiple projects. 或者您可以使用.js lib创建另一个项目并发布它并从调用者项目引用该脚本,除非需要在多个项目中共享,否则我不会这样做。

You cannot use external javascript libraries this way in Google Apps Script. 您无法在Google Apps脚本中以这种方式使用外部JavaScript库。 (You can do so in html files used with the HtmlService . Since so much of Trello is client-side anyway, this may be just what you need.) (您可以在与HtmlService一起使用的html文件中执行此操作 。由于Trello的大部分都是客户端,所以这可能就是您所需要的。)

In server-side apps script, you should be able to access the library code using the technique from this answer . 在服务器端应用程序脚本中,您应该能够使用此答案中的技术访问库代码。 It doesn't say, but I'd imagine that you would put that eval outside of all functions in your script, to make the objects in the library available to the rest of your code. 它没有说,但我想你会将eval放在脚本中的所有函数之外,以使库中的对象可用于其余的代码。

Download them and put them in the script. 下载它们并将它们放入脚本中。 The rest api is easy to use. 其余的api易于使用。 Ive used trello rest from appscript. 我已经在appscript中使用了trello rest。

Yes you can use JavasSript libraries in Google script. 是的,您可以在Google脚本中使用JavasSript库。 Copy all content of the libraries JavaScript and post it in new GS file. 复制库JavaScript的所有内容并将其发布到新的GS文件中。

You can try this 2 options: 您可以尝试以下两个选项:

Hope this helps 希望这可以帮助

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM