SlideShare a Scribd company logo
do something useful with
Apps Script in 5 minutes
8. Copy between databases
Bruce McPherson
www.mcpher.com
Snippet objectives
● Open a database using dbabstraction
● Copy to another couple of formats
Libraries used
● database abstraction
● driver sheet
● driver mongoLab
● driver drive
Add libraries to script
● get the id of the sheet you want to copy
● create a script
● open resources
● add references to libraries
MHfCjPQlweartW45xYs6hFai_d-phDA33
Mrckbr9_w7PCphJtOzhzA_Cz3TLx7pV4j
MPAHw_-cHNDxsYAg263J7Fai_d-phDA33
create mongolab account
● If you havent already got a mongolab
account, sign up to a free plan https:
//mongolab.com
● Create a new free deployment
create mongolab api key
● Click on your username
● get an API key and enable data access
write mongo key to properties
● Write and execute one off script to store
mongolab restapikey to properties service
function oneTimeSet () {
PropertiesService.getUserProperties()
.setProperty("mongoLabKeys", JSON.stringify({
"restAPIKey":"h3xxxxxxcz"
}));
}
layout what you are going to do
function copyTheSheet () {
// open the sheet to be copied
// get all the data
// open the database to copy it to
// write it
// check it
}
My test data looks like this
Sheet name is 'customers'
open the sheet to be copied
● use the ID and Sheet name to open.
// open the sheet to be copied
var sheetHandler = cDbAbstraction.DbAbstraction(cDriverSheet, {
dbid:"1EhgZ1-q9tP1u9BSEQPIQiSGozdfjsHn3zB1JfQGbftw",
siloid:"customers"
});
if (!sheetHandler.isHappy()) throw 'could not open sheet';
Get the data on the sheet
● a query with no arguments gets all the data
// get all the data
var result = sheetHandler.query();
if (result.handleCode < 0) throw JSON.stringify(result);
Get handle for mongolab database
● will use a collection called 'customers' in the
database 'dosomethinginfive'
var dbHandler = cDbAbstraction.DbAbstraction(cDriverMongoLab, {
dbid:"dosomethinginfive",
siloid:"customers",
driverob:JSON.parse(PropertiesService.getUserProperties()
.getProperty("mongoLabKeys"))
});
if (!dbHandler.isHappy()) throw 'could not open database';
delete any existing data
● delete all the current data in the db table
// delete any data already there
var dbResult = dbHandler.remove();
if (dbResult.handleCode < 0) throw JSON.stringify(dbResult);
copy the data from the sheet
● copy the data from the sheet
// write it all
var dbResult = dbHandler.save (result.data);
if (dbResult.handleCode < 0) throw JSON.stringify(dbResult);
check all was written
● do a count query on what was written
// check that it was all written
var dbResult = dbHandler.count ();
if (dbResult.data[0].count !== result.data.length)
throw 'data counts dont match';
look at data on mongolab
Homework
Do the same thing, but copy to a Drive file
instead of MongoLab.
Hints.
● You'll need cDriverDrive (Ma__4vH--nQ_FPsuNF1BFuyz3TLx7pV4j)
● There is only one line different from the mongolab version (specify the folder and file name)
// open the database to copy it to
var dbHandler = cDbAbstraction.DbAbstraction(cDriverDrive, {
dbid:"/datahandler/driverdrive/tasks",
siloid:"customers.json"
});
Follow up materials
Take a copy of these slides
Join me on G+, or the G+ community
More on desktop liberation
More on database abstraction
More 5 minute things

More Related Content

PDF
Do something in 5 with apps scripts number 6 - fusion crossfilter
PDF
Do something in 5 with gas 9-copy between databases with oauth2
PDF
Do something in 5 minutes with gas 1-use spreadsheet as database
PDF
Do something in 5 with gas 4- Get your analytics profiles to a spreadsheet
PDF
Do something in 5 with gas 3-simple invoicing app
PDF
Do something useful in Apps Script 5. Get your analytics pageviews to a sprea...
PDF
Do something in 5 with gas 2-graduate to a database
PDF
JavaScript client API for Google Apps Script API primer
Do something in 5 with apps scripts number 6 - fusion crossfilter
Do something in 5 with gas 9-copy between databases with oauth2
Do something in 5 minutes with gas 1-use spreadsheet as database
Do something in 5 with gas 4- Get your analytics profiles to a spreadsheet
Do something in 5 with gas 3-simple invoicing app
Do something useful in Apps Script 5. Get your analytics pageviews to a sprea...
Do something in 5 with gas 2-graduate to a database
JavaScript client API for Google Apps Script API primer

What's hot (20)

