Top Banner
ScriptDB as a deaddrop box Passing data between VBA, GAS and JS
17

Using script db as a deaddrop to pass data between GAS, JS and Excel

Jan 21, 2015

Download

Technology

Bruce McPherson

Here's how to use Google Apps Script ScriptDB as an automated short term data relay for feedback between web sites, Excel and Google Apps Script.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Using script db as a deaddrop to pass data between GAS, JS and Excel

ScriptDB as a

deaddrop boxPassing data between VBA, GAS and JS

Page 2: Using script db as a deaddrop to pass data between GAS, JS and Excel

what is this about?

• A short term message store to pass data

between VBA, GAS and JavaScript

• Uses the scriptDB API for each of these

platforms

• Segregate conversations by the use of keys

• Uses cookies and registry for authentication

purposes

Page 3: Using script db as a deaddrop to pass data between GAS, JS and Excel

scriptDB API COMPONENTS

VBA

/JS API

Your

code Your GAS

webapp

Handler (s)

Your

codeYour

VBA/JS

code

Your

ScriptDB

dispatcher

GAS

Library

API

Your

codeYour

codeMultiple

ScriptDB

Registry

/cookie

/local

storage

oauth2 /

rest

encrypted

oauth2

credentials

simple

noSql

Same as using scriptDB as a nosql database

Page 4: Using script db as a deaddrop to pass data between GAS, JS and Excel

Short lifetime data

Deaddrop components

Message

handler

Your

codeYour

codeYour

VBA

code

deaddrop

Registry

JS/GAS/VBA

scriptDB API

environment

Your

codeYour

codeYour

JS code

ScriptDB

credentials

Scriptdb

cookie/

local

storage

simple

noSql

Short lifetime data

Short lifetime data

Your messageid

Page 5: Using script db as a deaddrop to pass data between GAS, JS and Excel

DeadDrop class

Related message concepts can be identified by a class

name of your choosing. You can have as many classes

as you want

Each conversation creates an instance of a class with a

unique key

A class instance has a strict lifetime of 24 hours

The instance expires and is deleted 24 hours after

creation

Page 6: Using script db as a deaddrop to pass data between GAS, JS and Excel

deadDrop handle

You get a handle to a deadDrop class like this

The latest instance for a classgetDeadDrop(yourClass, scriptDBEntry)

A new instance for a classgetDeadDrop(yourClass, scriptDBEntry,true)

An existing specific class instancegetDeadDrop(yourClass, scriptDBEntry,,deadDropKey)

Page 7: Using script db as a deaddrop to pass data between GAS, JS and Excel

deadDrop class EntryA deaddrop class entry is stored in the registry or as a cookie.

A class entry knows which scriptDBEntry to use

Before deadDrop can be used, it must be registered to use the

scriptDBEntry. This is a one time operation, shared with other

scriptDB usage.

The scriptDB entry knows how to access scriptDB, which library and

which handler to use, and is subject to permissions allowed by the

scriptDB entry

Page 8: Using script db as a deaddrop to pass data between GAS, JS and Excel

scriptDB registration - VBAPrivate Sub firstTimescriptdbMessages()

Dim scriptdbCom As cScriptDbCom

Set scriptdbCom = New cScriptDbCom

With scriptdbCom.init(, _

"messages", _

, _

"messagesKey", _

"xliberation", _

False, _

"scriptDBMessages", _

False, _

"https://script.google.com/macros/s/AKfycbzvnq2IZu3JpngnuVxfnPAZYPooVBTULkUyiLFnItfvRxY0NrI/exec")

.tearDown

End With

End Sub

Page 9: Using script db as a deaddrop to pass data between GAS, JS and Excel

scriptDB registration - JSfunction firstTimeMessages() {

// full access to scriptDBPrimer database, oAuth not required - uses scriptdbrequesthandler

new cScriptDbCom().setScriptCredentials( {

endPoint : gasHandlerEndPoints.scriptdbrequesthandler,

restAPIKey : 'messagesKey',

scopeEntry : 'rest',

credentialsEntry: 'messages',

clientKey:'xliberation',

library: 'scriptDBMessages',

needDebug: false,

needOauth: false } );

}

