Continuing the theme of using Google Apps Script Content service and scriptDB for lots of things, this page will cover a few tricky topics
Why might we want to do this?Google apps script gives you the opportunity to serve up content without actually needing a web server. Let's say you wanted to create some dynamic image content throug some scheduled event, but had nowhere to serve it up from? ScriptDB could be a good repository for such data, and Google Apps Script could serve it upLoading image into scriptDBFirstly, let's take a look at loading an image into scriptDB. I'm going to use the silo functions to minimize development for this. These can be included in your project from the mcpher script library.Here we fetch some image, name it 'googleLogoBlob', convert it to base64 and save as a blob in a scriptDB silo called 'blobs'. function blob() { var googleLogoBlob = UrlFetchApp.fetch ("http://www.google.com/intl/en_com/images/srpr/logo3w.png") .getBlob().setName("googleLogoBlob"); var silo = mcpher.scriptDbSilo("blobs",ScriptDb.getMyDb()); silo.save ( {name: googleLogoBlob.getName(), blob: Utilities.base64Encode(googleLogoBlob.getBytes()) } ); } Publishing as a web applicationNext we need to publish a doGet(e) function that will be able to publish a jSon encoded version of that blob.function doGet(e) { return ContentService .createTextOutput(blobGet(e)) .setMimeType(ContentService.MimeType.JSON); } The result of that is shared here, and the function that does the work , takes the name parameter, and callback parameter to handle jSonP requests. function blobGet(e) { // imgs are in base64 format in scriptDB var stuffSilo = mcpher.scriptDbSilo("blobs",ScriptDb.getMyDb()); var results = stuffSilo.query( {name : e.parameter.name}); // find it? var result= results.hasNext() ? results.next() : { error: "no such blob " + e.parameter.name}; var j = Utilities.jsonStringify(result); // using jsonP? if (e.parameter.callback) { j = e.parameter.callback + "(" + j + ")" } return j; } Note that if callback is specified (and it should be), the jSon response is wrapped up in a call to the given callback function. See Proxy jsonp for more information on this technique Putting it together with jQueryFinally, here is a jQuery snippet of making that call and showing the image. You'll can test the whole thing with this jsfiddle, reproduced below.<div id='blobs'><img id='blobsimage' src=''/></div> $(document).ready(function() { var url = "https://script.google.com/a/macros/mcpher.com/s/AKfycbx37dFqzoaIdNfFZSujRr4iHEz0jKh5O_uUli6FPRjp3UEIevQY/exec"; var blobName = "googleLogoBlob"; $.getJSON(url + "?name=" + blobName + "&callback=?", null, function(blob) { $('#blobsimage').attr('src', "data:image/png;base64," + blob.blob); }); }); For help and more information join our community, follow the blog, follow me on twitter, or follow me on g+ |
Services > Desktop Liberation - the definitive resource for Google Apps Script and Microsoft Office automation > Things that have been deprecated >