Class HtmlService
تنظيم صفحاتك في مجموعات
يمكنك حفظ المحتوى وتصنيفه حسب إعداداتك المفضّلة.
HtmlService
خدمة لعرض محتوى HTML ومحتوى نصي آخر من نص برمجي
لا يمكن للنصوص البرمجية عرض المحتوى مباشرةً في المتصفّح بسبب اعتبارات الأمان. بدلاً من ذلك،
يجب تطهير صفحات HTML كي لا تتمكّن من تنفيذ إجراءات ضارة. اطّلِع على وصف HtmlOutput
لمعرفة القيود المفروضة على البيانات التي يمكن عرضها.
أماكن إقامة
الموقع | النوع | الوصف |
SandboxMode | SandboxMode | فهرس يمثّل أوضاع وضع الحماية التي يمكن استخدامها HtmlService
للنصوص البرمجية من جهة العميل |
XFrameOptionsMode | XFrameOptionsMode | فهرس يمثّل أوضاع X-Frame-Options التي يمكن استخدامها لنصوص HtmlService البرمجية من جهة العميل |
مستندات تفصيلية
createHtmlOutput()
لإنشاء عنصر HtmlOutput
جديد يمكن إرجاعه من النص البرمجي.
const output = HtmlService.createHtmlOutput();
الإرجاع
HtmlOutput
: عنصر HtmlOutput الجديد
createHtmlOutput(blob)
لإنشاء عنصر HtmlOutput
جديد من مورد BlobSource
function createFromBlob(blob) {
const output = HtmlService.createHtmlOutput(blob);
return output;
}
المعلمات
الاسم | النوع | الوصف |
blob | BlobSource | العنصر الذي يتم استخراج HTML منه |
الإرجاع
HtmlOutput
: عنصر HtmlOutput
الجديد
عمليات الرمي
Error
- إذا لم تحتوي الكتلة على محتوى HTML أو كان تنسيق HTML غير صحيح
createHtmlOutput(html)
لإنشاء عنصر HtmlOutput
جديد يمكن إرجاعه من النص البرمجي.
const output = HtmlService.createHtmlOutput('<b>Hello world!</b>');
المعلمات
الاسم | النوع | الوصف |
html | String | المحتوى المطلوب عرضه |
الإرجاع
HtmlOutput
: عنصر HtmlOutput الجديد
عمليات الرمي
Error
- إذا كان رمز HTML غير صالح
createHtmlOutputFromFile(filename)
لإنشاء عنصر HtmlOutput
جديد من ملف في محرِّر الرموز البرمجية
const output = HtmlService.createHtmlOutputFromFile('myPage');
المعلمات
الاسم | النوع | الوصف |
filename | String | اسم الملف المطلوب استخدامه |
الإرجاع
HtmlOutput
: عنصر HtmlOutput
الجديد
عمليات الرمي
Error
- إذا لم يتم العثور على الملف أو إذا كان ملف HTML فيه غير صالح
createTemplate(blob)
لإنشاء عنصر HtmlTemplate
جديد من مورد BlobSource
function createFromBlob(blob) {
const template = HtmlService.createTemplate(blob);
const output = template.evaluate();
return output;
}
المعلمات
الاسم | النوع | الوصف |
blob | BlobSource | العنصر الذي يتم استخراج HTML منه |
الإرجاع
HtmlTemplate
: عنصر HtmlTemplate
الجديد
عمليات الرمي
Error
- إذا لم تحتوي السلسلة على محتوى HTML
createTemplate(html)
لإنشاء عنصر HtmlTemplate
جديد يمكن إرجاعه من النص البرمجي.
const template = HtmlService.createTemplate(
'<b>The time is <?= new Date() ?></b>',
);
المعلمات
الاسم | النوع | الوصف |
html | String | محتوى النموذج |
الإرجاع
HtmlTemplate
: عنصر HtmlTemplate
الجديد
createTemplateFromFile(filename)
لإنشاء عنصر HtmlTemplate
جديد من ملف في محرِّر الرموز البرمجية
const template = HtmlService.createTemplateFromFile('myTemplate');
المعلمات
الاسم | النوع | الوصف |
filename | String | اسم الملف المطلوب استخدامه |
الإرجاع
HtmlTemplate
: عنصر HtmlTemplate
الجديد
عمليات الرمي
Error
- إذا لم يتم العثور على الملف
getUserAgent()
تحصل على سلسلة وكيل المستخدم للمتصفّح الحالي. تعرِض هذه الدالة القيمة null
لمعظم عمليات تنفيذ النصوص البرمجية في حال عدم استخدامها في دالة doGet()
أو doPost()
في تطبيق الويب.
الإرجاع
String
: سلسلة وكيل المستخدم
إنّ محتوى هذه الصفحة مرخّص بموجب ترخيص Creative Commons Attribution 4.0 ما لم يُنصّ على خلاف ذلك، ونماذج الرموز مرخّصة بموجب ترخيص Apache 2.0. للاطّلاع على التفاصيل، يُرجى مراجعة سياسات موقع Google Developers. إنّ Java هي علامة تجارية مسجَّلة لشركة Oracle و/أو شركائها التابعين.
تاريخ التعديل الأخير: 2025-07-26 (حسب التوقيت العالمي المتفَّق عليه)
[null,null,["تاريخ التعديل الأخير: 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"]]