Class TextOutput
קל לארגן דפים בעזרת אוספים
אפשר לשמור ולסווג תוכן על סמך ההעדפות שלך.
TextOutput
אובייקט TextOutput שאפשר להציג מסקריפט.
מטעמי אבטחה, סקריפטים לא יכולים להחזיר תוכן טקסט ישירות לדפדפן.
במקום זאת, הדפדפן ינותב לכתובת googleusercontent.com, שבה הוא יוצג ללא ניטרול או מניפולציה נוספים.
אפשר להחזיר תוכן טקסט באופן הבא:
function doGet() {
return ContentService.createTextOutput('hello world!');
}
יש גם שיטות להצגת JSON, RSS ו-XML כ-TextOutputs. אפשר לעיין בשיטות המתאימות ב-
ContentService
.
מסמכים מפורטים
append(addedContent)
הוספת תוכן חדש לתוכן שיוצג.
פרמטרים
שם | סוג | תיאור |
addedContent | String | התוכן שרוצים לצרף |
חזרה
TextOutput
– ה-TextOutput עצמו, שימושי לשרשור
clear()
ניקוי התוכן הנוכחי.
חזרה
TextOutput
– ה-TextOutput עצמו, שימושי לשרשור
downloadAsFile(filename)
הקוד הזה מורה לדפדפנים להוריד את התוכן הזה במקום להציג אותו.
דפדפנים מסוימים יתעלמו מההגדרה הזו. הגדרה של הערך הזה כ-null תחזיר אותו להתנהגות ברירת המחדל של הצגה במקום הורדה.
פרמטרים
שם | סוג | תיאור |
filename | String | שם הקובץ שרוצים להשתמש בו בדפדפן |
חזרה
TextOutput
– האובייקט TextOutput, שימושי לשרשור
זריקות
Error
– אם שם הקובץ מכיל תווים לא חוקיים
getContent()
הפונקציה מקבלת את התוכן שיוצג.
חזרה
String
– התוכן שיוצג
getFileName()
הפונקציה מחזירה את שם הקובץ שבו צריך להוריד את הקובץ הזה, או null אם צריך להציג אותו במקום להוריד אותו.
חזרה
String
– שם הקובץ
getMimeType()
אחזור של סוג ה-MIME שבו התוכן יוצג.
חזרה
MimeType
– סוג ה-MIME שבו הקובץ יוצג
setContent(content)
הגדרת התוכן שיוצג.
פרמטרים
שם | סוג | תיאור |
content | String | התוכן להצגה |
חזרה
TextOutput
– ה-TextOutput עצמו, שימושי לשרשור
setMimeType(mimeType)
הגדרת סוג ה-MIME של התוכן שיוצג. ברירת המחדל היא טקסט פשוט.
פרמטרים
חזרה
TextOutput
– ה-TextOutput עצמו, שימושי לשרשור
אלא אם צוין אחרת, התוכן של דף זה הוא ברישיון Creative Commons Attribution 4.0 ודוגמאות הקוד הן ברישיון Apache 2.0. לפרטים, ניתן לעיין במדיניות האתר Google Developers. Java הוא סימן מסחרי רשום של חברת Oracle ו/או של השותפים העצמאיים שלה.
עדכון אחרון: 2025-07-26 (שעון UTC).
[null,null,["עדכון אחרון: 2025-07-26 (שעון UTC)."],[[["\u003cp\u003eA \u003ccode\u003eTextOutput\u003c/code\u003e object enables scripts to serve text content securely by redirecting the browser to \u003ccode\u003egoogleusercontent.com\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eIt offers methods to serve various content types such as JSON, RSS, and XML using the \u003ccode\u003eContentService\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eDevelopers can manipulate the output using methods like \u003ccode\u003eappend\u003c/code\u003e, \u003ccode\u003eclear\u003c/code\u003e, \u003ccode\u003edownloadAsFile\u003c/code\u003e, \u003ccode\u003egetContent\u003c/code\u003e, \u003ccode\u003egetFileName\u003c/code\u003e, \u003ccode\u003egetMimeType\u003c/code\u003e, \u003ccode\u003esetContent\u003c/code\u003e, and \u003ccode\u003esetMimeType\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003eTextOutput\u003c/code\u003e enhances security by preventing direct return of text content to the browser from scripts.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003edownloadAsFile\u003c/code\u003e method enables content to be downloaded instead of displayed, offering flexibility in how the output is handled.\u003c/p\u003e\n"]]],["TextOutput objects allow scripts to serve text content, which browsers access via redirection to googleusercontent.com. Key actions include: `append()` to add content, `clear()` to erase content, `downloadAsFile()` to trigger downloads, `getContent()` to retrieve content, `getFileName()` and `getMimeType()` to get metadata. `setContent()` sets the content, and `setMimeType()` specifies the content type. Scripts can use it to create outputs in text, JSON, RSS, and XML formats.\n"],null,["# Class TextOutput\n\nTextOutput\n\nA TextOutput object that can be served from a script.\n\nDue to security considerations, scripts cannot directly return text content to a browser.\nInstead, the browser is redirected to googleusercontent.com, which will display it without any\nfurther sanitization or manipulation.\n\nYou can return text content like this:\n\n```javascript\nfunction doGet() {\n return ContentService.createTextOutput('hello world!');\n}\n```\nThere are also methods to serve JSON, RSS, and XML as TextOutputs; see the corresponding methods on [ContentService](/apps-script/reference/content/content-service). \n\n### Methods\n\n| Method | Return type | Brief description |\n|-----------------------------------------------------|------------------------------------------------------|-----------------------------------------------------------------------------------------------------------|\n| [append(addedContent)](#append(String)) | [TextOutput](#) | Appends new content to the content that will be served. |\n| [clear()](#clear()) | [TextOutput](#) | Clears the current content. |\n| [downloadAsFile(filename)](#downloadAsFile(String)) | [TextOutput](#) | Tells browsers to download rather than display this content. |\n| [getContent()](#getContent()) | `String` | Gets the content that will be served. |\n| [getFileName()](#getFileName()) | `String` | Returns the file name to download this file as, or null if it should be displayed rather than downloaded. |\n| [getMimeType()](#getMimeType()) | [MimeType](/apps-script/reference/content/mime-type) | Get the mime type this content will be served with. |\n| [setContent(content)](#setContent(String)) | [TextOutput](#) | Sets the content that will be served. |\n| [setMimeType(mimeType)](#setMimeType(MimeType)) | [TextOutput](#) | Sets the mime type for content that will be served. |\n\nDetailed documentation\n----------------------\n\n### `append(addedContent)`\n\nAppends new content to the content that will be served.\n\n#### Parameters\n\n| Name | Type | Description |\n|------------------|----------|-----------------------|\n| `added``Content` | `String` | the content to append |\n\n#### Return\n\n\n[TextOutput](#) --- this TextOutput itself, useful for chaining\n\n*** ** * ** ***\n\n### `clear()`\n\nClears the current content.\n\n#### Return\n\n\n[TextOutput](#) --- this TextOutput itself, useful for chaining\n\n*** ** * ** ***\n\n### `download``As``File(filename)`\n\nTells browsers to download rather than display this content.\n\nSome browsers will ignore this setting. Setting this to null will clear it back to the\ndefault behavior of displaying rather than downloading.\n\n#### Parameters\n\n| Name | Type | Description |\n|------------|----------|-----------------------------------------|\n| `filename` | `String` | the filename to tell the browser to use |\n\n#### Return\n\n\n[TextOutput](#) --- the TextOutput object, useful for chaining\n\n#### Throws\n\n\n[`Error`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error) --- if the filename contains illegal characters\n\n*** ** * ** ***\n\n### `get``Content()`\n\nGets the content that will be served.\n\n#### Return\n\n\n`String` --- the content that will be served\n\n*** ** * ** ***\n\n### `get``File``Name()`\n\nReturns the file name to download this file as, or null if it should be displayed rather than\ndownloaded.\n\n#### Return\n\n\n`String` --- the file name\n\n*** ** * ** ***\n\n### `get``Mime``Type()`\n\nGet the mime type this content will be served with.\n\n#### Return\n\n\n[MimeType](/apps-script/reference/content/mime-type) --- the mime type this will be served with\n\n*** ** * ** ***\n\n### `set``Content(content)`\n\nSets the content that will be served.\n\n#### Parameters\n\n| Name | Type | Description |\n|-----------|----------|----------------------|\n| `content` | `String` | the content to serve |\n\n#### Return\n\n\n[TextOutput](#) --- this TextOutput itself, useful for chaining\n\n*** ** * ** ***\n\n### `set``Mime``Type(mimeType)`\n\nSets the mime type for content that will be served. The default is plain text.\n\n#### Parameters\n\n| Name | Type | Description |\n|--------------|------------------------------------------------------|---------------|\n| `mime``Type` | [MimeType](/apps-script/reference/content/mime-type) | the mime type |\n\n#### Return\n\n\n[TextOutput](#) --- this TextOutput itself, useful for chaining"]]