Whoa!!! Back the truck up. There s a much simpler approach
I have been researching this a bit over the past few weeks because I m planning to do the same thing for my monthly reports. I don t have the actual code fleshed out yet but I ll add it as I make progress.
In Google Docs there are so many API s and similar terms related to working with docs that things can get a little confusing. If you don t already know, establish in your head the fact that GAS (Google Apps Scripting) and GAE (Google App Engine) are two completely different things. Even though they sound the same thing they re about as similar as Java is to JavaScript.
GAS are the scrips embedded in Google Docs (which will hopefully be importable as stand alone modules in the future) that drive things like validation and dynamic documents but they re a lot more powerful than most suspect (they can do things like modify/update external documents and auto-email responses). Keep in mind that these need to be lightweight because they re run on google s servers. If your script takes to long to finish up it s execution will get cut off prematurely (google around to find the limits). That means you should use vanilla JS only (no frameworks like jQuery) and performance tweaks wherever possible.
GAE, on the other hand, is like a web server (with an available database layer) that lives somewhere in the cloud. It exists as a convenient (and already deployed) middleware layer for businesses/interests to create custom apps to do more heavy lifting. Unfortunately, the external Spreadsheet s API is too limited to accomplish what we re working on by itself so it s a non-option.
Automation using Google Apps Scripting and time-based triggers
This approach should work but requires a slightly hackish approach.
Open up the workbook containing your report sheets. Click on [Tools] -> [Script editor...]. Once there goto [Triggers] -> [Current script s triggers...].
If you don t have any triggers present, add one. Then, under the Events dropdown menu select Time-driven .
Welcome to the world of server-side event handlers. One of the neat features that you get with cloud-based documents is the ability to trigger cron jobs directly within your document. No external middleware necessary.
If you haven t noticed by now there s no trigger for Month timer . This is where it gets hacky. To work around the lack of this feature it s going to require that we fire the trigger on a daily basis and use some JavaScript to match the current date with the previous day s date.
[code will go here]
First, comes the function that gets attached to the time trigger event handler. This block of code just simply parses the date, compares it to the previous date, and stores the value in a hidden sheet (that we use as out persistence layer) for the next day s comparison. If the new-month condition is met then the next block of code runs.
[code will go here]
Yours will obviously differ from mine a bit but the basic concept is:
- Load the SpreadSheet object (not to be confused with a Sheet object)
- Locate the template Sheet object
- Clone the template Sheet giving it an appropriate date-range-based name
In mine, my next step will be to extract data from the month to generate a stacked line graph to report the current status to my higher-ups.
Note: Because of the multi-user collaboration nature of docs, events have to be fired server-side. This creates a big problem for us. Because the event code runs elsewhere if the code errors, we don t get any feedback from our browser. The only solution to this is to setup a notification on the trigger to immediately email you when the script faults.
Update: While researching this, I found another cool technique. If I can manage to get this working without any bugs I might try to invoke the trigger using a date marked on Google Calendar.