你好,我正在尝试在发送电子邮件时将另一张表中的图表附加到PDF附件中。当我从另一张纸上获取图像时,我仍然不知道如何使用HTML中的图像源。
我在将图像附加到电子邮件正文时使用inlineImages:{image: img}
和<img src='cid:image'>
,但与PDF一起使用时不起作用。
function pdfSend() {
const id = <sheet id>;
const ss = SpreadsheetApp.openById(id);
//const ss = SpreadsheetApp.getActive();
const sheet = ss.getSheetByName('Employee Report');
const data = sheet.getDataRange().getValues().slice(1);
const gs = ss.getSheetByName('Graph');
var chart = gs.getCharts();
var img = chart[0].getAs("image/png");
data.forEach((row, i)=> {
let email = row[1];
if (email != "" ) {
let html = `<h1>Hello ${row[0]}</h1>`;
html += `<div>Is your email correct? ${row[1]}</div>`;
html += "<br><p align='center'><imgsrc='???'>/</p> <br><br>";
const blob = Utilities.newBlob(html,MimeType.HTML);
blob.setName(`${row[0]}.pdf`);
const subject = 'Example PDF';
MailApp.sendEmail({
to: email,
subject: subject,
htmlBody: html,
attachments:[blob.getAs(MimeType.PDF)]
});
sheet.getRange(i+2,4).setValue('Sent');
}
});
}
我相信您的目标如下。
- 您希望将
var img = chart[0].getAs("image/png")
包含在电子邮件附件文件的PDF文件中
在这种情况下,下面的修改如何?
修改的脚本:
function pdfSend() {
const id = "<sheet id>";
const ss = SpreadsheetApp.openById(id);
//const ss = SpreadsheetApp.getActive();
const sheet = ss.getSheetByName('Employee Report');
const data = sheet.getDataRange().getValues().slice(1);
const gs = ss.getSheetByName('Graph');
var chart = gs.getCharts();
var img = `data:image/png;base64,${Utilities.base64Encode(chart[0].getAs("image/png").getBytes())}`; // Modified
data.forEach((row, i) => {
let email = row[1];
if (email != "") {
let html = `<h1>Hello ${row[0]}</h1>`;
html += `<div>Is your email correct? ${row[1]}</div>`;
html += `<br><p align='center'><img src='${img}'>/</p> <br><br>`; // Modified
const blob = Utilities.newBlob(html, MimeType.HTML);
blob.setName(`${row[0]}.pdf`);
const subject = 'Example PDF';
MailApp.sendEmail({
to: email,
subject: subject,
htmlBody: html,
attachments: [blob.getAs(MimeType.PDF)]
});
sheet.getRange(i + 2, 4).setValue('Sent');
}
});
}
- 在此修改中,图表作为数据URL包含在
img
标记中
参考:
- base64编码(数据(