使用Google Apps脚本库 [英] Using Google Apps Script Libraries

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

问题描述

我已经阅读了有关管理和创建库的所有Google文档,但我仍然不知道它们是否适合解决我要解决的问题。

I have read all Google documentation on managing and creating libraries, yet I still do not know if they are an appropriate option for the problem I am trying to solve.

我知道如何保存独立脚本的版本。我知道如何通过脚本编辑器将库添加到电子表格。但我不很简单地了解如何在新电子表格中触发库脚本。

I know how to save a version of a standalone script. I know how to add the library to a spreadsheet via the script editor. But I don't understand, very simply, how to trigger the library script within the new spreadsheet.

我有一个电子表格,可以用作组织中经常复制的模板。该模板包含一个脚本(onOpen),该脚本访问单独电子表格(主数据库)上的数据,并在名为 admin的选项卡上设置这些值。理想的结果是在模板表中保留主数据库的副本(以及模板表的每个后续副本)。此时,模板表有成千上万的副本,每个副本都运行相同的脚本。

I have a spreadsheet that serves as an often-copied template within my organization. The template contains a script that (onOpen) accesses data on a separate spreadsheet (a master database) and sets those values on a tab called "admin." The desired result is to have a copy of the master database living within the template sheet (and every subsequent copy of the template sheet). At this point, there are thousands of copies of the template sheet, each running that same script.

每当我不得不更改脚本时,都必须在数千个之内进行更改的床单。我可以改用图书馆吗?我希望能够在库中创建脚本的新版本,并使连接到该库的所有工作表都能体验到这一变化。我知道该库需要处于开发模式(每张纸内)才能执行此操作。我也知道,为了进行此切换,我可能仍将不得不进入每张表以添加库。我只是希望这将是我最后一次要做这样繁琐的任务。

Whenever I have to change the script, I have to change it within thousands of sheets. Can I use a library instead? I'd like to be able to create a new version of the script in the library and have all sheets connected to that library experience the change. I understand that the library needs to be in development mode (within each sheet) to do this. I also understand that in order to make this switch, I will probably still have to go into each sheet to add the library. I'm just hoping it will be the last time I have to do such a tedious task.

任何建议或指向可靠信息的链接都会受到赞赏。

Any advice or links to solid info is appreciated.

推荐答案

除了制作附加组件(已经包含在另一个答案中)之外,我还将回答您的图书馆问题。他们会为您工作。您所缺少的是连接部分。

besides making an add-on (already covered in another answer) I will answer your libraries question. They will work for you. What you are missing is the "connect" part.

为此,您想从onOpen触发库代码。库中的onOpen不够,并且无法被应用脚本检测到。取而代之的是,电子表格的每个脚本都需要一个onOpen(e)来调用yourlibrary.onOpen(e)。

For this you want to trigger the library code from say, onOpen. The onOpen in the library is not enough and not detected by apps script. Instead each of your spreadsheet's script needs an onOpen(e) which just calls yourlibrary.onOpen(e).

因为这些钩子调用很少更改,尤其是一旦稳定您的库api,并在开发模式下使用它,您就可以只修改库。

since those "hook" calls rarely change, specially once you stabilize your library api, and using it in "development" mode will let you modify just the library.

每当这些钩子需要更改时(例如html的回调) GUI需要一个新参数),您需要更新所有电子表格。为了避免这种情况,请让您的所有回调都接收一个json对象而不是多个参数。

whenever one of those hooks needs to change (say a callback from an html GUI needs a new parameter) you need to update all the spreadsheets. to avoid this, make all your callbacks receive a single json object instead of multiple parameters.

这篇关于使用Google Apps脚本库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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