How to use Advanced Drive Service to download files

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

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 use the standard Google App Services createFile method of the createFile class, but it has a small file upload size and it loads very large files very slowly, close to this limit.

I’d like to move on to advanced Google services that allow you to use the public Google API methods directly in your Google Apps scripts. I have included the Drive API and I 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 together with my existing code, and all the sample languages ​​are designed for these encodings in other languages ​​and use the API, and not just in the Script application.

Can someone suggest a code revision needed to use the advanced method, allowing faster and more loading?

+1
google-drive-sdk google-apps-script
source share
1 answer

There are few possibilities and possibilities between Drive and DriveApp , and not to get knee-dep in a pure API driver.

 //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 advanced Google Drive API service , which is included in the Resources menu. You also need to enable it as an API from the developer console. These advanced services join your script as Liraries. Drive is what the library does by default.

How to enable additional services

+3
source share

All Articles