Serving apps script to JavaScript app

Here's an interesting idea. Let's say you want to use Google Apps Script as the master for a library of functions (since you can't use script tags in apps script) that you would like to use in both javaScript and Google Apps Script. Here's how to use an apps script web app as your CDN for your JavaScript applications.  There's an example of this being used in Color scheme explorer 

Using content service

As covered in Step by Step Gas Publisher you can expose your google apps script code and publish it to a web page, as JSON etc. Now that the google content service supports more MIME types, you can also serve up javaScript. With that, it's just a small hack to the Gas Publisher to be able to extract modules or even functions from apps script libraries (and GIST or even other web sites) and serve them up as javascript content. Since the language of GAS is javaScript, these should run perfectly well ( as long as you don't try to use specific apps script classes) in a pure JavaScript environment. 

Here's the very small enhancement to Gas Publisher that made this possible.

       // this is pure javascript
        case 'javascript':
          return ContentService.createTextOutput (
                   .setMimeType(ContentService.MimeType.JAVASCRIPT) ;  

An Example

Lets say we want to expose the VBA equivalent functions, plus the color manipulation functions from the mcpher shared library so that we can use them in some javaScript web app. Here's how it would look (using jQuery to load the scripts dynamically)

How does it work?

The apps script function in the modules ["vEquivalents","hacks","usefulColors"] from the mcpher shared library are returned to jQuery $.getScript() using the GAS publisher, with a MIME type of javaScript. Then we can simply execute the code that's in those modules (assuming they are not using any GAS specific classes). The test 

compareColors(VBCOLORS.vbBlack, VBCOLORS.vbWhite) 

uses functions from each of the 3 loaded modules and executes perfectly in its new javaScript environment. 

Using a script tag 

You'll notice that I'm loading the modules dynamically. This is not essential (you can use a regular script tag if you want), but like this you can go and do some other things whilst the scripts are loading asynchronously. Here's the script tag for loading the hacks module, for example. 

<script src=

For more stuff like this, see from VBA to Google Apps Script, and this blogpost.

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, All formats are available from O'ReillyAmazon 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.