Search code examples
htmlcssgoogle-apps-script

How to connect css to html in Google Apps Scripts


I'm trying to make my first Google App. I have my HTML doc which has the include statement as outlined on HTML Service: Best Practices.

<html>
    <head>
        <base target="_top">
        <?!= include('stylesheet') ?>
    </head>
    <body>
       ...
    </body>

My code.gs file is as follows:

function doGet() {
   return HtmlService.createTemplateFromFile('BooksTS2.html')
  .evaluate();
}

My style sheet is named stylesheet.html, and for testing purposes is really simple:

<style>
   p {color: green;}
</style>

But when I run it I get this error message: ReferenceError: "include" is not defined.


Solution

  • In the example they created the reusable function include(), you need to add it in your code.gs file:

    function include(filename) {
      return HtmlService.createHtmlOutputFromFile(filename)
          .getContent();
    }
    

    You can always use the printing scriptlet directly in your html file like this:

    <html>
        <head>
            <base target="_top">
        <?!= HtmlService.createHtmlOutputFromFile('stylesheet').getContent(); ?>
        </head>
        <body>
           ...
        </body>
    </html>