As you know the language of Google Apps Script is javaScript. So you probably have a lot of useful code buried away there, that could be used in web apps. One solution is to copy it to your web site. Another would be to access it directly from Google Apps Script using the Content service with the recently implemented javascript content type.
Before we start, although I do use Google Cache services this is probably going to be slower than just copying the source everywhere, but accessing it directly from GAS at least means the code will be in sync.
Publishing the code
In Publishing with Google Apps Script I showed how to publish prettified versions of your code (not just from GAS but also from web sites, Github and so on) using the GAS html service and the content service. Taking that one step further, here’s how to include scripts directly from GAS in your javaScript apps.
Example
The code
<html> <head> <title>"Testing"</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js" type="text/javascript"></script> <link href="http://xliberation.com/p/css/gaspub.css" type="text/css" rel="stylesheet" /> <script src="http://xliberation.com/ga.js" type="text/javascript"></script> <script> $.when.apply($,loadModules (["vEquivalents","hacks","usefulColors"],"mcpher")).then ( function () { // now we can start.... console.log( compareColors(VBCOLORS.vbBlack, VBCOLORS.vbWhite) ); }, function (error) { console.log(JSON.stringify(error)); }); function loadModules(modules, sourceLibrary) { var promises = []; var url = "https://script.google.com/macros/s/AKfycbzhzIDmgY9BNeBu87puxMVUlMkJ4UkD_Yvjdt5MhOxR1R6RG88/exec"; for (var i = 0 ; i < modules.length;i++) { var u = url + "?source=script&type=javascript&module=" + modules[i] + "&library=" + sourceLibrary; promises.push ($.getScript(u)); } return promises; } </script> </head> <body> </body> </html>
How does it work?
The GAS scripts in the modules [“vEquivalents”,“hacks”,“usefulColors”] from the mcpher shared library are returned to the 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.
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 rather unpleasant script tag for loading the hacks module, for example.
For more stuff like this, see from VBA to Google Apps Script, and for more detail on this post see Serving up javaScript from GAS