将图像添加到 Google 表格电子邮件应用程序脚本

Add Image to Google Sheets email App Script

我想在消息末尾添加图片,但我不知道该怎么做。我尝试添加一个内联图像,但因为我已经在使用 MailApp 中的选项,所以我认为它不会通过。

这是我要添加的图片:https://industry.datascience.columbia.edu/sites/default/files/images/NSF-NORTHEAST-BIG-DATA-INDUSTRY-LOGO.png

我希望它在 "Kathy McKeown" 之后和 "nebigdatahub.org" url

之前

感谢您的帮助!!

这是代码:

function personalguest2018 () {
var ss = SpreadsheetApp.getActiveSpreadsheet();
ss.setActiveSheet(ss.getSheetByName("2018Invites"));
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 8; 
var dataRange = sheet.getRange("A8:J164");
var data = dataRange.getValues();
for (var i = 0; i < data.length; ++i) {
   var row = data[i];
   var Lastname = row[0];       // First column
   var Firstname= row[1];       // Second column
   var organization = row[2];   // Third column
   var email = row[3];          // Fourth column
   var sector = row[4];         // Fifth column
   var role = row[5];           // Sixth column
   var typeofinvite = row[6];   // Seventh column
   var emailSent = row[7]       // Eighth column
   var subject = "2018 Northeast Big Data Innovation Annual Summit";
   var msgHtml = "Dear " + Firstname + "," + "</p>"
   +"<p>"+"We are reaching out to personally invite you to the <b>2018       Annual Summit of the Northeast Big Data Innovation Hub</b>, on Tuesday,   March 27th, at Columbia University."+"<p>"
+"<p>"+"Please join us and learn how the Hub has grown over the past year, including updates on new cross-sector initiatives, lightning talks from our Big Data Spoke PIs, and opportunities to collaborate with "+ 
"our stakeholders in breakout sessions on data literacy, ethics, and health. Keynote speaker Corinna Cortes (Google Research, New York) will highlight her team's data-driven approach to fighting fake news. "+
"A panel of leaders drawn from academia and the private sector will discuss how​ they address the challenges of rapid advances in digital media ​that may ​outpace our ability to ​maximize its benefits"+
" and minimize the​ potential drawbacks."+"<p>"
+"<p>"+"Your perspective would be a valued contribution to the day's discussions, and we hope very much to see you there! <b>Registration and further information is available at</b> bit.ly/RegisterNE2018." 
+"<p>"+"Please feel free to share with members of your team who may be interested in joining. Should you have any questions, please don't hesitate to reach out to us via rb70@columbia.edu." + "<p>"+
"All the best," + "<p>"
+"<p>"+"René Bastón"+"<br/>"+ 
"Kathy McKeown"+ "</p>" +
+"</p>"+ "<p>"+"nebigdatahub.org" + "</p>";
var msgPlain = msgHtml.replace(/\<br\/\>/gi, '\n').replace(/(<([^>]+)>)/ig, ""); // clear html tags and convert br to new lines for plain mail
if (emailSent !="EMAIL_SENT"){  // Prevents sending duplicates
  if(row[6]=="Personal" && row[5]== "Katy") { 
    GmailApp.sendEmail(email, subject, msgPlain,{ htmlBody: msgHtml });
    sheet.getRange(startRow + i, 8).setValue("EMAIL_SENT");
  // Make sure the cell is updated right away in casethe script is     inaterrupted
    SpreadsheetApp.flush();
   }
  }  
 }
}

这个修改怎么样?

修改点:

  • 图片在for循环之前导入到blob
  • 图像是使用<img src="cid:image"><br/>inlineImages: {image: blob}添加的。

修改脚本:

function personalguest2018 () {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  ss.setActiveSheet(ss.getSheetByName("2018Invites"));
  var sheet = SpreadsheetApp.getActiveSheet();
  var startRow = 8; 
  var dataRange = sheet.getRange("A8:J164");
  var data = dataRange.getValues();
  var blob = UrlFetchApp.fetch("https://industry.datascience.columbia.edu/sites/default/files/images/NSF-NORTHEAST-BIG-DATA-INDUSTRY-LOGO.png").getBlob(); // Added
  for (var i = 0; i < data.length; ++i) {
    var row = data[i];
    var Lastname = row[0];       // First column
    var Firstname= row[1];       // Second column
    var organization = row[2];   // Third column
    var email = row[3];          // Fourth column
    var sector = row[4];         // Fifth column
    var role = row[5];           // Sixth column
    var typeofinvite = row[6];   // Seventh column
    var emailSent = row[7]       // Eighth column
    var subject = "2018 Northeast Big Data Innovation Annual Summit";
    var msgHtml = "Dear " + Firstname + "," + "</p>"
      +"<p>"+"We are reaching out to personally invite you to the <b>2018       Annual Summit of the Northeast Big Data Innovation Hub</b>, on Tuesday,   March 27th, at Columbia University."+"<p>"
      +"<p>"+"Please join us and learn how the Hub has grown over the past year, including updates on new cross-sector initiatives, lightning talks from our Big Data Spoke PIs, and opportunities to collaborate with "+ 
      "our stakeholders in breakout sessions on data literacy, ethics, and health. Keynote speaker Corinna Cortes (Google Research, New York) will highlight her team's data-driven approach to fighting fake news. "+
      "A panel of leaders drawn from academia and the private sector will discuss how​ they address the challenges of rapid advances in digital media ​that may ​outpace our ability to ​maximize its benefits"+
      " and minimize the​ potential drawbacks."+"<p>"
      +"<p>"+"Your perspective would be a valued contribution to the day's discussions, and we hope very much to see you there! <b>Registration and further information is available at</b> bit.ly/RegisterNE2018." 
      +"<p>"+"Please feel free to share with members of your team who may be interested in joining. Should you have any questions, please don't hesitate to reach out to us via rb70@columbia.edu." + "<p>"+
      "All the best," + "<p>"
      +"<p>"+"René Bastón"+"<br/>"+ 
      "Kathy McKeown"+ "</p>" +
      '<img src="cid:image"><br/>' // Added
      +"</p>"+ "<p>"+"nebigdatahub.org" + "</p>";
    var msgPlain = msgHtml.replace(/\<br\/\>/gi, '\n').replace(/(<([^>]+)>)/ig, ""); // clear html tags and convert br to new lines for plain mail
    if (emailSent !="EMAIL_SENT"){  // Prevents sending duplicates
      if(row[6]=="Personal" && row[5]== "Katy") { 
        GmailApp.sendEmail(email, subject, msgPlain,{ htmlBody: msgHtml, inlineImages: {image: blob} }); // Modified
        sheet.getRange(startRow + i, 8).setValue("EMAIL_SENT");
        // Make sure the cell is updated right away in casethe script is     inaterrupted
        SpreadsheetApp.flush();
      }
    }  
  }
}

注:

  • 根据你的脚本,我无法理解 I tried adding an inline image but because I am already the using the option in MailApp, I think it is not going trough.。所以如果这个修改是你不想要的,我很抱歉。
  • 如果要用MailApp,也可以用MailApp.sendEmail({to: email, subject: subject, body: msgPlain, htmlBody: msgHtml, inlineImages: {image: blob}});

如果我误解了你的问题,我很抱歉。

使用它来帮助调整图像的大小:

<img src="cid:image" height="(insert number for height in px)" width="(insert number for width in px)">