Class HtmlService
با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
سرویس Html سرویس برای برگرداندن HTML و سایر محتوای متنی از یک اسکریپت.
به دلیل ملاحظات امنیتی، اسکریپت ها نمی توانند مستقیماً محتوا را به مرورگر بازگردانند. در عوض، آنها باید HTML را پاکسازی کنند تا نتواند اقدامات مخرب انجام دهد. توضیح Html Output
را ببینید که چه محدودیت هایی در مورد آنچه که می توان برگرداند وجود دارد.
خواص
اموال | تایپ کنید | توضیحات |
---|
Sandbox Mode | Sandbox Mode | فهرستی که حالتهای جعبه ایمنی را نشان میدهد که میتواند برای اسکریپتهای Html Service سمت سرویس گیرنده استفاده شود. |
XFrameOptionsMode | XFrameOptionsMode | فهرستی که حالتهای X-Frame-Options را نشان میدهد که میتواند برای اسکریپتهای Html Service سمت سرویس گیرنده استفاده شود. |
مستندات دقیق
create Html Output()
یک شیء Html Output
جدید ایجاد می کند که می تواند از اسکریپت برگردانده شود.
const output = HtmlService.createHtmlOutput();
بازگشت
Html Output
- شیء جدید HtmlOutput
create Html Output(blob)
یک شیء Html Output
جدید از یک منبع Blob Source
ایجاد می کند.
function createFromBlob(blob) {
const output = HtmlService.createHtmlOutput(blob);
return output;
}
پارامترها
نام | تایپ کنید | توضیحات |
---|
blob | Blob Source | شیئی که HTML از آن خارج می شود |
بازگشت
Html Output
- شیء Html Output
جدید
پرتاب می کند
Error
- اگر لکه حاوی HTML نباشد یا HTML بد شکل باشد
create Html Output(html)
یک شیء Html Output
جدید ایجاد می کند که می تواند از اسکریپت برگردانده شود.
const output = HtmlService.createHtmlOutput('<b>Hello world!</b>');
پارامترها
نام | تایپ کنید | توضیحات |
---|
html | String | محتوای مورد نظر برای ارائه |
بازگشت
Html Output
- شیء جدید HtmlOutput
پرتاب می کند
Error
- اگر html بد شکل باشد
create Html Output From File(filename)
یک شیء Html Output
جدید از یک فایل در ویرایشگر کد ایجاد می کند.
const output = HtmlService.createHtmlOutputFromFile('myPage');
پارامترها
نام | تایپ کنید | توضیحات |
---|
filename | String | نام فایل مورد استفاده |
بازگشت
Html Output
- شیء Html Output
جدید
پرتاب می کند
Error
- اگر فایل پیدا نشد یا HTML موجود در آن نادرست است
create Template(blob)
یک شیء Html Template
جدید از یک منبع Blob Source
ایجاد می کند.
function createFromBlob(blob) {
const template = HtmlService.createTemplate(blob);
const output = template.evaluate();
return output;
}
پارامترها
نام | تایپ کنید | توضیحات |
---|
blob | Blob Source | شیئی که HTML از آن خارج می شود. |
بازگشت
Html Template
- شیء Html Template
جدید
پرتاب می کند
Error
- اگر لکه حاوی HTML نباشد
create Template(html)
یک شیء Html Template
جدید ایجاد می کند که می تواند از اسکریپت برگردانده شود.
const template = HtmlService.createTemplate(
'<b>The time is <?= new Date() ?></b>',
);
پارامترها
نام | تایپ کنید | توضیحات |
---|
html | String | محتوای قالب |
بازگشت
Html Template
- شیء Html Template
جدید
create Template From File(filename)
یک شیء Html Template
جدید از یک فایل در ویرایشگر کد ایجاد می کند.
const template = HtmlService.createTemplateFromFile('myTemplate');
پارامترها
نام | تایپ کنید | توضیحات |
---|
filename | String | نام فایل مورد استفاده |
بازگشت
Html Template
- شیء Html Template
جدید
پرتاب می کند
Error
- اگر فایل پیدا نشد
get User Agent()
رشته user-agent را برای مرورگر فعلی دریافت می کند. اگر در تابع do Get()
یا do Post()
یک برنامه وب استفاده نشود، برای اکثر اجرای اسکریپت ها null
برمی گرداند.
بازگشت
String
- رشته عامل کاربر
جز در مواردی که غیر از این ذکر شده باشد،محتوای این صفحه تحت مجوز Creative Commons Attribution 4.0 License است. نمونه کدها نیز دارای مجوز Apache 2.0 License است. برای اطلاع از جزئیات، به خطمشیهای سایت Google Developers مراجعه کنید. جاوا علامت تجاری ثبتشده Oracle و/یا شرکتهای وابسته به آن است.
تاریخ آخرین بهروزرسانی 2025-07-24 بهوقت ساعت هماهنگ جهانی.
[null,null,["تاریخ آخرین بهروزرسانی 2025-07-24 بهوقت ساعت هماهنگ جهانی."],[[["\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"]]