Hosting your public scriptdb

You will need a script to host the scriptdb that contains your public data. In the step by step guide, Delegation to Google Apps Script, you should have created a script, pubstuff and added the mcpher library. Here's the code you need, you can just paste this in unchanged.

/** @description
 * using scrpt DB as a repository for public stuff - accessible through a rest call
 * @author <a href="mailto:bruce@mcpher.com">Bruce McPherson</a><a href="http://ramblings.mcpher.com"> ramblings.mcpher.com</a>
 */

/**
 * return the stuff from the db associated with the e.parameter.entry value or execute a proxy URL using lockbox for oAuth
 * @param {object} e the event parameters
 * @return {String} the rest response
 */
function doGet(e) {
       return ContentService
            .createTextOutput(mcpher.getPubStuffContent (e,publicStuffDb()))
            .setMimeType(ContentService.MimeType.JSON); ;     
}

/**
 * myStuffDb return the shared DB for myStuff
 * @return {ScriptDbInstance} the db
 */
function publicStuffDb() { 
  return ScriptDb.getMyDb();
}

function testPublicStuff(){
  Logger.log ( mcpher.getPubStuffContent(  { parameter: null}, publicStuffDb() ) );
}

Walkthrough

  • publicStuffDb is the function that will provide access to your public scriptdb, and will be used by other scripts. 
  • doGet(e) will expect the optional parameter ?entry=[name]. If no name is specified then the entire shared scriptDB will be shown.
  • ContentService is used to serve up a jSon representation of the data in the scriptDB

Testing

I always try to keep the data in a doGet() function to a minimum. This means that I can test the whole thing before publishing it. The function testPublicStuff () will log the contents of the scriptDB so you can check before publishing.  If you look at the log, you should see this, since we haven't yet put anything in the scriptdb
{"status":{"code":"bad","reason":"null has no data"},"results":null}

Publishing

This is going to be a public script, available to all, so we are are going to make it publicly accessible and publish it with anonymous access. Here's how

First authorize doGet() by running it. You'll get this message. You only have to do this once.

authorize doget

Go to manage version

and save this version
google apps script manage version
Now we can publish this as a web application.
google apps script publish web application

and make it accessible publicly, even anonymous

google apps script publish anonymous

Now take a note of  the url of your published app. My version of this example is https://script.google.com/a/macros/mcpher.com/s/AKfycbz3tRO5VJ-Sr7C_0rcQf2lWqxIPNN__LsNNV_ZfuvO5Ly0RAnWU/exec

publish doget url


Copy the URL to a browser. You should get the same result as you did while testing. 
{"status":{"code":"bad","reason":"null has no data"},"results":null}

Project properties

You are going to need the project properties of the script you have just created so you can include it as a library in other scripts that need to access this scriptdb. Take note of your own key from the project properties.  Here's mine below.

project properties google apps script


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.




Comments