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 toconst htmlText = html.getRawContent().replace(/{{value}}/g, "ReplacedValue");
.
- From this comment, when you use this workaround, a unique placeholder like
-
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