Google Apps Script – a new day – a new feature

GAS just keeps getting better. I was thinking the other day about how I might be able to embed sample Google Apps Script code in Google Sites, or other web sites in the way that you can embed Gist samples from github.

I searched around and couldn’t find anything suitable, so I posted a question on stackoverflow. Corey Goldfeder posted the answer, referring to a new (I think undocumented) method of the ScriptApp Class which can get the source code of your script file.

Along with the content service then, you can now get a jSon encoded version of the script of any module. Here’s an example from my mcpher utility library.

function doGet(e) {
       return ContentService
            .createTextOutput(JSON.stringify(mcpher.getMySource ( e )))
            .setMimeType(ContentService.MimeType.JSON); ;    

The code in the mcpher library that reacts to this is

 * Returns a modules source code
 * @param {parameters} e the argument to doGet(e). should have module parameter specified
 * @param {scriptappinstance} sap an instance of scriptapp
 * @return {object} The result.
function getMySource(e) {
  var results = {error : “missing module parameter”};
  if (e.hasOwnProperty(“parameter”)) {
    if (e.parameter.module) {
      try {
        var results =
        { code :
          { module : e.parameter.module, code : ScriptApp.getResource(e.parameter.module).getDataAsString() }
      catch (err) {
        var results = { error : “could not open module ” + e.parameter.module} ;
  return results;

function testGetSource() {
  Logger.log( getMySource (  {parameter: { module: “cRest” }}));

The Url to the public code (shown here asking for the code of module getMySource) is

Try it and see. So now you can get the source of any module, by creating a doGet() as above, and copying in the getMySource code.

I’ll implement this for google sites and elsewhere, and post here later with the results. No more maintaining source code as a Gist as well as Apps Script!

(function() {
var po = document.createElement(‘script’); po.type = ‘text/javascript’; po.async = true;
po.src = ‘’;
var s = document.getElementsByTagName(‘script’)[0]; s.parentNode.insertBefore(po, s);

Author: brucemcp

Leave a Reply

Your email address will not be published. Required fields are marked *