将文件从Google云端硬盘下载到Google Apps脚本中的本地文件夹

Jas*_*fin 5 download google-apps-script google-drive-api

我正在尝试*.csv从Google云端硬盘下载特定文件到计算机上的本地文件夹。我没有运气就尝试了以下方法:

ContentService.createTextOutput().downloadAsFile(fileName);
Run Code Online (Sandbox Code Playgroud)

我没有收到任何错误,而且似乎没有任何反应。关于我的尝试出了什么问题的任何想法?

Mog*_*dad 7

ContentService 用于将文本内容作为 Web 应用程序提供。您显示的代码行本身不执行任何操作。假设它doGet()是您作为 Web 应用程序部署的函数的一行代码体,那么这就是为什么您什么也看不到的原因:

因为我们没有内容,所以没有什么可下载的,所以你看,好吧,什么都没有

CSV 下载器脚本

此脚本将获取 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)