15
Parse.com – with GAS HOW TO USE A CLOUD BASED NOSQL DATABASE WITH GOOGLE APPS SCRIPT EXCEL LIBERATION

parse.com - how to use a noSQL data base for Google Apps Script

Embed Size (px)

DESCRIPTION

From Excel Liberation, here's free shared Google Apps Scripts that provides a wrapper for the parse.com noSQL rest API. Using a cloud based database means that you can now share data between Excel and other platforms such as Android and IOS.

Citation preview

Page 1: parse.com - how to use a noSQL data base for Google Apps Script

Parse.com – with GASHOW TO USE A CLOUD BASED NOSQL DATABASE WITH GOOGLE APPS SCRIPT

EXCEL LIBERATION

Page 2: parse.com - how to use a noSQL data base for Google Apps Script

What is parse.com? A noSQL database

SDK for multiple platforms, including IOS and Android

Cloud based script execution

Built in analytics and dashboards

Role based security

Free for most of us

Easy to use and to get started with

Best for smaller datasets

Read more at parse.com

Page 3: parse.com - how to use a noSQL data base for Google Apps Script

Why use parse.com with GAS? Google Apps Script already has its own noSQL database – scriptDB – fine for staying inside GAS

Using Parse allows GAS to easily share data with other platforms and across workbooks

Is easier to get started with than many other noSQL databases

There is a restAPI that’s pretty easy to implement a GAS wrapper class for

There’s already a Parse VBA API – this one is virtually the same. You can write code in one and copy to the other with only minor language syntax changes.

You can use oAuth2 if you want, but this also adds parse.com authentication

Parse.com is more table like in structure. ScriptDB is more free form. Both approaches have advantages

Because we can

Page 4: parse.com - how to use a noSQL data base for Google Apps Script

Authentication using GAS API

Encrypt parse.com restAPI and

application ID Keys

Store in User Properties

Once only per user

Get from User Properties

Decrypt restAPI and application

ID keys

Access Parse.com

Subsequent accesses from any Google Apps Script

Avoids the problem of needing keys in every Script

If you want you can add oAuth2 to further control access

Page 5: parse.com - how to use a noSQL data base for Google Apps Script

Code for Authentication First time to register and encrypt credentials for this user

