NOTE: ScriptDB is now deprecated. Please take a look at Database abstraction with google apps script for alternatives.
I leave this article here for interest.
In Using scriptDB, I covered some usages of scriptDB,along with a method of siloing data to keep it organized. Since scriptDb gives the possibility of shared persistent storage here’s a usage that might be useful for tracking things.
In Apps Script timer collection i covered how to measure how long certain activities took, but that was mainly for performance tuning. If you want to track activities, then you need some kind of persistent storage accessible from multiple places. You could for example, track who is opening what workbook.
Using the idea of siloing the scriptDB, we can set aside a slice of it to track when certain events start and finish using Google Apps Script. Here’s an example of logging a couple of events, and how long it took.
function testDb () { // this is where im centrally tracking all activities mcpher.trackInitialize (getPrivateDb()); // start doing something var record1 = mcpher.trackArrival("doing something with " + mcpher.WorkbookName()); //....do something inside that var record2 = mcpher.trackArrival("making tea" ); //... mcpher.trackDeparture(record2); //finished doing something mcpher.trackDeparture(record1); // report on it mcpher.trackReportAll ("logger"); }
function reportEverything() { mcpher.trackInitialize (getPrivateDb()); mcpher.trackReportAll ("logger"); }
function deleteEverything() { mcpher.trackInitialize (getPrivateDb()); mcpher.trackDeleteAll(); }
getPrivateDb()
to tell us which Db is to be used. To use the one private to the current spreadsheet script, you can create a function as followsfunction getPrivateDb() { // returns the scriptDB Im going to use privately for my account return ScriptDb.getMyDb(); }
However, much more usefully, you can use a shared DB by creating a script library , lets call it myLibrary, and create getPrivateDb() there. By adding a reference to myLibrary in each of your spreadsheets, you can get access to the same scriptDb. I recommend you create a getPrivateDb() in each spreadsheet script, which return the DB of a shared scriptDB as follows.
function getPrivateDb() { // returns the scriptDB that is shared return myLibrary. getPrivateDb (); }
bruce mcpherson is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Based on a work at http://www.mcpher.com. Permissions beyond the scope of this license may be available at code use guidelines