This Google Apps Script library contains a significant amount of the code you will need for most samples on this site. You access access my full library here
su_button url=”https://docs.google.com/spreadsheets/d/1DlKpVVYCrCPNfRbGsz6N_K3oPTgdC9gQIKi0aNb42uI/edit#gid=0″ target=”blank” style=”glass” center=”yes” icon=”icon: download” icon_color=”#f50808″ desc=”Get Library Info”][/su_button]
Here’s how to include it in your code.
How to include mcpher library in your project
Create a new document that is going to access this library, and bring up the Manage Libraries dialog in the script editor.
Add a reference to the shared library.
You’ll see whichever versions are available. If you leave development mode off, then your usage will be frozen to the version you select. You can change the Identifier to something else if you like. Its purpose is to nominate the namespace you will use to reference the shared functions. In this library shown all calls will be prefixed with the Mcpher namespace identifier – for example Mcpher.isEmpty()
.
References
Project Key :
MEQ3tE5y5_cTOAgUbUKSIAiz3TLx7pV4j
The Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 |
/** * @static */ var dbSilo; function createDbSilos(optionalDb){ return dbSilo ? dbSilo : (dbSilo = new cScriptDbSilo(optionalDb)); } function librarySiloDb() { return ScriptDb.getMyDb(); } /** * a cScriptDbSilo * @class * @param {ScriptDbInstance} Db the default scriptDB * @implements {cScriptDbSilo} * @return {cScriptDbSilo} a new cScriptDbSilo */ function cScriptDbSilo(optionalDb) { // create a Silo for known script silos this.xSilos = new collection(); this.silos = function () { return this.xSilos; }; this.xDb = IsMissing(optionalDb) ? librarySiloDb() : optionalDb; return this; } /** * return the db silo item associated with the given key * @param {string} k the key * @param {ScriptDbInstance=} optionalDb the default scriptDB (default the db for the silo collection) * @param {boolean=} createIfUnknown whether to create the item if it doesnt exist (default true) * @param {boolean=} optionalHash whether to encrypt the userstamp * @param {boolean=} optionalMeOnly whether to filter by only records that I created * @return {cScriptDbSiloItem} the cScriptDbSiloItem */ function scriptDbSilo(k,optionalDb,createIfUnknown,optionalHash,optionalMeOnly) { // find the Silo associated with this key // get the item or create it var dbSilo = createDbSilos(optionalDb); if (IsMissing(k)) return dbSilo; else { var silo = dbSilo.silos().item(k,false); // return the SiloItem, or create one if it's allowed return silo ? silo : (fixOptional (createIfUnknown,true) ? dbSilo.init(k,optionalDb,optionalHash,optionalMeOnly) : null) ; } } /** * add db silo item associated with the given key * @param {string} k the key * @param {ScriptDbInstance=} optionalDb the default scriptDB (default the db for the silo collection) * @param {boolean=} optionalHash whether to encrypt the userstamp * @param {boolean=} optionalMeOnly whether to filter by only records that I created * @return {cScriptDbSiloItem} the cScriptDbSiloItem */ cScriptDbSilo.prototype.init = function (k,optionalDb,optionalHash,optionalMeOnly) { return this.silos().add (new cScriptDbSiloItem(k,this,optionalDb,optionalHash,optionalMeOnly),k); }; /** * remove all db silo items for this db * @param {object} ob the query by example object * @param {ScriptDbInstance=} optionalDb the default scriptDB (default the db for the silo collection) * @return {cScriptDbSilo} the cScriptDbSilo */ cScriptDbSilo.prototype.remove = function (ob,optionalDb) { return this.removeData(ob,optionalDb); } /** * remove db silo item data associated with the given query object * @param {object} ob the query by example object * @param {ScriptDbInstance=} optionalDb the default scriptDB (default the db for the silo collection) * @return {cScriptDbSilo} the cScriptDbSilo */ cScriptDbSilo.prototype.removeData = function (ob,optionalDb) { var qob = fixOptional(ob,{}); //-- disabled for now---only allowed to delete your own data //qob.userStamp = Session.getUser().getEmail(); //if (this.xHash) qob.userStamp = shortKeyHash(qob.userStamp); var db = fixOptional(optionalDb, this.xDb); // we'll use removeBatch while(true) { var result = db.query(qob); // get everything, up to limit if (result.getSize() == 0) { break; } var o = []; while (result.hasNext()) { if (o.length > this.xBatchMax) { DebugAssert(db.allOk(db.removeBatch(o, false)), "failed to delete " + o.length + " objects"); o = []; } o.push(result.next()); } if (o.length > 0 ) { var result = db.removeBatch(o, false); DebugAssert(db.allOk(result), "failed to delete " + o.length + " objects"); } } return this; } /** * fill the silos from the data in the script DB * @param {object} ob the query by example object * @param {ScriptDbInstance=} optionalDb the default scriptDB (default the db for the silo collection) * @return {cScriptDbSilo} the cScriptDbSilo */ cScriptDbSilo.prototype.fill = function (optionalDb) { // fill Silo with every known silo in the given db var db = fixOptional ( optionalDb, this.xDb); var result = db.query({}); //note-- only works up to the limit while (result.hasNext()) { var k = result.next().siloId; if ( ! this.Silos.item(k,false) ) this.Silos().add (new cScriptDbSiloItem(k,this,db),k ); } return this; }; /** * a cScriptDbSiloItem * @class * @param {string} k the key * @param {cScriptDbSilo=} parent the collection of silo items * @param {boolean=} optionalHash whether to encrypt the userstamp * @param {boolean=} optionalMeOnly whether to filter by only records that I created * @implements {cScriptDbSiloItem} * @return {cScriptDbSiloItem} a new cScriptDbSiloItem */ function cScriptDbSiloItem(k,parent,optionalDb,optionalHash,optionalMeOnly) { // a silo item this.xBatch = []; this.xKey = makeKey(k); this.xParent = parent; this.xHash = fixOptional( optionalHash, false); this.xMeOnly = fixOptional( optionalMeOnly, false); this.xDb = fixOptional( optionalDb , this.xParent.xDb); this.db = function() { return this.xDb; } // this is wont change in a silo session so may as well do it once this.xWho = this.who(); // there is a bug in savebatch that failes for more than about 4000 // here's a workaround this.xBatchMax = 4000; return this; } /** * construct the query object that identifies this query by example * @param {object=} ob the query by example (default all data) * @return {object} a query by example to silo the data */ cScriptDbSiloItem.prototype.querySet = function(ob) { // apply a query for this silo var newOb = fixOptional(ob,{}); newOb.siloId = this.xKey; if (!newOb.userStamp && this.xMeOnly) newOb.userStamp = this.xWho; return newOb ; }; cScriptDbSiloItem.prototype.queryStamp = function(ob) { // apply a user and time stamp ob.timeStamp = new Date().getTime(); ob.userStamp = this.xWho; return ob; }; /** * do a query within the silo for this cScriptDbSiloItem * @param {object} [ob={}] the query by example (default all data) * @param {object} [ob={}] the query by example (default all data) * @return {ScriptDBquery} a query by example to silo the data */ cScriptDbSiloItem.prototype.query = function(ob) { return this.db().query(this.querySet(ob)); }; /** * do a query within the silo and put to an array, taking account of limits * @param {object} [ob={}] the query by example (default all data) * @param {object} [ob={}] the query by example (default all data) * @param {number} [optLimit=0] default bypass any limits * @return {Array} an array of results */ cScriptDbSiloItem.prototype.queryArray = function(ob, optLimit) { // this is the max I'll take in one go to be compatible with parse.com var maxLimit = 1000; var limit = fixOptional ( optLimit, 0); var results =[], q = this.querySet(ob); while(true) { var qr = this.db() .query(q) .limit(limit > 0 ? limit-results.length : maxLimit).startAt(results.length); // we're done if (!qr.hasNext()) return results; // still have pushing to do while(qr.hasNext() && (limit==0 || results.length < limit)) results.push(qr.next()); } }; /** * do a count query within the silo for this cScriptDbSiloItem * @param {object} [ob={}] the query by example (default all data) * @return {ScriptDBquery} a query by example to silo the data */ cScriptDbSiloItem.prototype.count = function(ob) { return this.db().count(this.querySet(ob)); }; /** * do a save within the silo for this cScriptDbSiloItem * @param {object} ob the data so save * @param {boolean} optBatch whether or not to save up for a batch update * @return {ScriptDbResult} a query result */ cScriptDbSiloItem.prototype.save = function(ob,optBatch) { var sob = this.queryStamp(this.querySet(ob)); if (fixOptional (optBatch, false)) { if (this.batchLength() > this.xBatchMax) { this.saveBatch(); } this.xBatch.push(sob); } else return this.db().save(sob); }; /** * provide current batch length * @return {number} the batch length */ cScriptDbSiloItem.prototype.batchLength = function() { return this.xBatch ? this.xBatch.length : 0; }; /** * clear out any batch items * @return {ScriptDbResult} a query result */ cScriptDbSiloItem.prototype.saveBatch = function() { if (this.xBatch.length) { var results = this.db().saveBatch(this.xBatch, false); this.xBatch = []; return results; } else return null; }; /** * do a save within the silo for this cScriptDbSiloItem, and delete the data * @param {object=} ob the data so remove (default all data for this silo) * @return {cScriptDbSiloItem} the cScriptDbSiloItem */ cScriptDbSiloItem.prototype.remove = function(ob) { return this.xParent.removeData(this.querySet(ob),this.db()); }; /** * get who user is, and hash email address if required * @param {boolean=} optHash whether to encrypt, otherwise use the silo default * @return {string} the user email or hashed emaik */ cScriptDbSiloItem.prototype.who = function(optHash) { return fixOptional(optHash, this.xHash) ? shortKeyHash(Session.getUser().getEmail()) : Session.getUser().getEmail(); }; /** * return the stuff from the db associated with the e.parameter.entry value * @param {String} siloNamethe the locker name * @param {scriptDB} db the private db to use * @return {ContentService} the rest response */ function getMyStuff (siloName, db) { var response = {status: {code:"bad", reason:"entry parameter not found"}, result:null}; var silo = scriptDbSilo(siloName,db); var thisMail = silo.who(); if (!db) { response.reason = "private db must be specified" ; } else if (siloName) { var q= silo.query(); if (q.hasNext()) { // only retrievable by teh same user as created it var result = q.next(); try { if (result.userStamp != thisMail) { response.status.code = "bad"; response.status.reason = siloName + "does not belong to you"; } else { response.status.code = "good"; response.status.reason = siloName + " found in your stuff"; response.result = result; } } catch (err) { response.status.code = "bad"; response.status.reason = err; } } else { response.status.code = "bad"; response.status.reason = siloName + " not found in your stuff"; } } return response; } /** * create an entry for the given siloname value * @param {String} siloNamethe the locker name * @param {scriptDB} db the private db to use * @param {object} content the content to store * @return {scriptDBObject} the saved object */ function createStuff (siloName, db, content) { // exist? DebugAssert(db, "you must provide a db"); var silo = scriptDbSilo(siloName,db); var myAuth = silo.query(); if (myAuth.hasNext()) silo.remove(); var put = {}; put.myStuff = content; put.help = "for details see ramblings.mcpher.com"; return silo.save(put); } /** react to a doGet(e) using lockbox for oAuth * @param {object} e as passed to doGet() (show or oauth) * @param {scriptDBInstance} db the private db to use * @return {String} the json string content */ function getStuffContent (e,db) { //parameters // e.parameter.entry - the lockBox entry name // e.parameter.proxyUrl - optional - if specifed then proxyUrl to execute , using lockbox entry for oAuth // e.parameter.action - manadatory - (not implemented yet edit,)show,oauth var content =""; if(IsMissing(db)) content = JSON.stringify ({error: "programming error - you must provide a scriptDB to use"}) ; else { var entry = decodeURIComponent(e.parameter.entry) var proxyurl = decodeURIComponent(e.parameter.proxyurl) ; var action = decodeURIComponent(e.parameter.action); try { switch (action) { case 'edit': content= JSON.stringify ({error: "action not yet implemented" + action}) ; break; //TODO return uiStuff (db, entry); case 'show': content = JSON.stringify(getMyStuff(entry,db)); break; case 'oauth': content = oAuthProxy (db,entry, proxyurl).getContentText(); break; default: content = JSON.stringify ({error: "unknown action - should be show or oauth" + action}) ; break; } } catch (err) { content = JSON.stringify ( { error: err } ); } } return content; } /** * oAuthPacket will create a packet of options and oauth parameters * @param {object} stuff contents of an oauth lockbox * @return {urlfetch.advancedoptions} the packet to use as options to urlfetch */ function oAuthPacket(stuff) { // relevant properties from silo var locker = stuff.result.myStuff; var siloId = stuff.result.siloId; // use GAS oauth library var oa = UrlFetchApp.addOAuthService(siloId); oa.setAccessTokenUrl (locker.accessUrl); oa.setRequestTokenUrl (locker.requestUrl); oa.setAuthorizationUrl(locker.authorizeUrl); oa.setConsumerKey(locker.consumerKey); oa.setConsumerSecret(locker.consumerSecret); // advanced oauth options return { "oAuthServiceName": siloId, "oAuthUseToken" : "always" }; } /** * oAuthProxy will execute an oauth then call some url by proxy * @param {String} siloId siloid of the scriptdb locker holding the oauth information * @param {scriptDBInstance} db the private db to use * @param {String} url to execute * @return {String} response from the urlfetch */ function oAuthProxy (db,siloId, url) { // get myStuff - this will have previously been stored in the myStuff locker var stuff = getMyStuff(siloId,db); DebugAssert(stuff.status.code == "good", "error getting secrets " + JSON.stringify(stuff) ); // construct the oauth packet var packet = oAuthPacket(stuff); // issue request return UrlFetchApp.fetch(url, packet); } /** * return the stuff from the db associated with the e.parameter.entry value * @param {String} siloNamethe the locker name * @param {scriptdbinstance} db public db * @param {queryOb} optQuery optional query by example * @return {ContentService} the rest response */ function getPubStuff (siloName, db, optQuery) { var q; var response = {status: {code:"bad", reason:"unknown error"}, results:null}; var ob = optQuery || {}; q= siloName ? scriptDbSilo(siloName,db).query(ob) : db.query(ob); if (q.hasNext()) { response.status.code = "good"; response.status.reason = siloName + " found in your stuff"; response.results = [] ; while (q.hasNext()) { response.results.push(q.next()); } } else { response.status.reason = siloName + " has no data"; response.status.query = JSON.stringify(ob); } return response; } /** react to a doGet(e) to return public data * @param {object} e as passed to doGet() * @param {scriptdbinstance} db public db * @return {String} the json string content */ function getPubStuffContent (e,db) { //parameters // e.parameter.entry - optional the lockBox entry name var content ; var entry = e.parameter && e.parameter.entry ? decodeURIComponent(e.parameter.entry) : null; try { content = JSON.stringify(getPubStuff(entry,db)); } catch (err) { content = JSON.stringify ( { error: err } ); } return content; } /** show everything in a given db * @param {scriptdbinstance} db public db * @return {String} the json string content */ function showAll(db) { var results = db.query({}); while (results.hasNext()) { var result = results.next(); Logger.log(Utilities.jsonStringify(result)); } } |
You can find more information on this library here. For help and more information join our forum, follow the blog or follow me on twitter . Now take a look at the next step in step by step guide to delegating to Google Apps Script.