How should I reuse functions on a number of different Google Spreadsheets? - google-apps-script

I have now written a number of different functions in Google Apps Script that I am using on a growing number of Google Spreadsheets.
Every time I make a small edit to one of these functions I then have to go into each spreadsheet and make the same correction.
Is there a way to "import" the functions to each spreadsheet so that I only have to edit the one "master" function and it is then updated on every spreadsheet?

Google has just release a new feature called Libraries, it is an elegant way to do what you're asking for. Here is the documentation, read it attentively because it's a multi-step process.

See this page for how to create and share functions in your sheets using Google Apps Scripts. The scripts are written in javascript and there are some limitations on how they can be used.
https://developers.google.com/apps-script/guide_writing_scripts#CustomFunctions

Related

Google Apps Script: Get the ID of script bound to a spreadsheet

I want to write a Google Apps Script to generate a list of the IDs of the scripts bound to certain spreadsheets.
I cannot find a way to programmatically, via GAS, get the ID of a bound script.
If I have a spreadsheet object, I have methods like getId to get the ID of the spreadsheet, but not the bounded script.
Using the DriveApp, I can find standalone scripts using DriveApp.getFilesByType('application/vnd.google-apps.script') but not bound ones!
Any suggestions?
Unfortunately (by my own research) this is not currently possible. I have been wanting to programatically identify and access bound scripts for several years, I look into it periodically, I've always come up empty handed.
Haven't found any Google documentation explicitly on the topic, still hoping this hole gets filled eventually.

Something like personal.xlsb in google sheets

I need use some functions in many sheets on Google Drive. Several users will use the functions of this script.
In excel I have got personal.xlsb with macros to all files.
Have You any solutions to this problem?
You may check in this documentation the list of classes and methods you may use in Google Apps Script connected in your Google Sheets. The Spreadsheet Service allows scripts to create, access, and modify Google Sheets files. See also the guide to storing data in spreadsheets. Here's the documentation on how Apps Script can interact with Google Sheets.
There's also a page about Google Sheets Macros that lets you record macros that duplicate a specific series of UI interactions that you define.
Hope this helps!

How can I add a Google apps script to a spreadsheet created using the API?

After reading up a lot on the Google Spreadsheet API I have come to the conclusion that formatting (such as merging cells, changing fonts etc) is only available throught the Apps scripts.
Since we need to create and fill the spreadsheets with data programatically using Java on the back-end I guess I need to somehow either;
link the new sheet to a Apps script that trigger on-load or
create a Apps script that creates the spreadsheet for me.
Anyone knows?
If you want to just "create" the spreadsheet, you don't need a script to load whenever it spreadsheet is opened. It's probably easier to develop a script that runs once and create the spreadsheet for you.
Another tip is to have a template file that you can copy with most of the formatting (if not all) already there. Possibly pending just little things that are related to the real data the new spreadsheet will have.
Edit to answer the question in the title.
No, you can not add a script to an existing spreadsheet programatically, only manually. What you can do is previously set up a template spreadsheet with a script in it and create new spreadsheets by copying this template.
(answering the comment)
You can run a script programatically, but not upload it. To run a script you can deploy it as a web-app and call its url with either a http get or post (will call its doGet or doPost functions, that you must have declared). Also, you could set this script to run on form submit of any spreadsheet-form and just submit a set of answers to this form. At last (that I can think of now) you could just add the script as a library in another Apps Script and call it directly.
(Aug 2016) There is no way programmatic way to link a Google Sheet and Apps Script code other than manually. Based on what it seems you want ("create and fill the spreadsheets with data programatically using Java"), you can now do it without Apps Script.
TL;DR: Above, #Henrique has answered multiple questions and even questions that weren't asked! The good news is that today, we have more answers representing alternate possible solutions to what you're seeking.
It's now possible to "upload" Apps Script code programmatically with the
import/export system, say with Eclipse since you're a Java developer (2013 announcement).
I agree with Henrique's suggestion that if you create a spreadsheet
template, i.e., Excel file, you can use the Google Drive API to
programmatically import/create identical Google Sheets with all your
desired formatting.
"Formatting (such as merging cells, changing
fonts etc)" can now be done outside of Apps Script, as there is a
"new" Google Sheets API v4 (not GData).
In order to use the new API, you need to get the Google APIs Client Library for Java and use the latest Sheets API, which is much more powerful and flexible than any previous API. Here's one code sample to help get you started. If you're not "allergic" to Python, I also made a video with a different, slightly longer example introducing the new API and gave a deeper dive into its code via a blogpost that you can learn from.
Note the v4 API allows you to create spreadsheets & sheets, upload & download data, as well as, in the general sense, programmatically access a Sheet as if you were using the user interface (create frozen rows, perform cell formatting, resizing rows/columns, adding pivot tables, creating charts, etc.), but to perform file-level access such as uploads & downloads, imports & exports (same as uploads & downloads but conversion to/from Google Apps formats), you would use the Drive API instead.

How to publish custom function for Google Spreadsheets developed using Google Script

Please forgive me if I sound naive, I did read some tutorials on Google Script site.
I recently became aware of extending Google Spreadsheets functionality with custom functions using Google Script.
I understand the Script is tied to a particular Spreadsheet that one opens in Google Docs and using Tools menu to insert custom functions code. The Spreadsheet works like a container for custom functions code
The issue that I am having trouble understanding is how such functions are published and how do the end users discover availability of custom functions so that they can insert those custom functions in the spreadsheets they create
And is there a way to put together a collection of custom functions to work like a Functions Library or do we have to create individual functions and publish each one
As you've mentioned, a custom function's availability is restricted to the spreadsheet under which the custom function was written until recently.
You can now create Libraries of the functions and make them available for other spreadsheets to use by the script ID - More details in the documentation https://developers.google.com/apps-script/guide_libraries
If you publish your script to the Script Gallery it can be discovered and installed by other users. They will have a copy of the script's source installed into their spreadsheet.

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:
changes in the scripting in the master spreadsheet are automatically used by the copies
aka: low maintenance
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();
}
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.
I think this has changed. According to Issue 40 starting from 22 May 2012 there is such a possibility. Please check:
https://developers.google.com/apps-script/guide_libraries
https://developers.google.com/apps-script/guide_versions
http://googleappsdeveloper.blogspot.it/2012/05/introducing-versions-and-libraries-in.html
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.
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.
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)