简体   繁体   中英

Google Apps Scripts- Upload File And Post Data To Spreadsheet

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM