2

I have the following Google Apps Script that takes a file from an upload form and stores it automatically in my Google Drive (full code in snippet below). The problem is with this section of it:

    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);
      }

I'm using the standard Google App Services createFile method of the DriveApp class but this has a small file upload size and is proving very slow to upload larger files close to this limit.

I would like to move to the Advanced Google Services that allow you to use Google's public API methods direct in Google Apps Scripts. I've enabled the Drive API and think I want to use something like this:

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);
}

But I'm struggling to implement it alongside my existing code, and the example languages are all for those coding in other languages and using the API rather than just writing in Apps Script.

Please can anyone suggest the code revision required to use the advanced method, allowing a quicker and larger upload?

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
astwood
  • 85
  • 1
  • 7

1 Answers1

5

There is a bit of to'ing and fro'ing possible between Drive and DriveApp rather than getting knee-dep into the pure 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 in this code refers to the Drive API Advanced Google Service that is enabled from the Resources menu. You'll need to enable it as an API from the developer console also. These advanced services attach themselves to your script much like Liraries. Drive here is what the library is defaulting to.

How to enable Advanced Services

JSDBroughton
  • 3,966
  • 4
  • 32
  • 52
  • Thanks that looks great although I keep getting the following error: 'ReferenceError: "Drive" is not defined.' Any ideas I've switched on both the Drive API from Resources>Advanced Google Services and on the Google Developer Console. I'm seeing the advanced methods on my autocomplete lists when writing code but just seems to fail when actually in use? – astwood Jul 13 '15 at 13:50
  • ReferenceError: "Drive" is not defined. – astwood Jul 13 '15 at 13:53
  • Yup, Sorry, I should have explained that one in the answer. You'll need to enable Drive API as an advanced Google Service. Goto the menu item: `Resources > Advanced Google Services > Drive API`. You'll need to enable it as an API in the developer console also. – JSDBroughton Jul 13 '15 at 13:55
  • Thanks I've switched on both the Drive API from Resources>Advanced Google Services and on the Google Developer Console. I'm seeing the advanced methods on my autocomplete lists when writing code but just seems to fail when actually in use? Do you need to allow a time gap between switching on and making calls to the API? – astwood Jul 13 '15 at 13:58
  • typically if you see it autocomplete then it is attached to your script and ready to go. – JSDBroughton Jul 13 '15 at 13:59
  • Must have been my mistake I seemed to have somehow created duplicate projects in the developer console. Copied everything to new script files and project and now working like a dream. Thanks for all your help – astwood Jul 13 '15 at 15:38
  • @jonathon I've seen this [post](http://stackoverflow.com/q/37041045/4625829) referring to your answer, where he is having trouble with uploading large files despite having a [huge maximum file size limit](https://developers.google.com/drive/v2/reference/files/insert). I've done some research in order to help him but I'm not quite sure. Seeing as he is using the Advance Drive Service, the `uploadType` cannot be specified since it is only for the [Drive REST API](https://developers.google.com/drive/v3/web/manage-uploads). What can you suggest? – AL. May 06 '16 at 03:33