如何使用高级驱动器服务上载文件

时间:2015-07-03 09:10:42

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

我有以下Google Apps脚本从上传表单中获取文件并将其自动存储在我的Google云端硬盘中(下面的代码段中的完整代码)。问题在于它的这一部分:

    var file = folder.createFile(blob);



        //Get root folder and pull all existing folders, plus setup variables pulled from form   
        var dropbox = form.Country;
        var filename = form.reportType+".xls";
        var rootfolder = DriveApp.getFolderById("0Byvtwn42HsoxfnVoSjB2NWprYnRiQ2VWUDZEendNOWwwM1FOZk1EVnJOU3BxQXhwU0pDSE0");
        //Note root folder is Live Uploads Folder in Flatworld App folder structure
        var folder, folders = rootfolder.getFoldersByName(dropbox);
        
        //Check if folder exists and if not create    
        if (folders.hasNext()) {
          folder = folders.next();
        } else {
          folder = rootfolder.createFolder(dropbox);
        }
        
        //Check if file already exists and delete if it does
        var file, files = folder.getFilesByName(filename);
        while( files.hasNext()){
          file = files.next();
          file.setTrashed(true);
        }
        
        
        //Upload file and set various properties
        var blob = form.myFile;    
        var file = folder.createFile(blob);
        var timeStamp = new Date();
        file.setDescription("Uploaded via BNI Upload Form by " + form.myName + " on: " + timeStamp);
        
        //Set file name slightly differently for Weekly Member Report (do not want to overright based on name just keep each extract so add timestamp to name)
        if (form.reportType == "Member Weekly"){
        file.setName(form.reportType + timeStamp + ".xls");
        }
      else
      {
        file.setName(filename);
      }




我使用的是DriveApp类的标准Google App Services createFile方法,但这个文件上传大小很小,并且证明上传接近此限制的较大文件的速度非常慢。

我想转到高级Google服务,该服务允许您直接在Google Apps脚本中使用Google的公共API方法。我已启用了Drive API,并认为我想使用以下内容:

function uploadFile() {
  var image = UrlFetchApp.fetch('http://goo.gl/nd7zjB').getBlob();
  var file = {
    title: 'google_logo.png',
    mimeType: 'image/png'
  };
  file = Drive.Files.insert(file, image);
  Logger.log('ID: %s, File size (bytes): %s', file.id, file.fileSize);
}

但是我在努力与现有代码一起实现它,并且示例语言全部用于那些使用其他语言进行编码并使用API​​而不仅仅是在Apps脚本中编写。

请问任何人都可以建议使用高级方法所需的代码修订,允许更快更大的上传吗?

1 个答案:

答案 0 :(得分:4)

DriveDriveApp之间可能会有一些“ing and”,而不是让纯粹的Drive API陷入困境。

//Upload file and set various properties
var mediaData = form.myFile;    
var timeStamp = new Date();

var resource = {
  title: (form.reportType == "Member Weekly") ? form.reportType + timeStamp + ".xls" : filename,
  mimetype: 'application/vnd.ms-excel',
  description: "Uploaded via BNI Upload Form by " + form.myName + " on: " + timeStamp
};

var file = Drive.Files.insert(resource, mediaData); // create file using Drive API
var fileId = file.id;    
var DriveAppFile = DriveApp.getFileById(fileId); // retrieve file in DriveApp scope. 

DriveApp.removeFile(DriveAppFile); // remove new file from Users root My Drive
folder.addFile(DriveAppFile); // puts file in selected folder
此代码中的

Drive是指从“资源”菜单启用的Drive API Advanced Google Service。您还需要从开发人员控制台启用它作为API。这些高级服务将自己附加到您的脚本,就像Liraries一样。 Drive这是库默认的内容。

How to enable Advanced Services