分发Google Apps脚本并推送更新 [英] Distribute Google Apps Script and push updates

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

问题描述

在我们的组织中,我们使用Google电子表格作为数据输入到我们的内部应用程序。我已经创建了一个Google Apps脚本来帮助修改电子表格。



如您所见,此脚本依赖于电子表格格式,并且仅适用于特定用途我的问题是如何自动将任何更改推送到所有使用它的电子表格实例?




我可以将脚本添加到脚本库中,但我认为它不适合这些类型的脚本,并且如果主副本的任何更改都会推送到客户端,我也无法找到信息。



我看到的另一个选项是创建一个带有脚本的电子表格模板,但同样会推动更改吗?

解决方案

分发特定(有界)脚本的最简单方法的确是创建一个包含脚本的模板电子表格。如果您的脚本更具通用性并且可以被任何人使用,那么该图库是适合的。



现在,没有很好的解决方案可以将更改推送到脚本的副本,无所谓你分发你的脚本,因为最终每个安装是一个独立的副本,没有链接到原始。



这样做最简单但仍然很麻烦是使用库。您将所有逻辑放入库中,并且只将脚本中的样板放入将被复制的样板中。即 onOpen onEdit 函数和存根菜单功能。像这样:

  function onOpen(e){return Lib.onOpen(e); } 
function onEdit(e){return Lib.onEdit(e); }
函数stub1(){return Lib.stub1(); }
函数stub2(){return Lib.stub2(); }
//等等......尽可能多的为你的需要,而不是为未来的增长多付

这个分布式脚本将导入你的主脚本( Lib ),它将拥有所有的逻辑。例如

  function onOpen(e){
e.source.addMenu('Custom菜单',[{name:'做某事',functionName:'stub1'}]);


函数onEdit(e){
; // something
}

函数stub1(){
浏览器.msgBox( '实施例');
}

现在仍然存在这样的问题,当您更新您的Lib时,您或您用户必须进入脚本编辑器中所有具有该脚本的文档并手动更新库版本。



您甚至可以做一些奇怪的事情,使其具有 onOpen 函数检查 ScriptProperty (您可以在不更新版本的情况下手动更改)并通知用户他们需要更新他们的脚本(即进入脚本编辑器并更新库版本)。

开发模式标志,可以在导入库,避免了这种需要,并更新库版本。但只有在运行脚本的用户是导入库的开发人员时才有效。也就是说,如果你有自己以外的用户,你必须与他们分享你的图书馆给予编辑权限。这可能是可以接受的,如果它全部在你的域名或朋友之间。但并不是真正的解决方案。



另一个可能的解决方法是,个人不喜欢很多但可以为你做的伎俩是使用 EVAL 。您可以将逻辑代码保存在Google文档或其他位置,然后将其读取到脚本中,然后将其读取到 eval 它以运行您的功能。您仍然需要在每个脚本上使用一些样板代码,但您的用户无需执行任何操作即可获取更新的代码。您还需要在分布式脚本中将存根呼叫添加到您在Apps Script中使用的所有服务中,只需提示所有授权即可。



Last ,有一些 问题 Apps脚本问题跟踪器与此有关的方式。你应该明星他们接收更新和他们的投票种类。我刚刚为此打开了一个特别是。

In our organization, we use Google spreadsheet as data input to our internal app. I have created a Google Apps Script to facilitate the process of modifying the spreadsheet.

As you see, this script is dependent on the spreadsheet format and serves very specific usage only applicable to our organization.

My question is how to push automatically any changes to all spreadsheet instances that are using it?

I can add the script to the Script Gallery but I think it is not the appropriate place for these types of scripts and also I can't find info if any changes to the master copy will be pushed to the clients.

The other option I see is to create a spreadsheet template with the script inside but, again, will the changes be pushed?

解决方案

The easiest way to distribute an specific (bounded) script is indeed to create a template spreadsheet with the script in it. The Gallery is suitable if your script is more generic and can be used by anyone.

Now, there's no great solution to push changes to script's copies, doesn't matter how you distribute your script, because in the end each "installation" is an independent copy, not linked to the original.

The easiest, but still cumbersome, way of doing this is to use a library. You place all your logic inside a library and only the boilerplate inside the scripts that will be copied. i.e. an onOpen and onEdit functions and stub menu functions. Something like this:

function onOpen(e) { return Lib.onOpen(e); }
function onEdit(e) { return Lib.onEdit(e); }
function stub1() { return Lib.stub1(); }
function stub2() { return Lib.stub2(); }
//etc... as many as you need, than a couple more for future growth

This distributed script will import your master script (the Lib) which will have all the logic. e.g.

function onOpen(e) {
  e.source.addMenu('Custom Menu', [{name:'Do something', functionName:'stub1'}]);
}

function onEdit(e) { 
  ;//something 
}

function stub1() {
  Browser.msgBox('Example');
}

Now, there's still the issue that when you update your Lib, you or your users will have to go inside the script editor of all the documents that have the script and update the library version manually.

You could even do something fancy as having the onOpen function check a ScriptProperty (that you can easily change manually without updating the version) and inform your users that they need to update their script (i.e. getting in the script editor and update the library version).

There's the development mode flag that can be set when importing a library, that avoids this need to go in and update the library version. But it only works when the user running the script is a developer of the imported library. That is, if you have users other than yourself you'll have to share your library with them giving edit permission. This may be acceptable for you, if it's all under your domain or among friends. But is not really a definite solution.

Another possible workaround, that personally don't like much, but may do the trick for you, is to use eval. You can save the "logic" code in a Google Document or somewhere else, and fetch it on the script then eval it to run your functions. You'll still need some boilerplate code on each script, but your users will not need to do anything to get the updated code. You'll also need to add stub calls to all services you use in Apps Script inside the distributed scripts, just so it prompts the users with all the authorization required.

Last, there's some issues opened on Apps Script issue tracker that are related to this in one way or another. You should "star" them to receive updates and kind of vote for them. I have just opened one for this specifically.

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

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