简体   繁体   English

Google Apps脚本-上传文件并将数据发布到电子表格

[英]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. 我正在使用Google Apps脚本,正在尝试创建一个表单,该表单将文件上传到Google云端硬盘中并将数据发布到电子表格中。 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. 这是我目前在.gs文件中合并功能的方式。

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. 在我的index.html文件中,这是调用我的成功处理程序的函数。

$(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时才可以使用fileUpload

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. 我尝试了所有可以从这里找到的内容,从博客到从Google直接获得的教程都无济于事。

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. 我唯一没有做过的尝试是将上传文件的功能放在在电子表格中输入数据的功能之前。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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