Step by Step Gas Publisher

I'm deprecating this shortly. Better to use Enhanced Github gadget if your source is on git hub. For how to do that see, Getting your apps scripts to Github

In Gas Publisher I described the detail of how the Gas Publisher works. Although most of the code is in the shared mcpher library,  there is some set up you have to do in your own projects. There is no coding to do - just attaching to libraries and a bit of cut and paste. Here's the schematic again to remind you of what we are trying to do. 
Gas publisher schematic


Creating a publisher

This is the publisher that will be exposed publicly, and you need to publish as a web app. All requests will be dealt with through this single doGet(e) function. You can copy a ready made one from here. That will cover these steps. Remember that everything is case sensitive.

  • Create default templates called js & html, or use the copied ones. I havent found out how to get the source of templates yet, so these are just pasted in. 

html template

<html>
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js" type="text/javascript"></script>
  <link href="http://caja.appspot.com/prettify.css" type="text/css" rel="stylesheet" />
  <script src="http://caja.appspot.com/prettify.js" type="text/javascript"></script>
  <style>
   .pretty {  margin: 8px;  padding: 4px }

  </style>
  <style>
    .module {margin: 8px;  padding: 10px; background :#8B4500; color :#F8F8FF; }
    .code {  background :#F8F8FF; ; line-height: 90% ; font-size: 90%;}
    .error { color:red ; background:white  } 
    .errorgas { color:white ; background:red ; } 
  </style>
 <script>
  $(document).ready(function () {
   // any jQueryness can happen here...
    var thingsThatScrewUp = [ 'toString', 'hasOwnProperty'],t;
    
    $('.pretty').each( function(i,elem) {
       var c = $(elem).text() ;
       // disguise
       for (var i = 0 ; i < thingsThatScrewUp.length ; i++ ) {
        c = c.replace(new RegExp("." + thingsThatScrewUp[i], 'g'),".sandw_" + i + "_ch");
       }
       try {
         t = prettyPrintOne(c);
       }
       catch (err) {
        $('#report').html(err + c);
       }
       // undisguise
       for (var i = 0 ; i < thingsThatScrewUp.length ; i++ ) {
         t = t.replace(new RegExp("sandw_" + i + "_ch", 'g'),thingsThatScrewUp[i]);
       }
       $(elem).html("<pre class='code pretty prettyprint'>" + t + "</pre>");
      });
      $('#working').html('Module:');
  });

  </script>
</head>
<body>
  <div>    
    <? var content = getSource(eArgs); ?>
    <? if (content.error) { 
      var errors = JSON.stringify (content, null , 2 ) ; ?>
      <div class = "error">
       <?= errors ?>
      </div>
    <? } else  { ?>
    <div class = "module"><span id="working">working on ... </span>
     <?= content.parameters.module ?>
    </div>   
    <div class = "code pretty"><pre class="prettyprint lang-js"><?= content.results.data ?></pre>
    </div>
    <? } ?>
  </div>
<div id="report"></div>
</body>
</html>

<html>
<head>

</head>
<body>

js template

  <div>    
    <? var content = getSource(eArgs); ?>
    <? if (content.error) { 
      var errors = JSON.stringify (content, null , 2 ) ; ?>
      <div class = "error">
       <?= errors ?>
      </div>
    <? } else  { ?>
    <div class = "module"><span id="working">Module: </span>
     <?= content.parameters.module ?>
    </div>   
    <div class = "code pretty"><pre class="prettyprint lang-js"><?= content.results.data ?></pre>
    </div>
    <? } ?>
  </div>
<div id="report"></div>
</body>
</html>

  • Attach any projects you want to access as libraries. You will need to create these functions (just copy them in as is) somewhere in every project that you want to be able to access from the publisher. This is because it needs to be able to get the scriptdb and scriptapp instance of the library it is reporting on and these are only accessible from the library itself. It will need these to exist to be able to access them.
function showMyScriptDb() {
  return publicStuffDb();
}
function showMyScriptAppResource(s) {
  try {
    return ScriptApp.getResource(s);
  }
  catch (err) {
    throw err + " getting script " + s;
  }
}
function publicStuffDb() {
  return ScriptDb.getMyDb();
}

  • Publish your Publisher project as a web app, allowing anybody to access, including anonymouse. This is public data remember.  For a reminder on how to do this see Hosting your public scriptdb
  • Create your own container script and host it on dropbox or somewhere if you plan to use gadgets for embedding. You should tweak the defaults to reflect the Url of your public app. Or, you can use my one and pass your url as an argument.  See Gas Publisher and copy the example from there.

Container



You are done. You can now publish and embed scriptdb and script data without worrying about cross domain stuff.  For help and more information join our forum,follow the blog or follow me on twitter . For more on Google Apps Script try Delegation to Google Apps Script or Gas Publisher

You want to learn Google Apps Script?

Learning Apps Script, (and transitioning from VBA) are covered comprehensively in my my book, Going Gas - from VBA to Apps script, available All formats are available now from O'Reilly,Amazon and all good bookshops. You can also read a preview on O'Reilly

If you prefer Video style learning I also have two courses available. also published by O'Reilly.
Google Apps Script for Developers and Google Apps Script for Beginners.




Comments