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.

2 Answers 2

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 <?!= ?>.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks! Looks like I have to do some review of templated HTML, but this gets it working.
why use createTemplate() instead ?
@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.
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.
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();

1 Comment

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.

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.