Class HtmlService
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
HtmlService
用于从脚本返回 HTML 和其他文本内容的服务。
出于安全考虑,脚本无法直接将内容返回给浏览器。相反,他们必须对 HTML 进行排错,以免其执行恶意操作。请参阅 HtmlOutput
的说明,了解这对可返回的内容有何限制。
属性
属性 | 类型 | 说明 |
SandboxMode | SandboxMode | 一个枚举,表示可用于客户端 HtmlService 脚本的沙盒模式。 |
XFrameOptionsMode | XFrameOptionsMode | 一个枚举,表示可用于客户端 HtmlService 脚本的 X-Frame-Options 模式。 |
详细文档
createHtmlOutput()
创建一个可从脚本返回的新 HtmlOutput
对象。
const output = HtmlService.createHtmlOutput();
返回
HtmlOutput
- 新的 HtmlOutput 对象
createHtmlOutput(blob)
基于 BlobSource
资源创建新的 HtmlOutput
对象。
function createFromBlob(blob) {
const output = HtmlService.createHtmlOutput(blob);
return output;
}
参数
返回
HtmlOutput
- 新的 HtmlOutput
对象
抛出
Error
- 如果 blob 不包含 HTML 或 HTML 格式有误
createHtmlOutput(html)
创建一个可从脚本返回的新 HtmlOutput
对象。
const output = HtmlService.createHtmlOutput('<b>Hello world!</b>');
参数
返回
HtmlOutput
- 新的 HtmlOutput 对象
抛出
Error
- 如果 HTML 格式有误
createHtmlOutputFromFile(filename)
在代码编辑器中根据文件创建新的 HtmlOutput
对象。
const output = HtmlService.createHtmlOutputFromFile('myPage');
参数
名称 | 类型 | 说明 |
filename | String | 要使用的文件的名称 |
返回
HtmlOutput
- 新的 HtmlOutput
对象
抛出
Error
- 如果找不到文件或其中的 HTML 格式有误
createTemplate(blob)
基于 BlobSource
资源创建新的 HtmlTemplate
对象。
function createFromBlob(blob) {
const template = HtmlService.createTemplate(blob);
const output = template.evaluate();
return output;
}
参数
返回
HtmlTemplate
- 新的 HtmlTemplate
对象
抛出
Error
- 如果 blob 不包含 HTML
createTemplate(html)
创建一个可从脚本返回的新 HtmlTemplate
对象。
const template = HtmlService.createTemplate(
'<b>The time is <?= new Date() ?></b>',
);
参数
返回
HtmlTemplate
- 新的 HtmlTemplate
对象
createTemplateFromFile(filename)
在代码编辑器中根据文件创建新的 HtmlTemplate
对象。
const template = HtmlService.createTemplateFromFile('myTemplate');
参数
名称 | 类型 | 说明 |
filename | String | 要使用的文件的名称 |
返回
HtmlTemplate
- 新的 HtmlTemplate
对象
抛出
Error
- 如果找不到文件
getUserAgent()
获取当前浏览器的用户代理字符串。如果未在 Web 应用的 doGet()
或 doPost()
函数中使用,则对于大多数脚本执行都会返回 null
。
返回
String
- 用户代理字符串
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-26。
[null,null,["最后更新时间 (UTC):2025-07-26。"],[[["\u003cp\u003eHtmlService enables scripts to safely return HTML and other text content to a browser by sanitizing the output to prevent malicious actions.\u003c/p\u003e\n"],["\u003cp\u003eIt offers methods to create HTML output from strings, files, or blobs, and supports templates for dynamic content generation.\u003c/p\u003e\n"],["\u003cp\u003eHtmlService provides control over sandbox modes and \u003ccode\u003eX-Frame-Options\u003c/code\u003e for enhanced security.\u003c/p\u003e\n"],["\u003cp\u003eDevelopers can utilize HtmlService to build user interfaces and web applications within Google Apps Script.\u003c/p\u003e\n"]]],["HtmlService allows scripts to return sanitized HTML content to browsers, preventing malicious actions. Key actions include creating `HtmlOutput` objects from strings, `BlobSource` resources, or files via `createHtmlOutput()` and `createHtmlOutputFromFile()`. Similarly, `HtmlTemplate` objects can be created using `createTemplate()` and `createTemplateFromFile()` for dynamic content. The `getUserAgent()` method retrieves the current browser's user-agent string. There are `SandboxMode` and `XFrameOptionsMode` properties that can be used for client-side `HtmlService` scripts.\n"],null,["# Class HtmlService\n\nHtmlService\n\nService for returning HTML and other text content from a script.\n\nDue to security considerations, scripts cannot directly return content to a browser. Instead,\nthey must sanitize the HTML so that it cannot perform malicious actions. See the description of\n[HtmlOutput](/apps-script/reference/html/html-output) for what limitations this implies on what can be returned. \n\n### Properties\n\n| Property | Type | Description |\n|---------------------|-----------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------|\n| `Sandbox``Mode` | [SandboxMode](/apps-script/reference/html/sandbox-mode) | An enum representing the sandbox modes that can be used for client-side `Html``Service` scripts. |\n| `XFrameOptionsMode` | [XFrameOptionsMode](/apps-script/reference/html/x-frame-options-mode) | An enum representing the `X-Frame-Options` modes that can be used for client-side `Html``Service` scripts. |\n\n### Methods\n\n| Method | Return type | Brief description |\n|-------------------------------------------------------------------------|-----------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------|\n| [createHtmlOutput()](#createHtmlOutput()) | [HtmlOutput](/apps-script/reference/html/html-output) | Creates a new [HtmlOutput](/apps-script/reference/html/html-output) object that can be returned from the script. |\n| [createHtmlOutput(blob)](#createHtmlOutput(BlobSource)) | [HtmlOutput](/apps-script/reference/html/html-output) | Creates a new [HtmlOutput](/apps-script/reference/html/html-output) object from a [BlobSource](../base/blob-source.html) resource. |\n| [createHtmlOutput(html)](#createHtmlOutput(String)) | [HtmlOutput](/apps-script/reference/html/html-output) | Creates a new [HtmlOutput](/apps-script/reference/html/html-output) object that can be returned from the script. |\n| [createHtmlOutputFromFile(filename)](#createHtmlOutputFromFile(String)) | [HtmlOutput](/apps-script/reference/html/html-output) | Creates a new [HtmlOutput](/apps-script/reference/html/html-output) object from a file in the code editor. |\n| [createTemplate(blob)](#createTemplate(BlobSource)) | [HtmlTemplate](/apps-script/reference/html/html-template) | Creates a new [HtmlTemplate](/apps-script/reference/html/html-template) object from a [BlobSource](../base/blob-source.html) resource. |\n| [createTemplate(html)](#createTemplate(String)) | [HtmlTemplate](/apps-script/reference/html/html-template) | Creates a new [HtmlTemplate](/apps-script/reference/html/html-template) object that can be returned from the script. |\n| [createTemplateFromFile(filename)](#createTemplateFromFile(String)) | [HtmlTemplate](/apps-script/reference/html/html-template) | Creates a new [HtmlTemplate](/apps-script/reference/html/html-template) object from a file in the code editor. |\n| [getUserAgent()](#getUserAgent()) | `String` | Gets the user-agent string for the current browser. |\n\nDetailed documentation\n----------------------\n\n### `create``Html``Output()`\n\nCreates a new [HtmlOutput](/apps-script/reference/html/html-output) object that can be returned from the script.\n\n```javascript\nconst output = HtmlService.createHtmlOutput();\n```\n\n#### Return\n\n\n[HtmlOutput](/apps-script/reference/html/html-output) --- the new HtmlOutput object\n\n*** ** * ** ***\n\n### `create``Html``Output(blob)`\n\nCreates a new [HtmlOutput](/apps-script/reference/html/html-output) object from a [BlobSource](../base/blob-source.html) resource.\n\n```javascript\nfunction createFromBlob(blob) {\n const output = HtmlService.createHtmlOutput(blob);\n return output;\n}\n```\n\n#### Parameters\n\n| Name | Type | Description |\n|--------|----------------------------------------|-------------------------------|\n| `blob` | [BlobSource](../base/blob-source.html) | the object to get HTML out of |\n\n#### Return\n\n\n[HtmlOutput](/apps-script/reference/html/html-output) --- the new `Html``Output` object\n\n#### Throws\n\n\n[`Error`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error) --- if the blob doesn't contain HTML or the HTML is malformed\n\n*** ** * ** ***\n\n### `create``Html``Output(html)`\n\nCreates a new [HtmlOutput](/apps-script/reference/html/html-output) object that can be returned from the script.\n\n```javascript\nconst output = HtmlService.createHtmlOutput('\u003cb\u003eHello world!\u003c/b\u003e');\n```\n\n#### Parameters\n\n| Name | Type | Description |\n|--------|----------|----------------------|\n| `html` | `String` | the content to serve |\n\n#### Return\n\n\n[HtmlOutput](/apps-script/reference/html/html-output) --- the new HtmlOutput object\n\n#### Throws\n\n\n[`Error`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error) --- if the html is malformed\n\n*** ** * ** ***\n\n### `create``Html``Output``From``File(filename)`\n\nCreates a new [HtmlOutput](/apps-script/reference/html/html-output) object from a file in the code editor.\n\n```javascript\nconst output = HtmlService.createHtmlOutputFromFile('myPage');\n```\n\n#### Parameters\n\n| Name | Type | Description |\n|------------|----------|-----------------------------|\n| `filename` | `String` | the name of the file to use |\n\n#### Return\n\n\n[HtmlOutput](/apps-script/reference/html/html-output) --- the new `Html``Output` object\n\n#### Throws\n\n\n[`Error`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error) --- if the file wasn't found or the HTML in it is malformed\n\n*** ** * ** ***\n\n### `create``Template(blob)`\n\nCreates a new [HtmlTemplate](/apps-script/reference/html/html-template) object from a [BlobSource](../base/blob-source.html) resource.\n\n```javascript\nfunction createFromBlob(blob) {\n const template = HtmlService.createTemplate(blob);\n const output = template.evaluate();\n return output;\n}\n```\n\n#### Parameters\n\n| Name | Type | Description |\n|--------|----------------------------------------|--------------------------------|\n| `blob` | [BlobSource](../base/blob-source.html) | The object to get HTML out of. |\n\n#### Return\n\n\n[HtmlTemplate](/apps-script/reference/html/html-template) --- the new `Html``Template` object\n\n#### Throws\n\n\n[`Error`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error) --- if the blob doesn't contain HTML\n\n*** ** * ** ***\n\n### `create``Template(html)`\n\nCreates a new [HtmlTemplate](/apps-script/reference/html/html-template) object that can be returned from the script.\n\n```javascript\nconst template = HtmlService.createTemplate(\n '\u003cb\u003eThe time is <?= new Date() ?>\u003c/b\u003e',\n);\n```\n\n#### Parameters\n\n| Name | Type | Description |\n|--------|----------|-----------------------------|\n| `html` | `String` | the content of the template |\n\n#### Return\n\n\n[HtmlTemplate](/apps-script/reference/html/html-template) --- the new `Html``Template` object\n\n*** ** * ** ***\n\n### `create``Template``From``File(filename)`\n\nCreates a new [HtmlTemplate](/apps-script/reference/html/html-template) object from a file in the code editor.\n\n```javascript\nconst template = HtmlService.createTemplateFromFile('myTemplate');\n```\n\n#### Parameters\n\n| Name | Type | Description |\n|------------|----------|-----------------------------|\n| `filename` | `String` | the name of the file to use |\n\n#### Return\n\n\n[HtmlTemplate](/apps-script/reference/html/html-template) --- the new `Html``Template` object\n\n#### Throws\n\n\n[`Error`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error) --- if the file wasn't found\n\n*** ** * ** ***\n\n### `get``User``Agent()`\n\nGets the user-agent string for the current browser. Returns `null` for most script\nexecutions if not used in a web app's `do``Get()` or `do``Post()` function.\n\n#### Return\n\n\n`String` --- the user-agent string"]]