HtmlService.createTemplateFromFile deletes all comments

I’m using the following code to get my page code. when I print out the html, all comments are removed. is there anyway to keep the comments?

var distLists = '=' + JSON.stringify(readData(globalVariables().spreadsheetId, "distLists"))
var htmlTemplate = HtmlService.createTemplateFromFile('Index');
htmlTemplate.env = env
var htmlOutput = htmlTemplate.evaluate().setSandboxMode(HtmlService.SandboxMode.IFRAME)
    .setFaviconUrl(ico)
    .setTitle(title);
var html=htmlOutput.getContent();

2

I had the same situation with you. In that case, I used a workaround as follows. In the current stage, it seems that when the HTML is retrieved by getContent(), the comments in HTML and Javascript are removed. This is the same result with both createTemplateFromFile, createTemplate, createHtmlOutputFromFile, and createHtmlOutput. In order to explain this workaround, the following steps are run.

Usage:

1. Create a Google Apps Script project

Please create a Google Apps Script project as a standalone script.

2. Prepare sample HTML

Please create a sample HTML as index.html to the created Google Apps Script project and copy and paste the following HTML.

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    <!-- sample1 -->
    <?!= value ?>
    <!-- sample2 -->
    <script>
      // sample3
      var a = 1;
      // sample4
    </script>
  </body>
</html>

3. Sample scripts

Sample script 1

Please copy and paste the following script as code.gs.

function sample1() {
  const html = HtmlService.createTemplateFromFile("index");
  html.value = "ReplacedValue";
  const htmlText = html.evaluate().getContent();
  console.log(htmlText);
}

When this script is run, the following result is obtained.

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    
    ReplacedValue    
    <script>
      
      var a = 1;
      
    </script>
  </body>
</html>

You can see that the comments in HTML and Javascript were removed. In order to avoid this issue, I used the next script.

Sample script 2

Please copy and paste the following script as code.gs.

function sample2() {
  const html = HtmlService.createTemplateFromFile("index");
  const htmlText = html.getRawContent().replace(/<?!= value ?>/g, "ReplacedValue");
  console.log(htmlText);
}

When this script is run, the following result is obtained.

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    <!-- sample1 -->
    ReplacedValue
    <!-- sample2 -->
    <script>
      // sample3
      var a = 1;
      // sample4
    </script>
  </body>
</html>

In this case, <?!= value ?> is directly replaced with ReplacedValue by replace and the comments in HTML and Javascript are kept.

In this sample, the raw HTML content is retrieved by getRawContent() of Class HtmlTemplate as a text, and it is replaced using replace.

In this workaround, as a sample, the scriptlets like <?!= value ?> are replaced. But, it is not required to be used. A placeholder like {{value}} might be suitable for this situation instead of <?!= value ?>.

And, if you want to use the output HTML as HtmlService.HtmlOutput, you can use the following script.

function sample3() {
  const html = HtmlService.createTemplateFromFile("index");
  const htmlText = html.getRawContent().replace(/<?!= value ?>/g, "ReplacedValue");
  console.log(htmlText);
  
  const htmlOutput = HtmlService.createHtmlOutput(htmlText); // Here
}

Note:

  • In the current stage, 3 kinds of scriptlets in the template. Ref The above script is a simple workaround. So, please modify this to your actual situation.

  • From the following comment by TheMaster, I updated the answer.

    Sidenote: It should however be noted that if you use simple regex like this, you lose the security features associated with printing scriplets such as escaping untrusted inputs. Also, the value to replace needs to be unique enough to be captured by regex.

    • From this comment, when you use this workaround, a unique placeholder like {{value}} might be suitable rather than the scriptlet. In that case, const htmlText = html.getRawContent().replace(/<?!= value ?>/g, "ReplacedValue"); is modified to const htmlText = html.getRawContent().replace(/{{value}}/g, "ReplacedValue");.
  • By the value for replacing, the value might be required to be done the URL encoding. Please also be careful about this.

References:

  • getRawContent()
  • replace()

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật