我如何在电子表格中调用独立脚本? [英] How can I invoke my standalone script in my spreadsheet?

查看:122
本文介绍了我如何在电子表格中调用独立脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经从Google云端硬盘创建了一个独立的Apps脚本,但是当我尝试从Google电子表格中访问它时,我看不到访问该脚本的方式(即使创建该脚本时,我也将其创建为电子表格脚本)。 在电子表格中,工具 - >脚本管理器...菜单项不显示我的脚本,工具 - >脚本编辑器也不显示。 。后者有一个'最近的项目'部分,但它没有列出我新创建的脚本,也没有列出它的相关项目。



如果我从里面创建一个新脚本电子表格(即.Tools-> Script editor ...)并从独立脚本中剪切并粘贴代码,它工作正常。但是,这只能在单个电子表格中使用 - 要从另一个电子表格中使用它,我必须剪切并粘贴/再次通过auth。



有没有方法来做到这一点,而不会将脚本发布到画廊,我认为它是公开的(我没有让它公开的问题,但似乎必须有更好的方法)?该脚本的此处 - 这是世界上可读的链接。它在Google电子表格中添加了一个菜单,让您可以在BigQuery中运行查询并将结果转储到电子表格中。

解决方案

按照这里的指示,与乔丹一起工作:



https://developers.google.com/apps-script/guide_libraries




  • 首先,他必须分享他的独立剧本与我有关,所以我已经阅读过它(可能将剧本设置为公开阅读也可以)。
  • 然后他告诉我他的项目关键。
  • 我将他的项目键并插入到查找库中的管理库菜单中。
  • 为此,乔丹不得不出口他的图书馆的一个版本 - 所以第三方可以依靠稳定的代码并按自己的节奏进行升级。
  • 由于Jordan的脚本自动运行onOpen(),我的代码必须定义一个函数onOpen(),它调用onOpen()的脚本。为此,图书馆经理给了我一个导入图书馆的标识符。



一旦库被导入并且库管理器给你一个库的标识符:

  function onOpen(){
GivenLibraryIdentifier.onOpen();
}


I have created a standalone Apps Script from Google Drive, but when I try to access it from a Google Spreadsheet, I don't see a way to access the script (even though when I created the script, I created it as a spreadsheet script).

In the spreadsheet, the "Tools->Script manager..." menu item doesn't show my script, nor does the "Tools->Script editor...". The latter has a section for 'recent projects', but it doesn't list my newly created script, nor its associated project.

If I create a new script from inside the spreadsheet (i.e .Tools->Script editor...) and cut and paste the code from the standalone script, it works fine. However, this can only be used in the single spreadsheet -- to use it from another one, I have to cut and paste / go through auth again.

Is there a way to do this without publishing the script to the gallery, which I assume makes it public (I don't have a problem making it public, but it seems like there must be a better way)? The script is here -- it is world readable with the link. It adds a menu to a google spreadsheet that lets you run a query in BigQuery and dumps the results into your spreadsheet.

解决方案

I worked this out with Jordan, following the instructions here:

https://developers.google.com/apps-script/guide_libraries

  • First, he had to share his standalone script with me, so I had read access to it (probably setting the script as public read would work too).
  • Then he told me his project key.
  • I took his project key and inserted it in "Find a Library", in the "Manage Libraries" menu.
  • For this to work, Jordan had to export "a version" of his library - so third parties can rely on stable code and upgrade at their own rhythm.
  • As Jordan's script runs automatically on onOpen(), my code had to define a function onOpen() that called his script onOpen(). To do this, the library manager gave me an identifier for the imported library.

Sample code once the library is imported and the library manager gives you an identifier for the library:

   function onOpen() {
     GivenLibraryIdentifier.onOpen();
   }

这篇关于我如何在电子表格中调用独立脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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