如何在Google Apps Script中修改HTMLOutput之前的html文件

How to modify html file before HTMLOutput in Google Apps Script

本文关键字:HTMLOutput 文件 html 修改 Google Apps Script      更新时间:2023-09-26

我需要在HTMLOut之前修改doGet()中的html文件。但是html文件中有<?!=include('css').getContent();?>,无法执行,成为html文本的一部分。感谢你的帮助?代码如下:

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')
}

项目

中的index.html
<!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>

如果你只是想用一些CSS和JavaScript加载页面。你不想在HTML文档中调用HTMLService。试试这个,

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>

注意doGet函数说的是.createTemplateFromFile(),而不是. createhtml OutputFromFile。OutputFromFile基本上从文件中获取数据,但不处理它。换句话说,它不呈现/合并样式表或JavaScript文件。

你可以在Google Apps Script文档中的最佳实践中阅读更多关于如何设置它的信息。