Appscript Gmail PDF 未附加但显示 [object][Object]

Appscript Gmail PDF not attached but showing [object][Object] instead

我在这里学习了一个教程:https://www.youtube.com/watch?v=EpZGvKIHmR8

下面的脚本在 google 表单提交后创建一个 PDF,并将 PDF 发送到指定的电子邮件地址。

一切正常,除了没有附加 PDF,而是发送了没有 PDF 的电子邮件,电子邮件正文中带有 [object Object] 字样,我不知道为什么!

还有第二个问题:每提交 1 个表单就会创建 2 个 PDF 文件。谁也能解决这个问题?

这是我的代码:

function afterFormSubmit(e) {
  const info = e.namedValues;
  const pdfFile = createPDF(info);
  createPDF(info);
  sendEmail(e.namedValues['Your Email Address'][0],pdfFile);
}

function sendEmail(email, pdfFile){
  console.log(pdfFile);
  GmailApp.sendEmail(email,"here is your PDF",{
    attachments:[pdfFile],
    name: "My Name"
  });

}

function createPDF(info){
   const pdfFolder = DriveApp.getFolderById("112350000ffedfef");
   const tempFolder = DriveApp.getFolderById("24355kknflef");
   const templateDoc = DriveApp.getFileById("343kndwlkncv");

   const newTempFile = templateDoc.makeCopy(tempFolder);

   const openDoc = DocumentApp.openById(newTempFile.getId());
   const body = openDoc.getBody();

   for (var key in info){
     const texttoreplace = "{{"+key+"}}";
     body.replaceText(texttoreplace,info[key][0]);
   }

   openDoc.saveAndClose();
  
   const blobPDF = newTempFile.getAs(MimeType.PDF);
   const pdfFile = pdfFolder.createFile(blobPDF).setName(info['Date of Meeting'][0] + " " + info['Company Name'][0]); 
   
   return pdfFile;

}

GmailApp.sendEmail(recipient, subject, body, options) 的参数是 recipient, subject, body, options。当我看到你的脚本时,你使用 GmailApp.sendEmail(email,"here is your PDF",{attachments:[pdfFile], name: "My Name"}).

在这种情况下,options用作body。我认为这就是你的问题的原因。为了消除这个问题,下面的修改怎么样?

发件人:

GmailApp.sendEmail(email,"here is your PDF",{
  attachments:[pdfFile],
  name: "My Name"
})

收件人:

GmailApp.sendEmail(email, "here is your PDF", "sample text body", {
  attachments: [pdfFile],
  name: "My Name"
});

或者,

MailApp.sendEmail({
  to: email,
  subject: "here is your PDF",
  attachments: [pdfFile],
  name: "My Name",
  // body: "sample text body" // If you want to use.
})

参考文献: