In a number of articles and blog posts I've been looking at using Google Apps Script to publish data
, to store it a scriptDB
and to behave as a proxy for both access and processing
. Pulling it all together, here is a way to publish data in multiple formats from scriptdb, to publish custom data, and even to publish the code of your own scripts. In these examples for now, all data and scripts are public. I haven't implemented oAuth as part of the tool yet
To implement your own version of this, see Step by Step Gas Publisher
How it fits together
Here's a quick explanation of what you will need.
- The publisher is a single script that can access all your public/enabled projects and retrieve source code, scriptDB contents or any kind of data (for example proxy processing, or spreadsheet data that you write a simple custom template handler for.
- The templates (google Apps Html service templates) are used to format and enumerate the data. 2 templates are provided - one for html and another for js. You can write your own or modify these as required.
- html format is expressed using the GAS HTML service. In other words a standalone web page is generated with the contents of the selected input.
- js, json, and jsonp are expressed using the GAS content service. In other words, some further processing and formatting is expected to happen on the expressed data.
- js format takes the encoded html code created by its template and assigns that to a variable. The generated script is inserted in a container web page, and that variable is simple assigned to a document element to provide formatted content. This mostly useful for inserting into Iframes for embedding in gadgets or web pages. This avoid cross domain x-frame problems that arise using iFrames with the GAS content service.
What you need to do to use.
Almost all of the code is in the mcpher shared library
. You will need to copy the provided code into a new project of your own, attach the mcpher library, and also attach any other of your projects as libraries that you would want to access. The js and html template can also be copied as is. For each library that will be accessed, you also need to create a couple of function that return your scriptDB and your scriptApp object. Finally if you expect to publish source code on google sites, you will need to use the provided gadget for embedding in your site.
| Parameter name|| Purpose|
| module|| Name of the module or siloid in scriptDB to fetch. Can also be a gist filename stored in a Gist, or the url of a web page.|
| template|| The name of the html service template to use. Needed for js & html. Ignored for the others. If not specified then the default template for the type is used|
| library|| The name of the attached library that hosts the module. If not specified will look in the same project as the publisher. If your are using a Gist as your source this can also be a gistID. |
| source|| the data source. Can be script|scriptdb|custom|gist|web. The data is rendered according to the template selected and the output type. You need to write your own handler for custom. Click on the options to see what you get as an html output for each one. script is the default.|
| callback|| the callback for jsonp. If you select jsonp type, but don't specify a callback, you get plain json.|
Here is a container using type js - try it
. When you use a type other than html, you need to create a container do deal with the data returned from the content service. Here's the code for the example
What's happening here
You'll notice that this prettifies the code just like in the GAS script IDE. You can use this with no modification, except perhaps to set up your own default parameters. The key thing to note here is that the js type will create a script with a function getMyEmbedded(). To include that in your web page, you simply have to set some elements .innerHTML to the code returned by getMyEmbedded() as below.
Here's the same thing directly from the web
The 'web' parameter will show the source code of a given url. So here is the same thing, directly from the web with this command
Embedding in google sites and other places.
You can use iFrames to include this container. It wont break any cross domain rules. You can use one of the public iFrame gadgets when you insert a gadget in Google Sites. Embedded below is the code for the Publisher. The Url displayed in the iFrame is
What about scriptDb
In Hosting your public scriptdb
I covered how to use scriptdb to store private data. You can use exactly the same mechanism, with the same range of outputs for scriptdb as you can for scripts. Here's what we get when we embed this link in the usual iFrame gadget,