Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
HtmlService
Dịch vụ trả về HTML và nội dung văn bản khác từ một tập lệnh.
Do các cân nhắc về bảo mật, tập lệnh không thể trực tiếp trả về nội dung cho trình duyệt. Thay vào đó, các trình duyệt phải dọn dẹp HTML để không thể thực hiện các hành động độc hại. Hãy xem phần mô tả về HtmlOutput để biết những giới hạn liên quan đến nội dung có thể được trả về.
Lấy chuỗi tác nhân người dùng cho trình duyệt hiện tại. Trả về null cho hầu hết các lần thực thi tập lệnh nếu không được sử dụng trong hàm doGet() hoặc doPost() của ứng dụng web.
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["Cập nhật lần gần đây nhất: 2025-07-26 UTC."],[[["\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,["HtmlService\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\nProperties\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\nMethods\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`create``Html``Output()` \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\nReturn\n\n\n[HtmlOutput](/apps-script/reference/html/html-output) --- the new HtmlOutput object\n\n*** ** * ** ***\n\n`create``Html``Output(blob)` \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\nParameters\n\n| Name | Type | Description |\n|--------|----------------------------------------|-------------------------------|\n| `blob` | [BlobSource](../base/blob-source.html) | the object to get HTML out of |\n\nReturn\n\n\n[HtmlOutput](/apps-script/reference/html/html-output) --- the new `Html``Output` object\n\nThrows\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)` \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\nParameters\n\n| Name | Type | Description |\n|--------|----------|----------------------|\n| `html` | `String` | the content to serve |\n\nReturn\n\n\n[HtmlOutput](/apps-script/reference/html/html-output) --- the new HtmlOutput object\n\nThrows\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)` \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\nParameters\n\n| Name | Type | Description |\n|------------|----------|-----------------------------|\n| `filename` | `String` | the name of the file to use |\n\nReturn\n\n\n[HtmlOutput](/apps-script/reference/html/html-output) --- the new `Html``Output` object\n\nThrows\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)` \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\nParameters\n\n| Name | Type | Description |\n|--------|----------------------------------------|--------------------------------|\n| `blob` | [BlobSource](../base/blob-source.html) | The object to get HTML out of. |\n\nReturn\n\n\n[HtmlTemplate](/apps-script/reference/html/html-template) --- the new `Html``Template` object\n\nThrows\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)` \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\nParameters\n\n| Name | Type | Description |\n|--------|----------|-----------------------------|\n| `html` | `String` | the content of the template |\n\nReturn\n\n\n[HtmlTemplate](/apps-script/reference/html/html-template) --- the new `Html``Template` object\n\n*** ** * ** ***\n\n`create``Template``From``File(filename)` \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\nParameters\n\n| Name | Type | Description |\n|------------|----------|-----------------------------|\n| `filename` | `String` | the name of the file to use |\n\nReturn\n\n\n[HtmlTemplate](/apps-script/reference/html/html-template) --- the new `Html``Template` object\n\nThrows\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()` \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\nReturn\n\n\n`String` --- the user-agent string"]]