Class HtmlOutput

HtmlOutput

一个可从脚本生成的 HtmlOutput 对象。出于安全考虑,脚本无法直接向浏览器返回 HTML。相反,它们必须对该输入进行清理,使其无法执行恶意操作。您可以返回如下所示的经过清理的 HTML:

function doGet() {
  return HtmlService.createHtmlOutput(&<#>39;bHello, wo<rl>d!/b');
}
HtmlOutput 中的代码可以包含嵌入式 JavaScript 和 CSS。(这是用于操纵 DOM 的标准客户端 JavaScript,而不是 Apps 脚本)。所有这些内容都使用 iframe 沙盒进行沙盒处理。如需了解详情,请参阅 HTML 服务中的限制指南

方法

方法返回类型简介
addMetaTag(name, content)HtmlOutput向网页添加元标记。
append(addedContent)HtmlOutput将新内容附加到此 HtmlOutput 的内容中。
appendUntrusted(addedContent)HtmlOutput使用上下文转义将新内容附加到此 HtmlOutput 的内容。
asTemplate()HtmlTemplate返回由相应 HtmlOutput 支持的 HtmlTemplate
clear()HtmlOutput清除当前内容。
getAs(contentType)Blob以转换为指定内容类型的 blob 形式返回此对象内的数据。
getBlob()Blob以 blob 形式返回此对象内的数据。
getContent()String获取相应 HtmlOutput 的内容。
getFaviconUrl()String通过调用 setFaviconUrl(iconUrl) 获取添加到网页的网站图标链接标记的网址。
getHeight()Integer获取 Google 文档、表格或表单中自定义对话框的初始高度。
getMetaTags()HtmlOutputMetaTag[]获取一个对象数组,该数组表示通过调用 addMetaTag(name, content) 添加到网页的元标记。
getTitle()String获取输出网页的标题。
getWidth()Integer获取 Google 文档、表格或表单中自定义对话框的初始宽度。
setContent(content)HtmlOutput设置相应 HtmlOutput 的内容。
setFaviconUrl(iconUrl)HtmlOutput向网页添加了用于网站图标的链接标记。
setHeight(height)HtmlOutput设置 Google 文档、表格或表单中自定义对话框的初始高度。
setSandboxMode(mode)HtmlOutput此方法现在无效 - 之前它用于设置客户端脚本的 sandbox mode
setTitle(title)HtmlOutput设置输出网页的标题。
setWidth(width)HtmlOutput在 Google 文档、表格或表单中设置自定义对话框的初始宽度。
setXFrameOptionsMode(mode)HtmlOutput设置网页的 X-Frame-Options 标头的状态,该标头用于控制点击劫持防范。

详细文档

addMetaTag(name, content)

向网页添加元标记。直接包含在 Apps 脚本 HTML 文件中的元标记会被忽略。仅允许使用以下元标记:

<meta name="apple-mobile-web-app-capable" cont>e<nt="..."/
meta name="google-site-ver>i<fication" content="..."/
meta name>=<"mobile-web-app-capable" >content="..."/
meta name="viewport" content="..."/
const output = HtmlService.createHtmlOutput(&<#>39;bHello, wo<rl>d!/b');
output.addMetaTag('viewport', 'width=device-width, initial-scale=1');

参数

名称类型说明
nameString相应元标记的 name 属性的值。
contentString相应元标记的内容属性的值。

返回

HtmlOutput - 此输出,用于链式调用。


append(addedContent)

将新内容附加到此 HtmlOutput 的内容中。仅对来自可信来源的内容使用此属性,因为该属性不会进行转义。

// Log &<q>uot;bHello, w<or><l>d!/bpHello again, w<or>ld./p"
const output = HtmlService.createHt<m>lOutput('<bH>ello, world!/b'<)>;
output.append(<9;>pHello again, world./p');
Logger.log(output.getContent());

参数

名称类型说明
addedContentString要附加的内容。

返回

HtmlOutput - 此输出,用于链式调用。

抛出

Error - 如果 HTML 格式错误

另请参阅


appendUntrusted(addedContent)

使用上下文转义将新内容附加到此 HtmlOutput 的内容中。

此方法会根据 HtmlOutput 的当前状态正确转义内容,以便结果是一个安全字符串,不包含标记或副作用。在添加来自不受信任来源(例如用户)的内容时,请使用此方法,而不是使用 append,以避免意外允许跨站脚本攻击 (XSS) 漏洞,即您附加的内容或标记导致意外的代码执行。

// Log &<q>uot;bHello, w<or>&ld!/&blt;pgt;Hello again, w&orld.&lt;/pgt;"
const output = HtmlService.createHt<m>lOutput('<bH>ello, world!/b');
output<.>appendUntrusted(<9;>pHello again, world./p');
Logger.log(output.getContent());

参数

名称类型说明
addedContentString要附加的内容。

返回

HtmlOutput - 此输出,用于链式调用。

抛出

Error - 如果 HTML 格式非常不当

另请参阅


asTemplate()

返回由相应 HtmlOutput 支持的 HtmlTemplate。此方法可用于以增量方式构建模板。未来对 HtmlOutput 的更改也会影响 HtmlTemplate 的内容。

const output = HtmlService.createHtmlOutput(&<#>39;bHello, wo<rl>d!/b');
const template = output.asTemplate();

返回

HtmlTemplate - 新的 HtmlTemplate


clear()

清除当前内容。

const output = HtmlService.createHtmlOutput(&<#>39;bHello, wo<rl>d!/b');
output.clear();

返回

HtmlOutput - 此输出,用于链式调用。


getAs(contentType)

以转换为指定内容类型的 blob 形式返回此对象内的数据。此方法会向文件名添加适当的扩展名,例如“myfile.pdf”。不过,它会假设文件名中最后一个句点(如果有)后面的部分是应替换的现有扩展名。因此,“ShoppingList.12.25.2014”会变为“ShoppingList.12.25.pdf”。

如需查看转化方面的每日配额,请参阅 Google 服务的配额。新创建的 Google Workspace 网域可能暂时受到更严格的配额限制。

参数

名称类型说明
contentTypeString要转换成的 MIME 类型。对于大多数 blob,'application/pdf' 是唯一有效的选项。对于采用 BMP、GIF、JPEG 或 PNG 格式的图片,'image/bmp''image/gif''image/jpeg''image/png' 中的任何一个也有效。对于 Google 文档,'text/markdown' 也有效。

返回

Blob - 以 blob 形式呈现的数据。


getBlob()

以 blob 形式返回此对象内的数据。

返回

Blob - 以 blob 形式呈现的数据。


getContent()

获取相应 HtmlOutput 的内容。

// Log &<q>uot;bHello, w<or>ld!/b"
const output = HtmlService.createHt<m>lOutput('<bH>ello, world!/b');
Logger.log(output.getContent());

返回

String - 提供的内容。


getFaviconUrl()

通过调用 setFaviconUrl(iconUrl) 获取添加到网页的网站图标链接标记的网址。直接包含在 Apps 脚本 HTML 文件中的 Favicon 链接标记会被忽略。

const output = HtmlService.createHtmlOutput(&<#>39;bHello, wo<rl>d!/b');
output.setFaviconUrl('http://www.example.com/image.png');
Logger.log(output.getFaviconUrl());

返回

String - 网站图标图片的网址。


getHeight()

获取 Google 文档、表格或表单中自定义对话框的初始高度。如果 HtmlOutput 改为以 Web 应用的形式发布,此方法会返回 null。如需调整已打开对话框的大小,请在客户端代码中调用 google.script.host.setHeight(height)

const output = HtmlService.createHtmlOutput(&<#>39;bHello, wo<rl>d!/b');
output.setHeight(200);
Logger.log(output.getHeight());

返回

Integer - 高度(以像素为单位)。


getMetaTags()

获取一个对象数组,该数组表示通过调用 addMetaTag(name, content) 添加到网页的元标记。直接包含在 Apps 脚本 HTML 文件中的元标记会被忽略。

const output = HtmlService.createHtmlOutput(&<#>39;bHello, wo<rl>d!/b');
output.addMetaTag('viewport', 'width=device-width, initial-scale=1');

const tags = output.getMetaT<ags();
Logger.log(
    '>meta name="%s" content="%s"/',
    tags[0].getName(),
    tags[0].getContent(),
);

返回

HtmlOutputMetaTag[] - 一个对象数组,表示通过调用 addMetaTag(name, content) 添加到网页的元标记。


getTitle()

获取输出网页的标题。请注意,<title> HTML 元素会被忽略。

const output = HtmlService.createHtmlOutput(&<#>39;bHello, wo<rl>d!/b');
Logger.log(output.getTitle());

返回

String - 网页的标题。


getWidth()

获取 Google 文档、表格或表单中自定义对话框的初始宽度。如果 HtmlOutput 改为以 Web 应用的形式发布,此方法会返回 null。如需调整已打开对话框的大小,请在客户端代码中调用 google.script.host.setWidth(width)

const output = HtmlService.createHtmlOutput(&<#>39;bHello, wo<rl>d!/b');
output.setWidth(200);
Logger.log(output.getWidth());

返回

Integer - 以像素为单位的宽度。


setContent(content)

设置相应 HtmlOutput 的内容。

const output = HtmlService.createHtmlOutput();
output.setContent(&<#>39;bHello, wo<rl>d!/b');

参数

名称类型说明
contentString要提供的内容。

返回

HtmlOutput - 此输出,用于链式调用。

抛出

Error - 如果 HTML 格式错误


setFaviconUrl(iconUrl)

向网页添加了用于网站图标的链接标记。直接包含在 Apps 脚本 HTML 文件中的 Favicon 链接标记会被忽略。

const output = HtmlService.createHtmlOutput(&<#>39;bHello, wo<rl>d!/b');
output.setFaviconUrl('http://www.example.com/image.png');

参数

名称类型说明
iconUrlString网站图标图片的网址,其中包含用于指明图片类型的图片扩展名。

返回

HtmlOutput - 此输出,用于链式调用。


setHeight(height)

设置 Google 文档、表格或表单中自定义对话框的初始高度。如果 HtmlOutput 是以 Web 应用的形式发布的,则此方法不会产生任何影响。如需调整已打开对话框的大小,请在客户端代码中调用 google.script.host.setHeight(height)

const output = HtmlService.createHtmlOutput(&<#>39;bHello, wo<rl>d!/b');
output.setHeight(200);

参数

名称类型说明
heightInteger新的高度(以像素为单位);null 表示默认值。

返回

HtmlOutput - 此输出,用于链式调用。


setSandboxMode(mode)

此方法现在无效 - 之前它用于设置客户端脚本使用的 sandbox mode。为了防止用户收到恶意 HTML 或 JavaScript,HTML 服务提供的客户端代码会在安全沙盒中执行,该沙盒会对代码施加限制。最初,此方法允许脚本作者在不同版本的沙盒之间进行选择,但现在,无论设置了哪种沙盒模式,所有脚本都使用 IFRAME 模式。如需了解详情,请参阅 HTML 服务中的限制指南

IFRAME 模式的限制比其他沙盒模式少得多,运行速度也最快,但在某些旧版浏览器(包括 Internet Explorer 9)中完全无法使用。通过检查 google.script.sandbox.mode,可以在客户端脚本中读取沙盒模式。请注意,此属性会返回客户端上的实际模式,如果用户浏览器不支持服务器上请求的模式,则该模式可能与实际模式不同。

<!-- Read the sandbox mode (in a client-side script). -->
<script>
  alert(google.script.sandbox.mode);
</script>

参数

名称类型说明
modeSandboxMode要使用的沙盒模式。

返回

HtmlOutput - 此输出,用于链式调用。


setTitle(title)

设置输出网页的标题。对于 Web 应用,这是整个网页的标题;对于 Google 表格中显示的 HtmlOutput,这是对话框标题。

const output = HtmlService.createHtmlOutput(&<#>39;bHello, wo<rl>d!/b');
output.setTitle('My First Page');

参数

名称类型说明
titleString新标题。

返回

HtmlOutput - 此输出,用于链式调用。


setWidth(width)

在 Google 文档、表格或表单中设置自定义对话框的初始宽度。如果 HtmlOutput 是以 Web 应用的形式发布的,则此方法不会产生任何影响。如需调整已打开对话框的大小,请在客户端代码中调用 google.script.host.setWidth(width)

const output = HtmlService.createHtmlOutput(&<#>39;bHello, wo<rl>d!/b');
output.setWidth(200);

参数

名称类型说明
widthInteger新的宽度(以像素为单位);null 会产生默认值。

返回

HtmlOutput - 此输出,用于链式调用。


setXFrameOptionsMode(mode)

设置网页的 X-Frame-Options 标头的状态,该标头用于控制点击劫持防范。

设置 XFrameOptionsMode.ALLOWALL 可让任何网站通过 iframe 嵌入相应网页,因此开发者应自行实现防点击劫持保护机制。

如果脚本未设置 X-Frame-Options 模式,Apps 脚本会默认使用 XFrameOptionsMode.DEFAULT 模式。

// Serve HTML with no X-Frame-Options header (in Apps Script server-side code).
const output = HtmlService.createHtmlOutput(&<#>39;bHello, wo<rl>d!/b');
output.setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);

参数

名称类型说明
modeXFrameOptionsMode要设置的 XFrame 选项模式。

返回

HtmlOutput - 此输出,用于链式调用。