将图片添加到Google表格电子邮件App脚本

时间:2018-03-09 17:29:59

标签: html google-apps-script google-sheets-api

我正在尝试在邮件末尾添加图片,但我不知道该怎么做。我尝试添加内联图像,但因为我已经在MailApp中使用该选项,我认为它不会低谷。

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

我想去“Kathy McKeown”和“nebigdatahub.org”网址之前

感谢您的帮助!

这是代码:

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();
   }
  }  
 }
}

2 个答案:

答案 0 :(得分:0)

这次修改怎么样?

修改要点:

  • 在循环之前将图像导入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}});

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

答案 1 :(得分:0)

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

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