5

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.

0

1 Answer 1

13

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>
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! That was exactly what I needed.
One note: if you are trying to develop elsewhere, using clasp say, and want to have js files be included, createHtmlOutputFromFile will throw an error if the js file isn't wrapped in tags like <script> or something. Those tags will then break your ability to include the same js file on a local machine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.