Use project Javascript and CSS files in a Google Apps Script web app?

Here is a workaround I have found useful:

  1. Add this function to your server-side Javascript:

    function getContent(filename) {
        return HtmlService.createTemplateFromFile(filename).getRawContent();
    }
    
  2. Add a second ‘html’ file to your project that contains only the JS or CSS surrounded by <script> or <style> tags as appropriate.

    <!-- myscript.js.html -->
    <script>
        alert("Script included!");
    </script>
    
  3. Now you can include the script in your main HTML template like this:

    <?!= getContent("myscript.js") ?>
    

This way, you can have your JS and CSS split into as many files as you like and keep them all accessible from within the Apps Script project.

Leave a Comment