将 Google 云端硬盘文件和 sheet PDF 附加到同一封电子邮件
attach both Google Drive file and sheet PDF to the same email
我有以下代码
var date = Utilities.formatDate(new Date(), "GMT-4", "yyyy-MM-dd")
var images = [];
var searchTerm = date
var searchFor ="title contains '" + searchTerm + "'"; //single quotes are needed around searchterm
var files = DriveApp.searchFiles(searchFor);
while (files.hasNext()) {
var file = files.next();
images.push(file);
}
var htmlBody = htmlTemplate.evaluate().getContent()
var url = "https://docs.google.com/spreadsheets/d/"+ssID+"/export?exportFormat=pdf&format=pdf&gid="+sheetgId+"&range=A1:AD36&pagenumbers=false&gridlines=false&portrait=false&scale=4&horizontal_alignment=CENTER&top_margin=0.50&bottom_margin=0.50&left_margin=0.80&right_margin=0.80";
var result = UrlFetchApp.fetch(url, {
headers: {
'Authorization': 'Bearer ' + token
}
});
var contents = result.getContent();
var aliases = GmailApp.getAliases();
GmailApp.sendEmail(email1,subject ,body, {htmlBody: htmlBody,from: aliases[2], name: "HCA AutoCorreos", attachments:[images,{fileName:"RdP "+date+ ".pdf", content:contents, mimeType:"application//pdf"}]});
应该发送从电子表格创建的 PDF 和从 Google 云端硬盘创建的文件。单独它工作正常但如果我尝试将它组合在“附件”中它 returns 错误。
我在附件部分做错了什么?
我想提出以下修改。
修改点:
attachments
要求是一维数组。在您的脚本中,使用了 [images,{fileName:"RdP "+date+ ".pdf", content:contents, mimeType:"application//pdf"}]
。在这种情况下,images
是一维数组。由此,发生错误。我认为这就是您遇到问题的原因。
当这一点反映到你的脚本中,就变成了下面这样。
修改后的脚本:
从:
GmailApp.sendEmail(email1,subject ,body, {htmlBody: htmlBody,from: aliases[2], name: "HCA AutoCorreos", attachments:[images,{fileName:"RdP "+date+ ".pdf", content:contents, mimeType:"application//pdf"}]});
到:
var attachments = images.concat({fileName:"RdP "+date+ ".pdf", content:contents, mimeType:"application/pdf"});
GmailApp.sendEmail(email1,subject ,body, {htmlBody: htmlBody,from: aliases[2], name: "HCA AutoCorreos", attachments:attachments});
或
GmailApp.sendEmail(email1,subject ,body, {htmlBody: htmlBody,from: aliases[2], name: "HCA AutoCorreos", attachments:[...images,{fileName:"RdP "+date+ ".pdf", content:contents, mimeType:"application/pdf"}]});
注:
- 我认为在这种情况下,您可以使用
mimeType:"application/pdf"
而不是 mimeType:"application//pdf"
。
参考:
我有以下代码
var date = Utilities.formatDate(new Date(), "GMT-4", "yyyy-MM-dd")
var images = [];
var searchTerm = date
var searchFor ="title contains '" + searchTerm + "'"; //single quotes are needed around searchterm
var files = DriveApp.searchFiles(searchFor);
while (files.hasNext()) {
var file = files.next();
images.push(file);
}
var htmlBody = htmlTemplate.evaluate().getContent()
var url = "https://docs.google.com/spreadsheets/d/"+ssID+"/export?exportFormat=pdf&format=pdf&gid="+sheetgId+"&range=A1:AD36&pagenumbers=false&gridlines=false&portrait=false&scale=4&horizontal_alignment=CENTER&top_margin=0.50&bottom_margin=0.50&left_margin=0.80&right_margin=0.80";
var result = UrlFetchApp.fetch(url, {
headers: {
'Authorization': 'Bearer ' + token
}
});
var contents = result.getContent();
var aliases = GmailApp.getAliases();
GmailApp.sendEmail(email1,subject ,body, {htmlBody: htmlBody,from: aliases[2], name: "HCA AutoCorreos", attachments:[images,{fileName:"RdP "+date+ ".pdf", content:contents, mimeType:"application//pdf"}]});
应该发送从电子表格创建的 PDF 和从 Google 云端硬盘创建的文件。单独它工作正常但如果我尝试将它组合在“附件”中它 returns 错误。 我在附件部分做错了什么?
我想提出以下修改。
修改点:
attachments
要求是一维数组。在您的脚本中,使用了[images,{fileName:"RdP "+date+ ".pdf", content:contents, mimeType:"application//pdf"}]
。在这种情况下,images
是一维数组。由此,发生错误。我认为这就是您遇到问题的原因。
当这一点反映到你的脚本中,就变成了下面这样。
修改后的脚本:
从:GmailApp.sendEmail(email1,subject ,body, {htmlBody: htmlBody,from: aliases[2], name: "HCA AutoCorreos", attachments:[images,{fileName:"RdP "+date+ ".pdf", content:contents, mimeType:"application//pdf"}]});
到:
var attachments = images.concat({fileName:"RdP "+date+ ".pdf", content:contents, mimeType:"application/pdf"});
GmailApp.sendEmail(email1,subject ,body, {htmlBody: htmlBody,from: aliases[2], name: "HCA AutoCorreos", attachments:attachments});
或
GmailApp.sendEmail(email1,subject ,body, {htmlBody: htmlBody,from: aliases[2], name: "HCA AutoCorreos", attachments:[...images,{fileName:"RdP "+date+ ".pdf", content:contents, mimeType:"application/pdf"}]});
注:
- 我认为在这种情况下,您可以使用
mimeType:"application/pdf"
而不是mimeType:"application//pdf"
。