从Google Apps脚本将文件从Google Drive下载到本地文件夹 [英] Download file from Google Drive to local folder from Google Apps Script
问题描述
我正在尝试从我的Google云端硬盘下载特定的 *。csv
文件到我的电脑上的本地文件夹。我没有运气尝试过以下内容:
I'm trying to download a specific *.csv
file from my Google Drive to a local folder on my computer. I have tried the following with no luck:
ContentService.createTextOutput().downloadAsFile(fileName);
我没有收到错误,没有任何事情发生。任何关于我的尝试发生什么的想法?
I don't get an error, and nothing appears to happen. Any ideas on what's wrong with my attempt?
推荐答案
ContentService用于将文本内容作为Web应用程序提供。你所显示的代码行并不是它自己的。假设它是您作为Web App部署的 doGet()
函数的一行代码,那么这就是为什么您看不到任何内容:
ContentService is used to serve up text content as a Web Application. The line of code you've shown does nothing on it's own. Assuming that it's a one-line body of a doGet()
function that you've deployed as a Web App, then here's why you're seeing nothing:
-
ContentService
- 使用内容服务和... -
.createTextOutput()
- 创建一个空的文本输出对象,然后... -
.downloadAsFile(fileName)
- 当浏览器调用我们的Get
服务时,请下载内容(名为fileName
)而不是显示它。
ContentService
- use Content Service, and....createTextOutput()
- create an empty text output object, then....downloadAsFile(fileName)
- when a browser invokes ourGet
service, have it download the content (namedfileName
) rather than displaying it.
由于我们没有内容,没有什么可以下载,所以你看,没有任何。
Since we have no content, there's nothing to download, so you see, well, nothing.
此脚本将在您的Google云端硬盘上获取csv文件的文本内容,并将其提供下载。一旦您保存了该版本的脚本并将其发布为网络应用程序,您可以将浏览器引导到发布的URL来开始下载。
This script will get the text content of a csv file on your Google Drive, and serve it for downloading. Once you've saved a version of the script and published it as a web app, you can direct a browser to the published URL to start a download.
根据您的浏览器设置,您可能可以选择特定的本地文件夹和/或更改文件名。您无法从服务器端运行此脚本。
Depending on your browser settings, you may be able to select a specific local folder and/or change the file name. You have no control over that from the server side, where this script runs.
/**
* 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
}
这篇关于从Google Apps脚本将文件从Google Drive下载到本地文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!