How to get started with cParseCom and Google Apps Script

Note to my regular readers: As you can see, this is a post in English. It targeted at other people writing Google scripts – not necessarily teachers.

Since I became teacher I’ve been writing a few Google scripts, and recently I’ve been more and more annoyed by data storage limitations. I just became happier, after I managed to use parse.com as a data backend to Google application scripts (”GAS”). Here’s an explanation of how I did, in case anyone else have similar problems.

Thanks goes to Bruce McPherson for his blog post that got me started on this track – and of course for writing the cParseCom library. And thanks to Parse for providing a nice service for free.

What’s the problem?

I have need for storing quite a bit of data in scripts executed as the acting user (not the script owner), and this rules out some of the usual data storage choices:

  • Storing data in a Google spreadsheet is no good, since the spreadsheet would have to be editable by anyone. (It is also a slow solution.)
  • Storing data in user properties is no good, since it has a limitation of 8–9 kB and is also only accessible to the acting user. (It would also bloat the user properties.) The script properties have similar limitations, and both of them are really meant to store small and simple data.
  • Storing data using ScriptDb in GAS is no good, since it is being deprecated. It is also quite slow.

I looked around for a free and hopefully easy-to-use data storing service, and eventually settled for parse.com. (I also tried MongoDb, but couldn’t get it to work for free.) After quite a bit of trial-and-error (since I’m not really a code head), I managed to get a Google script to store and fetch data on parse.com.

How to make Google scripts use parse.com as data backend

Set up things on parse.com

  1. First of all, you need an account on parse.com. It’s free and quick.
  2. When you have an account, you need to create an app.
  3. Each app has its own access codes. You’ll need to click in to settings and keys and write down the application ID and REST API key.

Install cParseCom library

To make things a ton easier, you should use the cParseCom library.

  1. Create a new Google script, or open an existing one.
  2. In the resources menu, select libraries and enter the ID MMaKU0wHrNShUwFypY3nM8iz3TLx7pV4j to locate and add the cParseCom library. (I used version 9, but if you find a newer version you should probably use that instead.)

The code for accessing parse.com

It took me some time to find out how to use the cParseCom library. These are my conclusions.

  1. You need an object (called ‘package’ by the library) where your parse.com keys are stored. It should have the following structure:
     var package = {'applicationID' : 'your-application-id', 'restAPIKey' : 'your-rest-api-key'};

    It is a good idea to have these credentials stored in a script property or something, so they won’t be readable in the actual code (if you want to share your script).

  2. All data at parse.com are stored in classes – which for me translates into different databases (or perhaps different types of objects). To get access to a selected class, you do something like this:
    var p = cParseCom.getParsed('className', package);

    (The p object is used in the examples below, but you can of course use any name you want.)

  3. You can now store and retrieve data using the p object:
    p.createObject({'key1' : 'value1', 'key2' : 'value2'});
    var searchResults = p.getObjectsByQuery({'key1' : 'value1'}).jObject();

    (Note the jObject method used when querying for objects – if you don’t use it you will get the full p object, not the result of the search.)

  4. There are more things you can do with cParseCom, such as batching jobs, updating existing entries and more. (Read the online docs for clues!) This basic example will hopefully help you get started.

Since storing and retrieving is done through a REST service, it makes sense to have as few read/writes as possible – but the size should no longer be an issue.

Happy coding!

Annonser

Kommentera

Fyll i dina uppgifter nedan eller klicka på en ikon för att logga in:

WordPress.com Logo

Du kommenterar med ditt WordPress.com-konto. Logga ut / Ändra )

Twitter-bild

Du kommenterar med ditt Twitter-konto. Logga ut / Ändra )

Facebook-foto

Du kommenterar med ditt Facebook-konto. Logga ut / Ändra )

Google+ photo

Du kommenterar med ditt Google+-konto. Logga ut / Ändra )

Ansluter till %s