PDF
VBA API for scriptDB primer
PPTX
Goa tutorial
PDF
Do something in 5 with gas 7-email log
PPTX
Google apps script database abstraction exposed version
PPTX
Dbabstraction
PDF
Using script db as a deaddrop to pass data between GAS, JS and Excel
KEY
Ajax - a quick introduction
PPTX
Google cloud datastore driver for Google Apps Script DB abstraction
PDF
Lazy evaluation drupal camp moscow 2014
PPTX
Introduction tomongodb
PDF
HeadCouch - CouchDB PHP Client
PPTX
MongoDB: Comparing WiredTiger In-Memory Engine to Redis
PDF
Improving the performance of Odoo deployments
PPTX
Using Cerberus and PySpark to validate semi-structured datasets
PPTX
MongoDB - Sharded Cluster Tutorial
PDF
Fetch data from form
PPTX
MongoDB Chunks - Distribution, Splitting, and Merging
PPTX
MongoDB - Aggregation Pipeline
PDF
[AI04] Scaling Machine Learning to Big Data Using SparkML and SparkR
PPTX
VBA API for scriptDB primer
Goa tutorial
Do something in 5 with gas 7-email log
Google apps script database abstraction exposed version
Dbabstraction
Using script db as a deaddrop to pass data between GAS, JS and Excel
Ajax - a quick introduction
Google cloud datastore driver for Google Apps Script DB abstraction
Lazy evaluation drupal camp moscow 2014
Introduction tomongodb
HeadCouch - CouchDB PHP Client
MongoDB: Comparing WiredTiger In-Memory Engine to Redis
Improving the performance of Odoo deployments
Using Cerberus and PySpark to validate semi-structured datasets
MongoDB - Sharded Cluster Tutorial
Fetch data from form
MongoDB Chunks - Distribution, Splitting, and Merging
MongoDB - Aggregation Pipeline
[AI04] Scaling Machine Learning to Big Data Using SparkML and SparkR
Ad

Similar to Do something in 5 with gas 8-copy between databases (16)

PDF
Remaining Agile with Billions of Documents: Appboy and Creative MongoDB Schemas
PPTX
Html indexed db
PDF
Intro to HTML5 Web Storage
PPTX
Unit 1 NoSQL commands.pptx
ODP
Indexed db
PDF
MongoDB and Schema Design
PDF
Who's afraid of front end databases
PDF
Mongo db improve the performance of your application codemotion2016
PPT
OTA Platform with MongoDB
PDF
NoSQL and JavaScript: a Love Story
PDF
Who's afraid of front end databases?
PDF
Google: Drive, Documents and Apps Script - How to work efficiently and happily
PPTX
IndexedDB - Querying and Performance
PDF
Whos afraid of front end databases?
PDF
The Fine Art of Schema Design in MongoDB: Dos and Don'ts
PDF
Local data storage for mobile apps
Remaining Agile with Billions of Documents: Appboy and Creative MongoDB Schemas
Html indexed db
Intro to HTML5 Web Storage
Unit 1 NoSQL commands.pptx
Indexed db
MongoDB and Schema Design
Who's afraid of front end databases
Mongo db improve the performance of your application codemotion2016
OTA Platform with MongoDB
NoSQL and JavaScript: a Love Story
Who's afraid of front end databases?
Google: Drive, Documents and Apps Script - How to work efficiently and happily
IndexedDB - Querying and Performance
Whos afraid of front end databases?
The Fine Art of Schema Design in MongoDB: Dos and Don'ts
Local data storage for mobile apps
Ad

Recently uploaded (20)

PPTX
Qualitative Qantitative and Mixed Methods.pptx
PPTX
1_Introduction to advance data techniques.pptx
PDF
Lecture1 pattern recognition............
PPTX
Database Infoormation System (DBIS).pptx
PPTX
Business Ppt On Nestle.pptx huunnnhhgfvu
PPTX
The THESIS FINAL-DEFENSE-PRESENTATION.pptx
PPTX
Introduction to Knowledge Engineering Part 1
PDF
Foundation of Data Science unit number two notes
PDF
annual-report-2024-2025 original latest.
PPTX
Business Acumen Training GuidePresentation.pptx
PDF
.pdf is not working space design for the following data for the following dat...
PPT
Reliability_Chapter_ presentation 1221.5784
PPT
Miokarditis (Inflamasi pada Otot Jantung)
PDF
168300704-gasification-ppt.pdfhghhhsjsjhsuxush
PPTX
IBA_Chapter_11_Slides_Final_Accessible.pptx
PDF
Clinical guidelines as a resource for EBP(1).pdf
PDF
Fluorescence-microscope_Botany_detailed content
PPTX
ALIMENTARY AND BILIARY CONDITIONS 3-1.pptx
PPTX
Acceptance and paychological effects of mandatory extra coach I classes.pptx
Qualitative Qantitative and Mixed Methods.pptx
1_Introduction to advance data techniques.pptx
Lecture1 pattern recognition............
Database Infoormation System (DBIS).pptx
Business Ppt On Nestle.pptx huunnnhhgfvu
The THESIS FINAL-DEFENSE-PRESENTATION.pptx
Introduction to Knowledge Engineering Part 1
Foundation of Data Science unit number two notes
annual-report-2024-2025 original latest.
Business Acumen Training GuidePresentation.pptx
.pdf is not working space design for the following data for the following dat...
Reliability_Chapter_ presentation 1221.5784
Miokarditis (Inflamasi pada Otot Jantung)
168300704-gasification-ppt.pdfhghhhsjsjhsuxush
IBA_Chapter_11_Slides_Final_Accessible.pptx
Clinical guidelines as a resource for EBP(1).pdf
Fluorescence-microscope_Botany_detailed content
ALIMENTARY AND BILIARY CONDITIONS 3-1.pptx
Acceptance and paychological effects of mandatory extra coach I classes.pptx

