Class TextOutput
文本输出
可从脚本提供的 TextOutput 对象。
出于安全考虑,脚本无法直接将文本内容返回给浏览器。而是会将浏览器重定向到 googleusercontent.com,该网站会在不进行任何进一步的净化或处理的情况下显示该图片。
您可以返回如下文本内容:
function doGet() {
return ContentService.createTextOutput('hello world!');
}
还有一些方法可将 JSON、RSS 和 XML 作为 TextOutput 提供;请参阅
ContentService
上的相应方法。
详细文档
append(addedContent)
将新内容附加到要投放的内容。
参数
名称 | 类型 | 说明 |
addedContent | String | 要附加的内容 |
返回
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 本身,适用于链接
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2024-12-22。
[null,null,["最后更新时间 (UTC):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"]]