Google 表单上传 + 在自动电子邮件回复中附加上传的 pdf

Google form upload + attach the uploaded pdf in an Auto email reply

我目前正在做一个项目,让用户填写一个简单的姓名、电子邮件地址表格并上传一个 pdf。

提交表单时,该表单的目标 GSheet 会接收姓名、电子邮件地址和上传的 PDF 的 URL。提交表单后,我可以自动回复一封带有 html 的电子邮件,上面写着“已收到您提交的带有附件的表单”。但是是否可以仅使用 URL 将上传的 pdf 附加到该电子邮件中?我找不到 GetFileByURL 的类驱动器。只能找到 getfilebyname/type.

我可以想到另一种方法,即让用户在表单中手动输入要上传的文件的名称,然后让 google 应用脚本获取文件名。但是,我正在努力避免人为错误。人为错误,例如上传的文件名和在表单中输入的文件名不匹配。 :)

    function onSubmit(e) {
      var ss = SpreadsheetApp.getActive();
      var range = e.range
      var row = Number(range.getRow());
    
      var upFolder = DriveApp.getFolderById("ADDRESS OF WHERE THE UPLOADED FILE FROM THE FORM IS");
    
      var formReceived = HtmlService.createTemplateFromFile("HTML Email"); //get the html template
    
      var parent_name = ss.getRange('B'+row).getValue();
      var parent_email = ss.getRange('C'+row).getValue();
      var fileURL = ss.getRange('D'+row).getValue();
    
      ss.getRange('E'+row).setValue("Error! Email not sent.");
    
      //Tag input into the html
      formReceived.parent_name=parent_name
    
    //Once the form is submitted, the URL will show up on the Gsheet
    //in column D. Is there a way to attach the file from that URL into the mailapp below.
   //Not using the line below (getfilesbyname).

        var invoicereceipt = upFolder.getFilesByName("Can be manual input from form");
    
        if(invoicereceipt.hasNext()){
          MailApp.sendEmail({
          to: parent_email,
          name: parent_name,
          htmlBody : formReceived.evaluate().getContent(),
          attachments: [invoicereceipt.next().getAs(MimeType.PDF)],
          });
            var now = new Date()
            var k = Utilities.formatDate(now,'GMT+8',"YYYY.MM.dd HH:mm:ss")
            ws.getRange('E'+index).setValue(k); 
        }
    
    }

我相信你的目标如下。

  • 您想在提交 Google 表单时发送电子邮件。这时候你想把上传的PDF文件附加到邮件中。
  • 您的脚本存在于 Google 与 Google 表格链接的电子表格中。
  • 您的 onSubmit 函数已安装为 OnSubmit 可安装触发器。

修改点:

  • 不包括电子邮件标题。在这种情况下,会发生错误。
  • wsindex 未声明。
  • 在这种情况下,我认为可以使用事件object。

当这些点反映到一个脚本中,就变成了这样。

修改后的脚本:

function onSubmit(e) {
  var [, parent_name, parent_email, fileURL] = e.values;
  var formReceived = HtmlService.createTemplateFromFile("HTML Email");
  formReceived.parent_name = parent_name;
  MailApp.sendEmail({
    subject: "sample mail",
    to: parent_email,
    name: parent_name,
    htmlBody: formReceived.evaluate().getContent(),
    attachments: [DriveApp.getFileById(fileURL.split("=").pop())],
  });

  // e.range.offset(0, 1).setValue("Error! Email not sent."); // I'm not sure whether you want to use this line.
}
  • 上传的PDF文件被DriveApp.getFileById(fileURL.split("=").pop())检索到。

  • 这种情况,请删除可安装的OnSubmit触发器,重新安装。这样,其他范围就包括在内了。

注:

  • 这是一个简单的修改脚本。所以请根据自己的实际情况修改。

参考: