我会解释我正在尝试做什么。
我需要将每行唯一的二维码发送到相应的电子邮件 ID。
下面是应用程序脚本代码(这与我从 Google Developer 获取的代码相同)。
obj.forEach(function(row, rowIdx){
if (row[EMAIL_SENT_COL] == ''){
try {
const msgObj = fillInTemplateFromObject_(emailTemplate.message, row);
GmailApp.sendEmail(row[RECIPIENT_COL], msgObj.subject, msgObj.text, {
htmlBody: msgObj.html,
attachments: emailTemplate.attachments,
inlineImages: emailTemplate.inlineImages
});
out.push([new Date()]);
} catch(e) {
out.push([e.message]);
}
} else {
out.push([row[EMAIL_SENT_COL]]);
}
});
function getGmailTemplateFromDrafts_(subject_line){
try {
// get drafts
const drafts = GmailApp.getDrafts();
// filter the drafts that match subject line
const draft = drafts.filter(subjectFilter_(subject_line))[0];
// get the message object
const msg = draft.getMessage();
// Handles inline images and attachments so they can be included in the merge
// Based on https://stackoverflow.com/a/65813881/1027723
// Gets all attachments and inline image attachments
const allInlineImages = draft.getMessage().getAttachments({includeInlineImages: true,includeAttachments:true});
const attachments = draft.getMessage().getAttachments({includeInlineImages: true});
const htmlBody = msg.getBody();
// Creates an inline image object with the image name as key
// (can't rely on image index as array based on insert order)
const img_obj = allInlineImages.reduce((obj, i) => (obj[i.getName()] = i, obj) ,{});
//Regexp searches for all img string positions with cid
const imgexp = RegExp('<img.*?src="cid:(.*?)".*?alt="(.*?)"[^\>]+>', 'g');
const matches = [...htmlBody.matchAll(imgexp)];
//Initiates the allInlineImages object
const inlineImagesObj = {};
// built an inlineImagesObj from inline image matches
matches.forEach(match => inlineImagesObj[match[1]] = img_obj[match[2]]);
return {message: {subject: subject_line, text: msg.getPlainBody(), html:htmlBody},
attachments: attachments, inlineImages: inlineImagesObj };
} catch(e) {
throw new Error("Oops - can't find Gmail draft");
}
电子邮件中缺少二维码。我觉得获取的图像数据存在一些问题。我对应用程序脚本非常陌生,事实上,这是我尝试的第一件事,我可能会遗漏一些基础知识。
如果需要任何其他信息,请告诉我。预先感谢。
从您显示的电子表格图像、“电子邮件正文的屏幕截图:”和“下面是应用程序脚本代码(这与我从 Google Developer 获取的代码相同)”的脚本来看,作为一个简单的修改,以下修改如何?在此修改中,修改了
https://developers.google.com/apps-script/samples/automations/mail-merge#code-source的函数
sendEmails
。
const obj = data.map(r => (heads.reduce((o, k, i) => (o[k] = r[i] || '', o), {})));
const obj = data.map(r => {
const temp = heads.reduce((o, k, i) => (o[k] = r[i] || '', o), {});
temp["QR code"] = `<img src="${temp["URL"]}">`;
return temp;
});
测试修改后的脚本时,得到以下结果。