Distribute Google Apps Script and push updates

拈花ヽ惹草 提交于 2019-11-27 07:40:36

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/16997274/distribute-google-apps-script-and-push-updates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!