Stefan van Aalst
Stefan van Aalst

Reputation: 778

Is it possible to have one script for multiple spreadsheets?

I have one master spreadsheet and a number of copies. This master spreadsheet uses some scripting.

Is it possible to link all the copies of this master spreadsheet to the same script as in the master spreadsheet?

Objective:

Upvotes: 21

Views: 13292

Answers (6)

gogi
gogi

Reputation: 31

best way is to publish as add-on, then install the add-on, it will appears in every spreadsheet you open. and you can publish as private, which only seen by yourself.

Upvotes: 3

antoine
antoine

Reputation: 2106

amleczko is right: you should use the new library feature in Google Apps script.

However, as of today, you won't be able to do exactly what you want (using the same script for several spreadsheets). What you can do instead is save a version of your script (Files > Manage Versions...), in order to create a library. Then, import this library in the other spreadsheets (Resources > Manage Libraries...). Switch on the "development mode" so every change made do the library will immediately take affect in the spreadsheets using this library. Otherwise, you will have to save a new version of the library for every change, and manually update the version number of the library in every spreadsheets using it.

The problem is, you need to write a script in every spreadsheets using your library, with skeleton functions like this:

function doSomething(){
   myLibrary.doSomething();
} 

Upvotes: 18

fooby
fooby

Reputation: 849

I have solved this problem when using a script which auto generates spreadsheets.

Typically, I will add a sheet to any spreadsheet with a script called "Info." I'll use that to store information that it important to the script. In my script which auto generates more spreadsheets, I keep track of the ID of the created sheet. This way, I can then quickly call up all of the "linked" sheets, and interact with them with using the same script. It might even be worth writing the script in one sheet, and keeping it totally separate from your Master sheet or it's children.

Take a look at this function, it might give you some ideas.

SpreadsheetApp.openById(id)

Upvotes: 0

Cartman
Cartman

Reputation: 518

The solution I put in place in this context was to have a Google Site, where the Master Script is embedded, and where the Spreadsheet is embedded too

Then, the script, refering to a dedicated spreadsheet, looks for the Google Site Page's name, looks in the Master spreadsheet and get the ID of the spreadsheet which is embedded in the Page.

Upvotes: 0

Henrique G. Abreu
Henrique G. Abreu

Reputation: 17752

It's not possible in this way that you're thinking. At least, not yet (see issue 40).

But, depending on your script usage, you may connect them "the hard way" or even better, use only one script. The script on the master spreadsheet can open the other spreadsheet files and do its job "remotely". It's not required that script to be hosted on a spreadsheet to interact with it (read/write on it). You only need a script hosted on the spreadsheet if you're going to use spreadsheet events triggers i.e. on-open, on-edit and on-form-submit.

Maybe you can develop a nice UI for the script on the master sheet and publish it as service. Then only have a link on the copies to access the same UI on a different browser tab. Adding parameters to the link the script UI can even adapt to the particular spreadsheet that is "triggering" it.

Well, that's all I can imagine now. But, unfortunately, there's some use cases that just don't fit this nice "workarounds". For those, one can only star issue 40 (to kind of vote and keep track of updates) and hope it's developed soon.

Upvotes: 1

Related Questions