Page 10: Using script db as a deaddrop to pass data between GAS, JS and Excel

Public deaddrop

For testing you may use the credentials for the

‘messages’ scriptDB’ entry

Remember that each class instance expires

24 hours after creation

If you find all this useful, you should create

your own scriptDb environment

Page 11: Using script db as a deaddrop to pass data between GAS, JS and Excel

Usage example

In googleMapping.xlsm, I create a web page

that plots Excel data on a Google Map.

The use case is to retrieve data interactively

through the map, and update the source

Excel sheet.

Page 12: Using script db as a deaddrop to pass data between GAS, JS and Excel

How it’s done

In VBA, when the mapping app is created, a

new deaddrop conversation is initiated, and

the key is passed to the app.

With getdeaddrop(yourClass, "messages", True)

With .scriptDb

Set job = JSONParse("{'subject':'" & subject & "','info':'xliberation public data for testing'}")

.createObject(job).flush

job.tearDown

End With

addDeadDrop = .key

End with

Page 13: Using script db as a deaddrop to pass data between GAS, JS and Excel

User interaction// Infobox is instrumented to collect data and write to deaddrop using scriptDB API.

// capture change event - what we'll do here is to update dead drop

mcpherAddEvent(ci, "change", function(e) {

// we have a change - record the update via the dead drop

if (pDb) {

pDb.createObject ({uniqueId: cj.uniqueId, type:'comment', title:cj.title, cj:cj.childIndex,

comments:e.srcElement.value});

// empty batch - we'll do it immediately - it'll happen asynch anyway

pDb.finalFlush()

.done(function(data) { // nothing to do

})

.fail(function(data){

alert ("failed to flush to deaddrop");

});

}

}, false, true);

The deadrop key is passed to the web

app and we get a scriptDb handle for it

pDb = getScriptDb(pParams.deaddrop,"messages");

Page 14: Using script db as a deaddrop to pass data between GAS, JS and Excel

Clearing The logclass key registered processed

googleMapping googleMapping395633308428428 2/4/2014 11:07 2/4/2014 12:18

googleMapping googleMapping395937493918511 2/4/2014 11:58 2/4/2014 12:18

googleMapping googleMapping397080577155843 2/4/2014 15:08

Each time a web app is created, this log is updated. Later processing retrieves any data from the deaddrop for each key and

updates the original spreadsheet with the data from the deadDrop messsage, marks this as processed and deletes the

messages from the deaddrop

With getdeaddrop(dr.cell("class").toString, "messages", False, dr.cell("key").toString)

' now we can get all the message data for this

With .scriptDb

.getObjectsByQuery

Set data = .jObject.child("results")

Set subject = data.find("subject")

With ds.load(subject.toString)

…..etc….

Page 15: Using script db as a deaddrop to pass data between GAS, JS and Excel

The results

Ive used the {subject:xxx} to

determine the original sheet, the

{uniqueId:xxx} and the

{comments:xxxx} to update the

comments field in the original sheet

with data collected by the

generated web app.

Page 16: Using script db as a deaddrop to pass data between GAS, JS and Excel

How to try itDownload googleMapping.xlsm

Set to enable deaddrop in the geoCodingParameters sheet

Register your computer to the test drop box locally (on the venues

tab)

Plot on Google Map, click on some places, add data

Process the deadDrop Log

Check the comments column on the venues sheet

use custom deaddrop test custom code to use

enable deaddrop TRUE if true, then deaddrop conversation will be enabled and lgged - the custom code selected should match this

Page 17: Using script db as a deaddrop to pass data between GAS, JS and Excel

Next steps

Read all about the scriptDB API

Read more about deadDrop

Contribute to our forum with use case ideas.

Build your own scriptDb environment and

applications