简体   繁体   中英

How to modify html file before HTMLOutput in Google Apps Script

I need to modify a html file in doGet() before HTMLOut. But the html file has <?!=include('css').getContent();?> , which cannot be executed, and become part of HTML text. Appreciate your help? Here is the code.

function doGet(e) {
  var landingPage;
  landingPage=e.parameter.page ||'index';
  var html=HtmlService.createHtmlOutputFromFile(landingPage);

  var content=html.getContent();
  content=content.replace("%%ToBeChanged%%","New Value");
  var html2=HtmlService.createTemplate(content);
  return html2.evaluate()
    .setTitle('testing Website')
    .addMetaTag('viewport', 'width=device-width, initial-scale=1')
}

Here is the index.html in the project

<!doctype html>
<html>
<head>
<meta charset="UTF-8" />

   <title>TITLE</title>

   <?!=HtmlService.createHtmlOutputFromFile('styleSheet').getContent(); ?>
    <?!=HtmlService.createHtmlOutputFromFile('script').getContent(); ?>

</head>
<body>
  <form>
    <input type='text' value='##ToBeChanged##'>
  </form>
</body>
</html>

If you are just trying to load the page with some CSS and JavaScript. You don't want to call the HTMLService in your HTML document. Try this-

Code.gs—

function doGet() {
  var template = HtmlService.createTemplateFromFile('LandingPage'); 
  return template.evaluate().setSandboxMode(HtmlService.SandboxMode.IFRAME);
}

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

index.html-

<head>
<?!= include("StyleSheet"); ?>
</head>

Notice the doGet function says .create TemplateFromFile (), and not .createHtml OutputFromFile . OutputFromFile basically gets the data from the file, but doesn't process it. In other words it doesn't render/incorporate the StyleSheet or JavaScript files.

You can read more about how to set this up in the Google Apps Script Documentation under Best Practices .

I hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM