Appscript Gmail PDF 未附加,但显示 [对象][对象]
我在此处遵循了一个教程: 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
GmailApp.sendEmail(recipient, subject, body, options)
的参数为recipient, subject, body, options
。当我看到您的脚本时,您使用GmailApp.sendEmail(email,"here is your PDF",{attachments:[pdfFile], name: "My Name"})
。在本例中,
options
用作body
。我认为这就是您问题的原因。为了解决这个问题,下面的修改如何?发件人:
收件人:
或者,
参考文献:
The arguments of
GmailApp.sendEmail(recipient, subject, body, options)
arerecipient, subject, body, options
. When I saw your script, you useGmailApp.sendEmail(email,"here is your PDF",{attachments:[pdfFile], name: "My Name"})
.In this case,
options
is used asbody
. I thought that this is the reason of your issue. In order to remove this issue, how about the following modification?From:
To:
Or,
References: