首页 > 解决方案 > 该标签在 Google 脚本中的含义是什么?

问题描述

标签<?= ?>是谷歌脚本的特定语法还是可以在纯 html/javascript 页面中工作?有什么描述吗?

这类似于<?=$a; ?>PHP 标签,但当我看到这个时我怀疑:<? var foo = "test"; ?>.

标签: google-apps-scriptgoogle-sheets

解决方案


正式地,这些代码位在 GAS 文档中被称为“scriptlet”。它们是用于服务器端 HTML 渲染的 Apps 脚本语法。

在将 HTML 内容发送到浏览器以进行客户端渲染之前,<? ?><?! ?>被执行并且它们的输出被附加到模板中。它也可能只是这些标签之间的纯文本而不是脚本。当您在 GAS 中将 HtmlTemplate 转换为 HtmlOutput 对象时,Scriptlet 会被执行:

 //HtmlTemplate instance
 var htmlTemplate = HtmlService.createTemplate("<a href='<?!= www.google.com ?>'>Google</a>"); 

 //HtmlOutputInstance - calling evaluate() fires off the scriptlets and 
 //creates HTML output that is ready to be sent to the client. 
 var htmlOutput = htmlTemplate.evaluate(); 

 //Logs <a href='www.google.com'> Google </a>
 Logger.log(htmlOutput.getContent());

最简洁的答案是不。您不能直接在浏览器中运行小脚本——它们是在 Google 服务器上执行的。您是正确的,有许多模板引擎提供相同的功能但实现方式不同。


推荐阅读