Class HtmlTemplate
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.
Mẫu Html
Đối tượng mẫu để tạo HTML một cách linh động. Để biết thêm thông tin, hãy xem hướng dẫn về mẫu.
Phương thức
Phương thức | Loại dữ liệu trả về | Mô tả ngắn |
evaluate() | HtmlOutput | Đánh giá mẫu này và trả về một đối tượng HtmlOutput . |
getCode() | String | Tạo một chuỗi mã JavaScript, dựa trên tệp mẫu, có thể được đánh giá. |
getCodeWithComments() | String | Tạo một chuỗi mã JavaScript có thể được đánh giá, trong đó mỗi dòng mã chứa dòng ban đầu từ mẫu dưới dạng một nhận xét. |
getRawContent() | String | Trả về nội dung chưa được xử lý của mẫu này. |
Tài liệu chi tiết
evaluate()
Đánh giá mẫu này và trả về một đối tượng HtmlOutput
. Mọi thuộc tính được đặt trên đối tượng HtmlTemplate
này sẽ nằm trong phạm vi khi đánh giá. Để gỡ lỗi trong mẫu, hãy kiểm tra mã bằng phương thức getCode()
.
// A template which evaluates to whatever is bound to 'foo'.
const template = HtmlService.createTemplate('<?= foo ?>');
template.foo = 'Hello World!';
Logger.log(template.evaluate().getContent()); // will log 'Hello World!'
Cầu thủ trả bóng
HtmlOutput
– một đối tượng HtmlOutput
getCode()
Tạo một chuỗi mã JavaScript, dựa trên tệp mẫu, có thể được đánh giá. Phương thức này tạo một chuỗi mã JavaScript dựa trên tệp mẫu. Việc gọi
eval(<code>)
sẽ trả về một đối tượng HtmlOutput
mới có nội dung của mẫu sau khi chạy tất cả tập lệnh máy chủ được nhúng. Mã được tạo ra để người dùng có thể đọc được. Vì vậy, nếu cần gỡ lỗi mẫu, bạn có thể gọi
Logger.log(<code>)
để xem kết quả.
Việc đánh giá mã này sẽ liên kết ngầm trong tất cả các biến trong phạm vi hiện tại. Nhìn chung, bạn nên sử dụng phương thức evaluate()
. Phương thức này sẽ lấy các liên kết rõ ràng.
const template = HtmlService.createTemplate(
'<b>The time is <?= new Date() ?></b>',
);
Logger.log(template.getCode());
Cầu thủ trả bóng
String
– một chuỗi dựa trên mẫu có thể được đánh giá
getRawContent()
Trả về nội dung chưa được xử lý của mẫu này.
const template = HtmlService.createTemplate(
'<b>The time is <?= new Date() ?></b>',
);
Logger.log(template.getRawContent());
Cầu thủ trả bóng
String
– nội dung thô của mẫu
Trừ phi có lưu ý khác, nội dung của trang này được cấp phép theo Giấy phép ghi nhận tác giả 4.0 của Creative Commons và các mẫu mã lập trình được cấp phép theo Giấy phép Apache 2.0. Để biết thông tin chi tiết, vui lòng tham khảo Chính sách trang web của Google Developers. Java là nhãn hiệu đã đăng ký của Oracle và/hoặc các đơn vị liên kết với Oracle.
Cập nhật lần gần đây nhất: 2025-07-26 UTC.
[null,null,["Cập nhật lần gần đây nhất: 2025-07-26 UTC."],[[["\u003cp\u003e\u003ccode\u003eHtmlTemplate\u003c/code\u003e helps you dynamically build HTML content within Apps Script.\u003c/p\u003e\n"],["\u003cp\u003eIt provides methods like \u003ccode\u003eevaluate()\u003c/code\u003e to render HTML with data, and \u003ccode\u003egetCode()\u003c/code\u003e to generate the underlying JavaScript.\u003c/p\u003e\n"],["\u003cp\u003eYou can use \u003ccode\u003egetRawContent()\u003c/code\u003e to access the original template content and \u003ccode\u003egetCodeWithComments()\u003c/code\u003e for debugging purposes.\u003c/p\u003e\n"],["\u003cp\u003eThe rendered output can be further handled using the \u003ccode\u003eHtmlOutput\u003c/code\u003e object.\u003c/p\u003e\n"]]],[],null,["# Class HtmlTemplate\n\nHtmlTemplate\n\nA template object for dynamically constructing HTML. For more information, see the [guide to templates](/apps-script/guides/html/templates). \n\n### Methods\n\n| Method | Return type | Brief description |\n|-------------------------------------------------|-------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------|\n| [evaluate()](#evaluate()) | [HtmlOutput](/apps-script/reference/html/html-output) | Evaluates this template and returns an [HtmlOutput](/apps-script/reference/html/html-output) object. |\n| [getCode()](#getCode()) | `String` | Generates a string of JavaScript code, based on the template file, that can be evaluated. |\n| [getCodeWithComments()](#getCodeWithComments()) | `String` | Generates a string of JavaScript code that can be evaluated, with each line of the code containing the original line from the template as a comment. |\n| [getRawContent()](#getRawContent()) | `String` | Returns the unprocessed content of this template. |\n\nDetailed documentation\n----------------------\n\n### `evaluate()`\n\nEvaluates this template and returns an [HtmlOutput](/apps-script/reference/html/html-output) object. Any properties set on this\n`Html``Template` object will be in scope when evaluating. To debug errors in a template,\nexamine the code using the [getCode()](#getCode()) method.\n\n```javascript\n// A template which evaluates to whatever is bound to 'foo'.\nconst template = HtmlService.createTemplate('\u003c?= foo ?\u003e');\ntemplate.foo = 'Hello World!';\nLogger.log(template.evaluate().getContent()); // will log 'Hello World!'\n```\n\n#### Return\n\n\n[HtmlOutput](/apps-script/reference/html/html-output) --- an HtmlOutput object\n\n*** ** * ** ***\n\n### `get``Code()`\n\nGenerates a string of JavaScript code, based on the template file, that can be evaluated. This\nmethod produces a string of JavaScript code based on the template file. Calling `\neval(\u003ccode\u003e)` will return a new [HtmlOutput](/apps-script/reference/html/html-output) object with the content of the\ntemplate after running all embedded server scripts. The generated code is intended to be\nhuman-readable, and so if you need to debug a template you can call `\nLogger.log(\u003ccode\u003e)` to see what was produced.\n\nEvaluating this code will implicitly bind in all variables in the current scope. In general,\nit's preferable to use the [evaluate()](#evaluate()) method, which takes explicit bindings.\n\n```javascript\nconst template = HtmlService.createTemplate(\n '\u003cb\u003eThe time is <?= new Date() ?>\u003c/b\u003e',\n);\nLogger.log(template.getCode());\n```\n\n#### Return\n\n\n`String` --- a string based on the template, which can be evaluated\n\n*** ** * ** ***\n\n### `get``Code``With``Comments()`\n\nGenerates a string of JavaScript code that can be evaluated, with each line of the code\ncontaining the original line from the template as a comment. This method produces a string of\nJavaScript code based on the template file. Calling `eval(\u003ccode\u003e)` will return\na new [HtmlOutput](/apps-script/reference/html/html-output) object with the content of the template after running all embedded\nserver scripts. The generated code is intended to be human-readable, and so if you need to\ndebug a template you can call `Logger.log(\u003ccode\u003e)` to see what was produced.\n\nEvaluating this code will implicitly bind in all variables in the current scope. In general,\nit's preferable to use the [evaluate()](#evaluate()) method, which takes explicit bindings.\n\n```javascript\nconst template = HtmlService.createTemplate(\n '\u003cb\u003eThe time is <?= new Date() ?>\u003c/b\u003e',\n);\nLogger.log(template.getCodeWithComments());\n```\n\n#### Return\n\n\n`String` --- an string based on the template, which can be evaluated\n\n*** ** * ** ***\n\n### `get``Raw``Content()`\n\nReturns the unprocessed content of this template.\n\n```javascript\nconst template = HtmlService.createTemplate(\n '\u003cb\u003eThe time is <?= new Date() ?>\u003c/b\u003e',\n);\nLogger.log(template.getRawContent());\n```\n\n#### Return\n\n\n`String` --- the template's raw content"]]