我在这里学习了一个教程:https://www.youtube.com/watch?v=EpZGvKIHmR8.
下面的脚本在谷歌表单提交后创建一个PDF,并将PDF发送到指定的电子邮件地址。
除了没有附加PDF,而是在没有PDF的情况下发送电子邮件,邮件正文中有[object object]字样,我不知道为什么!
还有第二个问题:每提交一份表格,就会创建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.
})
参考文献:
- 发送GmailApp类的电子邮件(收件人、主题、正文、选项(
- sendClass MailApp的电子邮件(消息(