Google Script语法错误求助:意外标记<(第119行,文件"Code")
Let's break down what's causing this error and how to fix it:
The Root Cause
Your code is mixing up Google Apps Script HTML template syntax incorrectly. The <?!= tag expects a valid JavaScript expression (like a string variable, a function that returns HTML, or a quoted HTML string), but you're directly placing raw HTML tags right after it. The parser sees the < from <p> as an unexpected token because it doesn't belong in a JavaScript expression context.
Corrected Code Options
You have two clean ways to fix this:
Option 1: Use Standard Template Conditional Syntax
Wrap your logic in <? ?> tags and write the HTML directly outside them:
"<? if (authorized) { ?> <p><span style='color:green'>Authorized Successfully</span></p> <? } else { ?> <p><span style='color:red'>Not Authorized</span></p> <? } ?>".evaluate()
Option 2: Use a Ternary Expression with <?!=
If you prefer using <?!=, wrap your HTML in quotes to turn it into a valid string expression (note the escaped double quotes inside the style):
"<?!= authorized ? '<p><span style=\"color:green\">Authorized Successfully</span></p>' : '<p><span style=\"color:red\">Not Authorized</span></p>' ?>".evaluate()
Why This Works
Google Apps Script's HTML templating engine parses <? ?> as executable script blocks, and <?!= as a way to output unescaped content from a JavaScript expression. By either separating your logic from the HTML (Option 1) or turning the HTML into a string expression (Option 2), you're giving the parser valid syntax to work with, eliminating the unexpected < token error.
内容的提问来源于stack exchange,提问作者Tomas Forsythe




