7
votes

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?

2

2 Answers

11
votes

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.

3
votes

(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.

  1. 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).
  2. 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.
  3. "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.