Deprecated – Google Gadgets do not work anymore

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

 

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 apps script 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 apps script 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 apps script content service.
    • json and jsonp do not need a template. The source code or other requested data is encoded into jSon and returned using the GAS content service. The normal usage here would be to provide data for further processing. If you are using JavaScript, you will probably want to use jSonp to avoid same domain restriction issues, which wraps a callback around the jSon data.

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.

Parameters

 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.
 type  The type of output needed. Click on each the links to see what gets generated Can be js|json|jsonp|html|javascript. If you select jsop you also need a callback.
 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.

Containers

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
https://gist.github.com/brucemcpherson/4117688

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.
$('#myEmbedded').html(getMyEmbedded() );

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
"https://storage.googleapis.com/toasty/p/gaspubcontainer.html?source=script&module=Code"