This document provides instructions for copying data between databases in 5 minutes using Apps Script. It describes opening a Google Sheet using the database abstraction library, getting all the data, opening a MongoDB database on MongoLab, writing the sheet data to the MongoDB database, and verifying the data was copied correctly. The document then suggests copying the data to a Google Drive file as homework using the driver sheet and driver drive libraries instead of MongoLab.
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';
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