Class TextOutput
文字輸出內容可透過指令碼提供的 TextOutput 物件。
基於安全考量,指令碼無法直接將文字內容傳回瀏覽器。相反地,瀏覽器會重新導向至 googleusercontent.com,該網站會直接顯示內容,不會進行任何進一步的清理或處理。
您可以傳回類似以下的文字內容:
function doGet() {
return ContentService.createTextOutput('hello world!');
}
此外,您也可以使用方法將 JSON、RSS 和 XML 做為 TextOutputs 提供;請參閱
ContentService
上的對應方法。
內容詳盡的說明文件
append(addedContent)
在即將放送的內容中附加新內容。
參數
名稱 | 類型 | 說明 |
addedContent | String | 要附加的內容 |
回攻員
TextOutput
:這個 TextOutput 本身,可用於鏈結
downloadAsFile(filename)
指示瀏覽器下載而非顯示此內容。
部分瀏覽器會忽略這項設定。如果將這個值設為空值,系統會將其清除,並改為預設的顯示行為,而不是下載。
參數
名稱 | 類型 | 說明 |
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 本身,可用於鏈結
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2024-12-22 (世界標準時間)。
[null,null,["上次更新時間:2024-12-22 (世界標準時間)。"],[[["A `TextOutput` object enables scripts to serve text content securely by redirecting the browser to `googleusercontent.com`."],["It offers methods to serve various content types such as JSON, RSS, and XML using the `ContentService`."],["Developers can manipulate the output using methods like `append`, `clear`, `downloadAsFile`, `getContent`, `getFileName`, `getMimeType`, `setContent`, and `setMimeType`."],["`TextOutput` enhances security by preventing direct return of text content to the browser from scripts."],["The `downloadAsFile` method enables content to be downloaded instead of displayed, offering flexibility in how the output is handled."]]],["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"]]