Appscript Gmail PDF 未附加,但显示 [对象][对象]

发布于 2025-01-20 22:48:55 字数 1442 浏览 1 评论 0原文

我在此处遵循了一个教程: https://wwwww.youtube.com/watch? /a>。

下面的脚本在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;

}

I followed a tutorial here: https://www.youtube.com/watch?v=EpZGvKIHmR8.

The script below creates a PDF upon a google form submit and sends the PDF to a specified email address.

Everything works except that instead of the PDF being attached, the email is sent without a PDF, with the words [object Object] in the email body and I have no idea why!

Also a second problem: 2 PDF files are being created for every 1 form submission. Can anyone solve this too?

Here is my code:

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;

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忆梦 2025-01-27 22:48:55

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.
})

参考文献:

The arguments of GmailApp.sendEmail(recipient, subject, body, options) are recipient, subject, body, options. When I saw your script, you use GmailApp.sendEmail(email,"here is your PDF",{attachments:[pdfFile], name: "My Name"}).

In this case, options is used as body. I thought that this is the reason of your issue. In order to remove this issue, how about the following modification?

From:

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

To:

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

Or,

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

References:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文