Apps Script Sidebar Reference Error When Using Include
Asked Answered
P

1

12

I created a Google Apps Script add-on to display a sidebar on click:

function displayPage_() {
  getScriptProperties_()
  var htmlTemplate = HtmlService.createTemplateFromFile('PAGE');
  var html = htmlTemplate.evaluate()
          .setTitle('Connector');
  SpreadsheetApp.getUi().showSidebar(html);
}   

This successfully loads the file PAGE.html as long as it is simple html. However, when I try to add include like this:

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
    <?!= include("STYLESHEET"); ?>
  </head>
  <body>
  </body>
</html>

It throws this error:

[17-02-09 08:55:50:239 MST] Execution failed: ReferenceError: "include" is not defined.

It doesn't matter what is in the include it always fails.

I have done this before and it worked. As far as I can tell, I have this set up just like the other project, but it doesn't work in this project. I assume that I forgot to enable something, but don't know how to tell what is missing because the transcript is vague.

What am I missing? Or, doing wrong?

Platelet answered 9/2, 2017 at 16:1 Comment(0)
S
28

I got crazy too... Found it in an example provided in Google Script Reference, what I didn't know was that include() is a user-defined function. Paste this code and it will work:

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

Let me know if I understood properly.

Swanson answered 9/2, 2017 at 16:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.