function firstTimeParseCom () { // run this once for each user/scope combination parseCom.setRegistryPackage ( "parse","default",{restAPIKey: "your rest API key", applicationID:"your application id"});}

Thereafter from any script executed as this user

var parseCom = getParsed("VBAParseCustomers");

A Parse Class is like a Table

Page 6: parse.com - how to use a noSQL data base for Google Apps Script

Code for a Queryfunction testparsequery () { // get a number of items that match a query by example var w = getParsed("VBAParseData").getObjectsByQuery( {customerid:1},{order:'-value'}); //test if it worked, and do something with the results if (w.isOk() ){ Logger.log( "all is ok"+ JSON.stringify(w.jObject())); } else { throw( "failed to do query:" + w.browser().url() + ":" + w.browser().status() + ":" + w.browser().text()); }}

Or as a one liner

JSON.stringify(getParsed("VBAParseData").getObjectsByQuery( {customerid:1},{order:'-value'}).jObject());All methods are chainable

Queries are by example. Default is to get all objects in the class

Page 7: parse.com - how to use a noSQL data base for Google Apps Script

Get by objectIDfunction testGetItemByUniqueId () { // get a handle for this class var parseCom = getParsed("VBAParseCustomers"); // use a valid unique ID to get the data if (parseCom.getObjectById("VbzHLEte62").isOk()) { Logger.log (JSON.stringify(parseCom.jObject())); } else { throw ("failed to get object:" + parseCom.browser().url() + ":" + parseCom.browser().status() + ":" + parseCom.browser().text()); }}

Or as a one liner

JSON.stringify(getParsed("VBAParseCustomers").getObjectById(("VbzHLEte62").jObject());All methods are chainable

Each parse object (like a row) gets assigned a unique ID

Page 8: parse.com - how to use a noSQL data base for Google Apps Script

JSON.stringifiable object is returned from every operation

{ "address":"584-5478 Et Road", "city":"Hastings", "company":"Eu Accumsan Sed Inc.", "coordinates":"38.2264, 75.04849", "country":"Comoros", "customerid":100, "email":"[email protected]", "name":"Contreras", "region":"NI", "zip":"12396", "createdAt":"2013-11-26T14:36:40.517Z", "updatedAt":"2013-11-26T14:36:40.517Z", "objectId":"SmnyjZKs9m" }

Results are in the .jObect property of cParseCom

Page 9: parse.com - how to use a noSQL data base for Google Apps Script

Deleting objectsgetParsed(‘a parse class’).batch().deleteObjects()

Or just some

getParsed(‘aclass’).batch().deleteObjects( {customerID:1});

Delete operations can be ‘batched’

.deleteObjects will delete all objects (rows) that match its query.

Page 10: parse.com - how to use a noSQL data base for Google Apps Script

Creating objectsgetParsed(“aclass”).batch().createObject(job)

Delete first, if you don’t want a new object to be created

getParsed(“aclass”). getObjectsByQuery(job).batch().delete().createObject(job)

Create operations can be ‘batched’

.createObjects will create a new class if it doesn’t exist

Page 11: parse.com - how to use a noSQL data base for Google Apps Script

Updating objects function testparseUpdate () { // get some items by query and change the scheme name to something else var w = getParsed("VBAParseData").batch(true).updateObjects({customerid:39}, {customerid:1}).batch(false); if (w.isOk() ){ Logger.log( "all is ok"+ JSON.stringify(w.jObject())); } else { throw( "failed to do query:" + w.browser().url() + ":" + w.browser().status() + ":" + w.browser().text()); }}

Or as a one liner

JSON.stringify(getParsed("VBAParseData").batch(true).updateObjects({customerid:39}, {customerid:1}).batch(false).jObject());

Update operations can be ‘batched’

.createObjects will create a new class if it doesn’t exist

Page 12: parse.com - how to use a noSQL data base for Google Apps Script

Counting objects in a classLogger.log (getParsed("VBAParseCustomers").count({country:"Libya"}));

Or total in class

Logger.log (getParsed("VBAParseData").count());

.count() will return the total number of records in the class or that match a query if one is given

Page 13: parse.com - how to use a noSQL data base for Google Apps Script

Copying a sheet to a parse class // copy two sheets to parse.comfunction testPopulate() { populateFromName ("gasParseCustomers"); populateFromName ("gasParseData");}

Code for populateFromName

function populateFromName (sheetName) { parseCom.populateFromSheetValues(SpreadsheetApp.getActiveSpreadsheet() .getSheetByName(sheetName).getDataRange().getValues(), sheetName);}

Call shared scripts from a workbook with the data

Page 14: parse.com - how to use a noSQL data base for Google Apps Script

Typical setup

cParseCom library – shared by everyone, owned by

Excel Liberation

Create your own parseCom library – shared in between

your scripts

Your spreadsheets

Your scripts

Copy parseCom library code from here

Reference this project key in your parseCom library

MMaKU0wHrNShUwFypY3nM8iz3TLx7pV4j

Reference your parseCom library in each of your scripts

UserPropertiesYour encrypted

parse.com credentials are

stored here

Page 15: parse.com - how to use a noSQL data base for Google Apps Script

Getting started Register with parse.com, create an application and get an applicationID and a restAPI key

Set up your parseCom script with this code

Add a reference to cParseCom at MMaKU0wHrNShUwFypY3nM8iz3TLx7pV4j

Create a first time script, add a reference to your parseCom library and run thisfunction firstTimeParseCom () { // run this once for each user/scope combination parseCom.setRegistryPackage ( "parse","default",{restAPIKey: "your rest API key", applicationID:"your application id"});}

Run some of the examples in parseCom

Create some scripts that reference your parseCom library

Get some testData (there’s some here), reference your parseCom, and load some data from Worksheets

Read about how all this works and learn how to do more complex operations at Excel Liberation

For help and more information visit me on GooglePlus, join our forum, follow the blog or follow me on twitter .