1

I'm running into an interesting issue with Google Apps Script HTML force-printing scriptlets (<?!= ... ?>) where a JSON object is being strangely truncated directly before the double forward slashes of a URL (//) on the client side.

My objective is to insert these JSON objects into the HTML code to be used for JS code in a <script> element, for them to be used by the JS as if the JSON object were hard-coded into the HTML file.

What can I do to ensure all of the data I want to send to the client via scriptlets is correctly received/not truncated? Why does the data get truncated?

Minimal Reproducible Example

Server-side GAS code:

function doGet() {
  const template = HtmlService.createTemplateFromFile('fileName');
  template.data = JSON.stringify({
    "propertyWithSingleQuote": "T'Challa",
    "url": "https://stackoverflow.com/users/23479790/brodblox09"
  });
  return template.evaluate();
}

Server-side HTML code:

<!DOCTYPE html>
<html>
<head>
  <base target="_top">
</head>
<body>
  <script>
    const closingParagraphTag = `</p>`; // '
    console.log(<?!= data ?>);
  </script>
</body>
</html>

Received by client:

<!DOCTYPE html>
<html>
<head>
  <base target="_top">
</head>
<body>
  <script>
    const closingParagraphTag = `</p>`; // '
    console.log({"propertyWithSingleQuote":"T'Challa","url":"https:
  </script>
</body>
</html>

Expected output:

<!DOCTYPE html>
<html>
<head>
  <base target="_top">
</head>
<body>
  <script>
    const closingParagraphTag = `</p>`; // '
    console.log({
      "propertyWithSingleQuote": "T'Challa",
      "url": "https://stackoverflow.com/users/23479790/brodblox09"
    });
  </script>
</body>
</html>

The issue appears directly linked to the server-side HTML code, and does not seem to be the fault of the server-side GAS code. If the entire const closingParagraphTag = `</p>`; // ' line is removed, the force-printing scriptlet works just fine. If just the // ' comment is removed, the force-printing scriptlet works just fine. If just the `</p>` part of the line is removed/modified, e.g. changed to '</p>' or "</p>", the force-printing scriptlet also works just fine.

2
  • I'm considering using Utilities.base64Encode() in GAS and atob() in HTML so that this bug-seeming issue with GAS scriptlets can be circumvented, but I would still like a more appropriate/industry-correct solution to this if it exists. Commented Jan 28 at 17:17
  • 1
    @Wicket After a lot of trial and error, I have been able to pin down the source of the error I am encountering. I have refined my question and provided a proper minimal reproducible example. I have also attempted to improve the readability of the code lines and formatted the JSON objects to not be so large, and also removed the "time" property from the example JSON object as that is not required to demonstrate the issue. Please let me know if there is anything else I can do to make my question clearer, and thank you for your support thus far. Commented Jan 28 at 20:15

1 Answer 1

3

The problem is the apostrophe / single stright quote character in

const closingParagraphTag = `</p>`; // '

Remove it and the problem is solved.

If you think that this is a bug, please report it to Google using the issue tracker. For details see https://developers.google.com/apps-script/support

Sign up to request clarification or add additional context in comments.

2 Comments

Funny thing: the HTML Service used to remove the JavaScript comments, but at least today, the comment included in the code to reproduce the OP problem wasn't.
Added an issue to the issue tracker, thanks for pointing me to it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.