【问题标题】:Add Image to Google Sheets email App Script将图像添加到 Google 表格电子邮件应用脚本
【发布时间】:2018-03-09 17:29:59
【问题描述】:

我正在尝试在我的消息末尾添加一张图片,但我不知道该怎么做。我尝试添加内联图像,但因为我已经在使用 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();
   }
  }  
 }
}

【问题讨论】:

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


    【解决方案1】:

    这个修改怎么样?

    修改点:

    • 图片在for循环前导入blob。
    • 图片是使用&lt;img src="cid:image"&gt;&lt;br/&gt;和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}});。

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

    【讨论】:

    • 非常感谢!有用!一个快速的问题,如何调整图像大小?
    • @Katherine Malave Brasa 欢迎您。谢谢你让我知道。如果您的问题已解决,请按接受按钮。与您有相同问题的其他人也可以将您的问题作为可以解决的问题。如果找不到按钮,请随时告诉我。 stackoverflow.com/help/accepted-answer 对于您添加的问题,您可以使用此库调整其大小。 github.com/tanaikech/ImgApp这些信息对您有用吗?
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多