Do something in 5 with gas 8-copy between databases

  • 1. do something useful with Apps Script in 5 minutes 8. Copy between databases Bruce McPherson www.mcpher.com
  • 2. Snippet objectives ● Open a database using dbabstraction ● Copy to another couple of formats Libraries used ● database abstraction ● driver sheet ● driver mongoLab ● driver drive
  • 3. Add libraries to script ● get the id of the sheet you want to copy ● create a script ● open resources ● add references to libraries MHfCjPQlweartW45xYs6hFai_d-phDA33 Mrckbr9_w7PCphJtOzhzA_Cz3TLx7pV4j MPAHw_-cHNDxsYAg263J7Fai_d-phDA33
  • 4. create mongolab account ● If you havent already got a mongolab account, sign up to a free plan https: //mongolab.com ● Create a new free deployment
  • 5. create mongolab api key ● Click on your username ● get an API key and enable data access
  • 6. write mongo key to properties ● Write and execute one off script to store mongolab restapikey to properties service function oneTimeSet () { PropertiesService.getUserProperties() .setProperty("mongoLabKeys", JSON.stringify({ "restAPIKey":"h3xxxxxxcz" })); }
  • 7. layout what you are going to do function copyTheSheet () { // open the sheet to be copied // get all the data // open the database to copy it to // write it // check it }
  • 8. My test data looks like this Sheet name is 'customers'
  • 9. open the sheet to be copied ● use the ID and Sheet name to open. // open the sheet to be copied var sheetHandler = cDbAbstraction.DbAbstraction(cDriverSheet, { dbid:"1EhgZ1-q9tP1u9BSEQPIQiSGozdfjsHn3zB1JfQGbftw", siloid:"customers" }); if (!sheetHandler.isHappy()) throw 'could not open sheet';
  • 10. Get the data on the sheet ● a query with no arguments gets all the data // get all the data var result = sheetHandler.query(); if (result.handleCode < 0) throw JSON.stringify(result);
  • 11. Get handle for mongolab database ● will use a collection called 'customers' in the database 'dosomethinginfive' var dbHandler = cDbAbstraction.DbAbstraction(cDriverMongoLab, { dbid:"dosomethinginfive", siloid:"customers", driverob:JSON.parse(PropertiesService.getUserProperties() .getProperty("mongoLabKeys")) }); if (!dbHandler.isHappy()) throw 'could not open database';
  • 12. delete any existing data ● delete all the current data in the db table // delete any data already there var dbResult = dbHandler.remove(); if (dbResult.handleCode < 0) throw JSON.stringify(dbResult);
  • 13. copy the data from the sheet ● copy the data from the sheet // write it all var dbResult = dbHandler.save (result.data); if (dbResult.handleCode < 0) throw JSON.stringify(dbResult);
  • 14. check all was written ● do a count query on what was written // check that it was all written var dbResult = dbHandler.count (); if (dbResult.data[0].count !== result.data.length) throw 'data counts dont match';
  • 15. look at data on mongolab
  • 16. Homework Do the same thing, but copy to a Drive file instead of MongoLab. Hints. ● You'll need cDriverDrive (Ma__4vH--nQ_FPsuNF1BFuyz3TLx7pV4j) ● There is only one line different from the mongolab version (specify the folder and file name) // open the database to copy it to var dbHandler = cDbAbstraction.DbAbstraction(cDriverDrive, { dbid:"/datahandler/driverdrive/tasks", siloid:"customers.json" });
  • 17. Follow up materials Take a copy of these slides Join me on G+, or the G+ community More on desktop liberation More on database abstraction More 5 minute things