2

I'm trying to separate my javascript and stylesheet from the main HTML file in my Google Apps Spreadsheet script that is published as a Web App. I have seen this answer to the problem, but I cannot get this approach to work for me.

When I have my stylesheet and javascript in the main HTML file, it works fine. When I try to separate them exactly as the answer recommends, the stylesheet and javascript code is not processed and instead the line that calls 'getContent()' is displayed in my browser. It looks like getContent() is not being executed at all.

I've tried moving my code away from the Spreadsheet to a Standalone Web App but still have the same problem. Any ideas why it's not working for me? Thank you!

A bit from my Code.gs:

   function doGet() {
      var output = HtmlService.createHtmlOutputFromFile('index');
      output.setSandboxMode(HtmlService.SandboxMode.IFRAME);
      output.setTitle('Dashboard Tool');
      return output;
    }
function getContent(filename) {
  Logger.log('getContent()');  
  return HtmlService.createTemplateFromFile(filename).getRawContent();
}

index.html:

<?!= getContent("stylesheet") ?>
    <div class='header'>Dashboard</div>
    <div id=content>
Content Here
</div>
<?!= getContent("javascript") ?>

'stylesheet.html' code is surrounded by the style tag and 'javascript.html' code is surrounded by the script tag.

Community
  • 1
  • 1
Carrie
  • 1,037
  • 2
  • 12
  • 19

2 Answers2

3

You forgot evaluate() in the createHtmlOutputFromFile(), also you should use the createTemplateFromFile, as such.

var output = HtmlService.createTemplateFromFile('index').evaluate();

As @brian-p pointed out, you need the 'Template' instead of 'Output', for the evaluate occur on the scriplets <?!= ?>.

Kriggs
  • 3,731
  • 1
  • 15
  • 23
  • Thanks! Looks like I have to do some review of templated HTML, but this gets it working. – Carrie Feb 10 '15 at 01:17
  • why use `createTemplate()` instead ? – Bryan P Apr 15 '15 at 18:04
  • @BryanP It was suposed to be be createTemplateFromFile, ended up writing only the first two words, but had written right on the code below. Edited. – Kriggs Apr 15 '15 at 18:07
  • right i know `FromFile` was needed, but i think it's important to point out *why* it needed to switch away from `createHtmlOutput` - *to enable the scriplet tags*. the server function inside the those tag types, won't run when using `createHtmlOutput`. could not wrap my head around that difference for the longest time. – Bryan P Apr 15 '15 at 18:22
1

In this line of code:

return HtmlService.createTemplateFromFile(filename).getRawContent();

createTemplateFromFile() is being used. That method is for creating the original template, but the getContent() function is not for that purpose. Use:

return HtmlService.createHtmlOutputFromFile(filename).getContent();
Alan Wells
  • 30,746
  • 15
  • 104
  • 152
  • 1
    After making the change recommended in the first answer, it seems that my original code referenced in this answer works OK. Though I need to brush up on templates, your answer does make sense and so I've made this change also. – Carrie Feb 10 '15 at 01:20