Class ContentService
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.
Dịch vụnội dung
Dịch vụ trả về nội dung văn bản từ một tập lệnh.
Bạn có thể phân phát văn bản ở nhiều dạng. Ví dụ: xuất bản tập lệnh này dưới dạng ứng dụng web.
function doGet() {
return ContentService.createTextOutput('Hello World');
}
Khi truy cập vào URL ứng dụng web, bạn sẽ thấy "Hello World" ("Xin chào thế giới") trong trình duyệt. 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 trang web đó phải phân phát nội dung từ một URL khác. Đây là lý do khiến URL trong trình duyệt cho đầu ra được tạo thông qua ứng dụng web này sẽ khác với URL tập lệnh ban đầu.
Tài liệu chi tiết
createTextOutput()
Tạo một đối tượng TextOutput
mới.
function doGet() {
const output = ContentService.createTextOutput();
output.append('Hello world!');
return output;
}
Cầu thủ trả bóng
TextOutput
– đối tượng TextOutput mới.
createTextOutput(content)
Tạo một đối tượng TextOutput
mới có thể phân phát nội dung đã cho.
function doGet() {
const output = ContentService.createTextOutput('Hello world!');
return output;
}
Tham số
Tên | Loại | Mô tả |
content | String | nội dung cần phân phát. |
Cầu thủ trả bóng
TextOutput
– đối tượng TextOutput mới.
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\u003eContentService allows scripts to return text content, which can be served as a web app.\u003c/p\u003e\n"],["\u003cp\u003eDue to security, content is served from a different URL than the original script URL.\u003c/p\u003e\n"],["\u003cp\u003eIt provides methods like \u003ccode\u003ecreateTextOutput()\u003c/code\u003e to generate and manipulate text output objects.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003ecreateTextOutput()\u003c/code\u003e can be used with or without initial content, enabling flexible content creation.\u003c/p\u003e\n"]]],[],null,["# Class ContentService\n\nContentService\n\nService for returning text content from a script.\n\nYou can serve up text in various forms. For example, publish this script as a web app.\n\n```javascript\nfunction doGet() {\n return ContentService.createTextOutput('Hello World');\n}\n```\nWhen visiting the web app URL you will see \"Hello World\" in the browser. Due to security considerations, scripts cannot directly return content to a browser. Instead, they must serve the content from a different URL. This is why the URL in the browser for output created via this web app will be different than the original script URL. \n\n### Properties\n\n| Property | Type | Description |\n|--------------|------------------------------------------------------|-------------|\n| `Mime``Type` | [MimeType](/apps-script/reference/content/mime-type) | |\n\n### Methods\n\n| Method | Return type | Brief description |\n|--------------------------------------------------------|----------------------------------------------------------|----------------------------------------------------------------------------------------------------------------|\n| [createTextOutput()](#createTextOutput()) | [TextOutput](/apps-script/reference/content/text-output) | Create a new [TextOutput](/apps-script/reference/content/text-output) object. |\n| [createTextOutput(content)](#createTextOutput(String)) | [TextOutput](/apps-script/reference/content/text-output) | Create a new [TextOutput](/apps-script/reference/content/text-output) object that can serve the given content. |\n\nDetailed documentation\n----------------------\n\n### `create``Text``Output()`\n\nCreate a new [TextOutput](/apps-script/reference/content/text-output) object.\n\n```javascript\nfunction doGet() {\n const output = ContentService.createTextOutput();\n output.append('Hello world!');\n return output;\n}\n```\n\n#### Return\n\n\n[TextOutput](/apps-script/reference/content/text-output) --- the new TextOutput object.\n\n*** ** * ** ***\n\n### `create``Text``Output(content)`\n\nCreate a new [TextOutput](/apps-script/reference/content/text-output) object that can serve the given content.\n\n```javascript\nfunction doGet() {\n const output = ContentService.createTextOutput('Hello world!');\n return output;\n}\n```\n\n#### Parameters\n\n| Name | Type | Description |\n|-----------|----------|-----------------------|\n| `content` | `String` | the content to serve. |\n\n#### Return\n\n\n[TextOutput](/apps-script/reference/content/text-output) --- the new TextOutput object."]]