Using script db as a deaddrop to pass data between GAS, JS and Excel
-
Upload
bruce-mcpherson -
Category
Technology
-
view
3.462 -
download
1
description
Transcript of 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
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
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
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
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
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)
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
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
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 } );
}
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
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.
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
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");
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….
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.
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
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