将对象添加到inlineImages sendEmail参数

时间:2019-05-11 22:33:33

标签: javascript google-apps-script google-sheets

我正在使用以下内容遍历工作表名称“图表”上的所有图表,并将它们作为嵌入式图像附加。

在htmlBody中,我在需要的地方将每个图表作为字符串ChartXString引用。

  var Chart1Sheet = SpreadsheetApp.getActive().getSheetByName('Charts');
  var charts1 = Chart1Sheet.getCharts();
  var chartBlobs1=new Array(charts1.length); 
  var emailImages1={};
  for(var i=0;i<charts1.length;i++){
    var builder = charts1[i].modify();
    builder.setOption('vAxis.format', '#');
    var newchart = builder.build();
    chartBlobs1[i]= newchart.getAs('image/png');
    chart1String = "<p align='center'><img src='cid:chart"+0+"'></p>";
    chart2String = "<p align='center'><img src='cid:chart"+1+"'></p>";
    chart3String = "<p align='center'><img src='cid:chart"+2+"'></p>";
    chart4String = "<p align='center'><img src='cid:chart"+3+"'></p>";
    emailImages1["chart"+i]= chartBlobs1[i];
  }


  myHTML = myHTML + myHTML2 + myHTML3 + myHTML4 + myHTML5 + chart1String + chart2String + chart3String + chart4String + myHTML6;
  recipient = 'email@address';
  subject = 'My subject text';
  body = 0;


  MailApp.sendEmail(recipient, subject, body, {htmlBody: myHTML, inlineImages:emailImages1});

这工作得很好,但是我想添加一个像the example from the documentation这样的图像。

  var youtubeLogoUrl =
        "https://developers.google.com/youtube/images/YouTube_logo_standard_white.png";
  var youtubeLogoBlob = UrlFetchApp
                          .fetch(youtubeLogoUrl)
                          .getBlob()
                          .setName("youtubeLogoBlob");
  MailApp.sendEmail({
    to: "recipient@example.com",
    subject: "Logos",
    htmlBody: "inline YouTube Logo <img src='cid:youtubeLogo'>",
    inlineImages:
      {
        youtubeLogo: youtubeLogoBlob
      }
  });

我认为问题不在于如何添加对象。到目前为止,这是我尝试过的:

  var youtubeLogoUrl =
        "https://developers.google.com/youtube/images/YouTube_logo_standard_white.png";
  var youtubeLogoBlob = UrlFetchApp
                          .fetch(youtubeLogoUrl)
                          .getBlob()
                          .setName("youtubeLogoBlob");
LogoStr = "<img src='cid:youtubeLogo'>";

  var Chart1Sheet = SpreadsheetApp.getActive().getSheetByName('Charts');
      ................
    chart4String = "<p align='center'><img src='cid:chart"+3+"'></p>";
    emailImages1["chart"+i]= chartBlobs1[i] + youtubeLogoBlob;
  }


  myHTML = LogoStr + myHTML + myHTML2 + myHTML3 + myHTML4 + myHTML5 + chart1String + chart2String + chart3String + chart4String + myHTML6;
  recipient = 'email@address';
  subject = 'My subject text';
  body = 0;


  MailApp.sendEmail(recipient, subject, body, {htmlBody: myHTML, inlineImages:emailImages1});

我也尝试过:

  MailApp.sendEmail(recipient, subject, body, {htmlBody: myHTML, inlineImages:emailImages1,youtubeLogoBlob});

1 个答案:

答案 0 :(得分:1)

  • 您想将youtubeLogoBlob中的emailImages1作为inlineImages。

如果我的理解是正确的,那么该修改如何?请按如下所示修改问题中的第三个脚本。

发件人:

  emailImages1["chart"+i]= chartBlobs1[i] + youtubeLogoBlob;
}
myHTML = LogoStr + myHTML + myHTML2 + myHTML3 + myHTML4 + myHTML5 + chart1String + chart2String + chart3String + chart4String + myHTML6;
recipient = 'email@address';
subject = 'My subject text';
body = 0;
MailApp.sendEmail(recipient, subject, body, {htmlBody: myHTML, inlineImages:emailImages1});

收件人:

  emailImages1["chart"+i]= chartBlobs1[i]; // Modified
}
emailImages1["youtubeLogo"] = youtubeLogoBlob; // Added
myHTML = myHTML + myHTML2 + myHTML3 + myHTML4 + myHTML5 + chart1String + chart2String + chart3String + chart4String + myHTML6;
recipient = 'email@address';
subject = 'My subject text';
body = 0;
MailApp.sendEmail(recipient, subject, body, {htmlBody: myHTML, inlineImages:emailImages1}); // Modified

注意:

  • 在您的脚本中,显示的脚本中未声明几个变量,请注意这一点。

如果我误解了你的问题,我表示歉意。