Receive Files in Drive and send Confirmation Email

I would like to create a form that allows anyone to upload multiple files to my drop box. But I would also like for the person that uploads the file to receive a confirmation email. The following code uploads the files but does send the email. Please help!

Here is the .gs

function doGet(e) {return HtmlService.createHtmlOutputFromFile('form.html');} 
function sendEmails(form) {

var message= ", your abstract was submitted. Send other work by APR 20";
var subject = "Application Received";
MailApp.sendEmail(form.myEmail, subject, message);
 }

function uploadFiles(form) {

try {

var dropbox = "New Abstracts";
var folder, folders = DriveApp.getFoldersByName(dropbox);

if (folders.hasNext()) {
  folder = folders.next();
} else {
  folder = DriveApp.createFolder(dropbox);
}


var blob = form.myFile1;    
var file = folder.createFile(blob);   
file.setDescription("Uploaded by " + form.myName);

var blob = form.myFile2;    
var file = folder.createFile(blob);   
file.setDescription("Uploaded by " + form.myName);

return "File uploaded successfully " + file.getUrl();

} catch (error) {

return error.toString();
  }
}

And here is the .html portion

<form id="myForm">
<input type="text" name="myName" placeholder="Your name..">
<input type="email" name="myEmail" placeholder ="Your email..">
<input type="file" name="myFile1" >
<input type="file" name="myFile2" >
<input type="submit" value="Upload File" 
       onclick="this.value='Uploading..';
                google.script.run.withSuccessHandler(fileUploaded)
                .uploadFiles(this.parentNode).sendEmails(this.parentNode);
                return false;">

<script>
function fileUploaded(status) {
    document.getElementById('myForm').style.display = 'none';
    document.getElementById('output').innerHTML = status;
}
</script>

<style>
 input { display:block; margin: 20px; }
</style>

I trying this based on the codes found at: http://www.labnol.org/internet/receive-files-in-google-drive/19697/

Upvotes: 1

Views: 213

Answers (1)

Jens Astrup
Jens Astrup

Reputation: 2454

You can't append multiple calls to the script like this:

google.script.run.withSuccessHandler(fileUploaded)
                .uploadFiles(this.parentNode).sendEmails(this.parentNode);

Only uploadFiles (the first one being called) gets executed. Everything after that is getting ignored.

You can add the code inside sendEmails() to the uploadFiles() as one solution? Or add it to the success handler? Or have uploadFiles() call sendEmails() once it is done...

Upvotes: 1

Related Questions