簡體   English   中英

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

[英]How to modify html file before HTMLOutput in Google Apps Script

我需要在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。 嘗試這個-

代碼-

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函數說.create TemplateFromFile (),而不是.createHtml OutputFromFile OutputFromFile基本上是從文件中獲取數據,但不對其進行處理。 換句話說,它不會呈現/合並StyleSheet或JavaScript文件。

您可以在Google Apps腳本文檔的最佳做法中閱讀有關如何進行設置的更多信息。

希望對您有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM