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?