【问题标题】:Download file from Google Drive to local folder from Google Apps Script从 Google Apps 脚本将文件从 Google Drive 下载到本地文件夹
【发布时间】:2013-06-26 21:35:28
【问题描述】:

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

ContentService.createTextOutput().downloadAsFile(fileName);

我没有收到错误消息,而且似乎什么也没发生。关于我的尝试有什么问题有什么想法吗?

【问题讨论】:

    标签: download google-apps-script google-drive-api


    【解决方案1】:

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

    由于我们没有内容,所以没有可下载的内容,所以你看,什么都没有

    CSV 下载器脚本

    此脚本将获取您 Google 云端硬盘上 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
    }
    

    【讨论】:

    • 您好,感谢您的回答。我可以使用浏览器手动下载文件。但是你知道如何使用 bash 脚本下载它吗?
    • @zhihong - 查看curl。 SO中有很多例子。
    • 感谢您的快速建议。就像在浏览器中一样,您已经通过了身份验证,但是如果我使用 curl,我会收到“未经授权”的反馈。我今天发现 google 也有电子表格 API,我想这也许是我应该使用的正确的。
    猜你喜欢
    • 2020-08-27
    • 2015-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多