Jas*_*fin 5 download google-apps-script google-drive-api
我正在尝试*.csv
从Google云端硬盘下载特定文件到计算机上的本地文件夹。我没有运气就尝试了以下方法:
ContentService.createTextOutput().downloadAsFile(fileName);
Run Code Online (Sandbox Code Playgroud)
我没有收到任何错误,而且似乎没有任何反应。关于我的尝试出了什么问题的任何想法?
ContentService 用于将文本内容作为 Web 应用程序提供。您显示的代码行本身不执行任何操作。假设它doGet()
是您作为 Web 应用程序部署的函数的一行代码体,那么这就是为什么您什么也看不到的原因:
ContentService
- 使用内容服务,和....createTextOutput()
- 创建一个空的文本输出对象,然后....downloadAsFile(fileName)
- 当浏览器调用我们的Get
服务时,让它下载内容(名为fileName
)而不是显示它。因为我们没有内容,所以没有什么可下载的,所以你看,好吧,什么都没有。
此脚本将获取 Google Drive 上 csv 文件的文本内容,并提供下载。保存脚本版本并将其发布为 Web 应用程序后,您可以将浏览器定向到发布的 URL 以开始下载。
根据您的浏览器设置,您可以选择特定的本地文件夹和/或更改文件名。您无法从运行此脚本的服务器端控制它。
/**
* This function serves content for a script deployed as a web app.
* See https://developers.google.com/apps-script/execution_web_apps
*/
function doGet() {
var fileName = "test.csv"
return ContentService
.createTextOutput() // Create textOutput Object
.append(getCsvFile(fileName)) // Append the text from our csv file
.downloadAsFile(fileName); // Have browser download, rather than display
}
/**
* Return the text contained in the given csv file.
*/
function getCsvFile(fileName) {
var files = DocsList.getFiles();
var csvFile = "No Content";
for (var i = 0; i < files.length; i++) {
if (files[i].getName() == fileName) {
csvFile = files[i].getContentAsString();
break;
}
}
return csvFile
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
9359 次 |
最近记录: |