Overview
When //
in template literal is used in a HTML file in script editor, it is used as a comment start.
const sample = `//`;
For example, when above script is used in a HTML file at the script editor, ;"
of const sample =`//`;
is used as the comment.
Description
I would like to explain about this bug using the following sample flow.
Flow
-
Create new Spreadsheet and open the script editor. In this explanation, please use the container-bound script.
-
Put the following script to
Code.gs
.
function openDialog() {
const html = HtmlService.createHtmlOutputFromFile("index");
SpreadsheetApp.getUi().showModalDialog(html, "sample");
}
- Create a HTML file as
index
, and put the following script to the created HTML file.
<script>
const sample = `//`;
</script>
- At the script editor, please run “openDialog”. And please open the console.
- Please replace
`//`
to"//"
,`"//"`
,'//'
,`'//'`
and`\/\/`
. And test them.
- By this, you can see that the error of
Uncaught SyntaxError: Invalid or unexpected token
occurs only whenconst sample = `//`;
is tested. The result is as follows.
const sample = `//`;
occurs an error like above.const sample = "//";
occurs no error.const sample = `"//"`;
occurs no error.const sample = '//';
occurs no error.const sample = `'//'`;
occurs no error.const sample = `\/\/`;
occurs no error.
From these results, it is considered that when //
is used without enclosed by the double quotes and the single quotes in the template literal, it is used as the comment start. As the current workaround, `\/\/`
can be used.
By the way, when const sample = `//`;
is used as the Google Apps Script side (Code.gs
), no error occurs. So I thought that this might be the current specification or a bug.
This has been mentioned by this thread ( https://stackoverflow.com/q/61692427 ).
Google Issue Tracker
This has reported to Google Issue Tracker. https://issuetracker.google.com/issues/156139610