Reputation: 51
I have tried for a while now and can't get this to work. I am in Google Apps Scripts and trying to create a form that uploads a file in Google Drive and posts the data to a spreadsheet. I wrote a function to do each one and they work fine. But when I try to call both of them or combine them the whole thing breaks down.
I thought the answer here would solve my issue but it did not.
Here is how I have the function combined currently in my .gs file.
function addEmail(form) {
var dropbox = "EAlertUploads";
var folder = DriveApp.getFoldersByName(dropbox);
var blob = form.myFile;
var file = folder.createFile(blob);
var ss = SpreadsheetApp.openByUrl("<Spreadsheet URL>");
var sheet = ss.getSheets()[0];
var range = sheet.getRange(sheet.getLastRow()+1,1,1,12);
var values = [[new Date(),form.first,form.last,form.phone,form.email,form.info,form.method,form.call,form.text,form.questions,form.acknowledgement,form.deadline]];
range.setValues(values);
Logger.log(form.first,form.last,form.phone,form.email,form.info,form.method,form.call,form.text,form.questions,form.acknowledgement,form.deadline);
return 200;
}
And here is what they looked like separated
function addEmail(form) {
var ss = SpreadsheetApp.openByUrl("<SpreadsheetURL>");
var sheet = ss.getSheets()[0];
var range = sheet.getRange(sheet.getLastRow()+1,1,1,12);
var values = [[new Date(),form.first,form.last,form.phone,form.email,form.info,form.method,form.call,form.text,form.questions,form.acknowledgement,form.deadline]];
range.setValues(values);
Logger.log(form.first,form.last,form.phone,form.email,form.info,form.method,form.call,form.text,form.questions,form.acknowledgement,form.deadline);
return 200;
}
function uploadFiles(form) {
try {
var dropbox = "EAlertUploads";
var folder, folders = DriveApp.getFoldersByName(dropbox);
if (folders.hasNext()) {
folder = folders.next();
} else {
folder = DriveApp.createFolder(dropbox);
}
var blob = form.myFile;
var file = folder.createFile(blob);
file.setDescription("Uploaded by " + form.first + first.last);
return "File uploaded successfully " + file.getUrl();
} catch (error) {
return error.toString();
}
}
In my index.html file here is my function that calls my success handler.
$(document).ready(function() {
$("#email_subscribe".submit(function(){
google.script.run.withSuccessHandler(function(ret)
$("#thank_you").show("slow");
$("#email_subscribe").slideUp();
console.log(ret);
}).addEmail(this);
});
});
I am using a submit button. I know I have seen differing views on how that effects things. Also, one last caveat. The fileUpload only works if I do it as an onclick like this
onclick="google.script.run
.uploadFiles(this.parentNode);
return false;"
I have tried everything I can find from here, to blogs, to the tutorials straight from Google to no avail.
Upvotes: 0
Views: 1583
Reputation: 51
I got this to work by combining the functions. The only thing I did differently, that I hadn't tried yet was putting the function that uploaded the files before the one that entered data in the Spreadsheet.
Upvotes: 1