Google Drive API - 批量上传,文件重命名和超时

时间:2018-03-18 14:40:56

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

我最近发现了一个很棒的谷歌脚本,允许用户使用Google表格列出大量下载(在我的情况下为Jpegs)并设置标题。脚本转换(?)...将文件从远程位置移动到您的Google云端硬盘。所以没有毫无意义的下载,在中间上传。

function SaveToGoogleDrive(){

  var folderID = 'FOLDER_HERE';  // put id of the Google Drive folder
  var folder = DriveApp.getFolderById(folderID)// get the folder
  var sheet = SpreadsheetApp.getActiveSheet();
  var data = sheet.getDataRange().getValues();

  for (var i = 1; i < data.length; i++) {
    var pdfURL = data[i][2];
    var myFileName = data[i][1] + '.pdf';
    var file = UrlFetchApp.fetch(pdfURL);
    folder.createFile(myFileName,file);
  }
}

(代码来自 - http://unexpectedweb.blogspot.com.es/2017/11/directly-save-file-to-google-drive-by.html

  1. 该脚本应该允许我为每次上传设置一个名称,该名称将在添加到Google云端硬盘时应用于该文件,但这对我不起作用。

    代码中有哪些东西对你不好看,因为重命名不起作用。也许有一个脚本允许我在文件全部在我的Google云端硬盘中后重命名?

  2. 此外 - 我正在转载(?)大约500个文件,Google的脚本只能运行6分钟。我如何加入类似脚本demonstrated here:

  3. 的内容

    那段代码......

    /* Based on https://gist.github.com/erickoledadevrel/91d3795949e158ab9830 */
    function isTimeUp_(start) {
      var now = new Date();
      return now.getTime() - start.getTime() > 300000; // 5 minutes
    }
    
    function SaveToGoogleDrive(){
    
      var folderID = 'FOLDER_HERE';  // put id of the Google Drive folder
      var folder = DriveApp.getFolderById(folderID)// get the folder
      var sheet = SpreadsheetApp.getActiveSheet();
      var data = sheet.getDataRange().getValues();
    
      var threads = GmailApp.getInboxThreads(0, 50);  
      var start = new Date();
    
      for (var i in threads) {
        if (isTimeUp_(start)) {
          Logger.log("Time up");
          break;
        }
    
        // Process the thread otherwise    
        for (var i = 1; i < data.length; i++) {
          var pdfURL = data[i][2];
          var myFileName = data[i][1] + '.pdf';
          var file = UrlFetchApp.fetch(pdfURL);
          folder.createFile(myFileName, file);
        }
      }
    }
    

    感谢您的想法。用我有限的知识把两者结合起来真是太麻烦了。

1 个答案:

答案 0 :(得分:2)

更改

folder.createFile(myFileName,file);

 folder.createFile(file).setName(myFileName);