Google 表格 - 使用 Apps 脚本未通过电子邮件收到来自 URL 的图像

问题描述 投票:0回答:1

我会解释我正在尝试做什么。

我需要将每行唯一的二维码发送到相应的电子邮件 ID。

下面是Google Sheet格式的截图: Screenshot of sheets template

下面是应用程序脚本代码(这与我从 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");
}

邮件正文截图: Screenshot of email body

收到邮件截图: Screenshot of email received:

电子邮件中缺少二维码。我觉得获取的图像数据存在一些问题。我对应用程序脚本非常陌生,事实上,这是我尝试的第一件事,我可能会遗漏一些基础知识。

如果需要任何其他信息,请告诉我。预先感谢。

google-sheets google-apps-script google-sheets-api googlesheets4
1个回答
0
投票

从您显示的电子表格图像、“电子邮件正文的屏幕截图:”和“下面是应用程序脚本代码(这与我从 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;
});

测试:

测试修改后的脚本时,得到以下结果。

enter image description here

注:

  • 在此修改中,从您的电子表格图像中,使用“URL”的值作为 HTML 标记创建“QR 代码”的值。因此,当您更改标题标题时,此修改可能无法使用。请注意这一点。
© www.soinside.com 2019 - 2024. All rights reserved.