Google表格脚本:将多个HTML格式的行作为一封电子邮件发送

时间:2018-02-16 20:01:53

标签: google-apps-script google-sheets

编辑:“代码2”完成我想要的,除非配额被击中。我删除了现在无关的“CODE 1”。

目标:要迭代多个电子表格行,请使用HTML格式化每一行,并将每行的格式化版本发送到一个电子邮件地址。

是什么问题?:

我可以遍历所有内容并将所有行合并到一封电子邮件中,但它会发送电子邮件直到配额被点击,现在我无法进一步测试。

为什么我是否尝试这样做?:创建更准确的MLA 8参考书目。想想一个更准确的EasyBib,我可以根据自己的喜好进行编辑。

WHO 其他人可以使用这个吗?:将邮件合并中的多行发送到一个地址很有用,但这种格式化解决方法也是如此。这让我可以使用HTML自动编辑字符串的一部分。使用内置函数(以及我可以通过脚本在此处找到的内容),无法将字符串附加在一起,单独格式化每个字符串并使输出保留每个字符串的原始格式。

如何我试图解决这个问题?:请参阅下面的两个代码。我开始使用HERE提供的解决方案。我试图限制处理的列数(以便电子邮件地址根本不相关),但这就是导致我达到配额的原因。这是我可以在大约3个小时内得到的,现在由于搞砸了并达到配额,我无法进一步测试。我不确定我错过了什么。

WHERE 可以试用吗?:如果有帮助,HERE是相关工作表的可编辑副本。

免责声明:我是新手,所以用小词慢慢说话哈哈。另外,如果我不马上回来,我很抱歉,接下来的12个小时左右即将变得有点紧张。 严肃地说,我感谢能帮助我指明正确方向的任何帮助!

CODE 2

//This constant is used to format the date and time as "09/20/1900 11:20 AM"
var formattedDate = Utilities.formatDate(new Date(),"EST", "MM/dd/yyyy hh:mm a");

// var EMAIL_SENT = ("Email Sent");


function mlaTest2() {

// Sheet Information
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = SpreadsheetApp.setActiveSheet(ss.getSheetByName('test'));
  var startRow = 3;  
  var startCol = 21; 
  var numRows = sheet.getLastRow()-1;  
  var numCols = 14; 


  var dataRange = sheet.getRange(startRow, startCol, numRows, numCols);
  var dataRange2 = sheet.getRange(3,22,numRows,3);

  var message = '';
  var html = '';

// Fetch values for each row in the Range.
  var cells = dataRange.getValues();
  Logger.log(cells);

  //var i in cells
  for (var i = 0; i < cells.length; ++i) {

    var column = cells[i];
    var colU = column[0];           
    var colV = column[1];           
    var colW = column[2];           
    var colX = column[3];           
    var colY = column[4];          
    var colZ = column[5];           
    var colAA = column[6];          
    var colAB = column[7];           
    var colAC = column[8];
    var colAD = column[9];
    var colAE = column[10];
    var colAF = column[11];
    var colAG = column[12];
    var colAH = column[13];

// The columns below don't contain citation data    
//    var colAI = column[14];
//    var colAJ = column[15];
//    var colAK = column[16];

      var author1 = colU;
      var author2 = colV;
      var author3 = colW;
      var title1Quotes = colX;
      var title2Italics = colY;
      var containerItalics = colZ;
      var contribution1 = colAA;
      var contribution2 = colAB;
      var contribution3 = colAC;
      var version = colAD;
      var number = colAE;
      var publisher = colAF;
      var pubDate = colAG;
      var location = colAH;

 // The columns belw don't contain citation data
      var emailAddress = 'nathaniel.omar.davis@gmail.com';
   //   var sent = colAJ;
      var subject = "test";

  //  if (sent != EMAIL_SENT) {  // Prevents sending duplicates

     // Format text as html   
     html +=
      '<p>' + author1 +  
      author2 + 
      author3 + 
      title1Quotes + 
      '<i>' + title2Italics + containerItalics + '</i>' + 
      contribution1 + 
      contribution2 + 
      contribution3 + 
      version + 
      number + 
      publisher + 
      pubDate +
      location+
      '<br> </p>'
      Logger.log(html);
      ;

      var replyTo = 'coachnod@outlook.com'
      var bcc = ''
      var cc = ''
      var sender = 'Nate Davis'


//Send the emails
      MailApp.sendEmail(emailAddress, subject, message, {
      name: sender, 
      replyTo: replyTo, 
      htmlBody: html
      });

//Update the sheet
 //     sheet.getRange(startRow + i, 36).setValue(EMAIL_SENT);
 //     sheet.getRange(startRow + i, 37).setValue(formattedDate);
      SpreadsheetApp.flush();
    }
  }

1 个答案:

答案 0 :(得分:0)

问题澄清编辑: MailApp.sendMail()函数位于for循环内,因此脚本将尝试在for循环的每次迭代中发送电子邮件。将sendMail()功能移出for循环只会发送一封电子邮件。

旁注:同样在html + =赋值内,您在语句结束前有Logger.log(html);

function mlaTest2() {

  // Sheet Information
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = SpreadsheetApp.setActiveSheet(ss.getSheetByName('test'));
  var startRow = 3;  
  var startCol = 21; 
  var numRows = sheet.getLastRow()-1;  
  var numCols = 14; 


  var dataRange = sheet.getRange(startRow, startCol, numRows, numCols);
  var dataRange2 = sheet.getRange(3,22,numRows,3);

  var message = '';
  var html = '';

  // Fetch values for each row in the Range.
  var cells = dataRange.getValues();
  Logger.log(cells);

  //var i in cells
  for (var i = 0; i < cells.length; ++i) {

    var column = cells[i];
    var colU = column[0];           
    var colV = column[1];           
    var colW = column[2];           
    var colX = column[3];           
    var colY = column[4];          
    var colZ = column[5];           
    var colAA = column[6];          
    var colAB = column[7];           
    var colAC = column[8];
    var colAD = column[9];
    var colAE = column[10];
    var colAF = column[11];
    var colAG = column[12];
    var colAH = column[13];

    // The columns below don't contain citation data    
    //var colAI = column[14];
    //var colAJ = column[15];
    //var colAK = column[16];

      var author1 = colU;
      var author2 = colV;
      var author3 = colW;
      var title1Quotes = colX;
      var title2Italics = colY;
      var containerItalics = colZ;
      var contribution1 = colAA;
      var contribution2 = colAB;
      var contribution3 = colAC;
      var version = colAD;
      var number = colAE;
      var publisher = colAF;
      var pubDate = colAG;
      var location = colAH;

      //The columns belw don't contain citation data
      var emailAddress = 'nathaniel.omar.davis@gmail.com';
     //var sent = colAJ;
      var subject = "test";

     //if (sent != EMAIL_SENT) {  // Prevents sending duplicates

     //Format text as html   
     html +=
      '<p>' + author1 +  
      author2 + 
      author3 + 
      title1Quotes + 
      '<i>' + title2Italics + containerItalics + '</i>' + 
      contribution1 + 
      contribution2 + 
      contribution3 + 
      version + 
      number + 
      publisher + 
      pubDate +
      location+
      '<br> </p>';
      Logger.log(html);

      var replyTo = 'coachnod@outlook.com'
      var bcc = ''
      var cc = ''
      var sender = 'Nate Davis'

//Update the sheet
 //     sheet.getRange(startRow + i, 36).setValue(EMAIL_SENT);
 //     sheet.getRange(startRow + i, 37).setValue(formattedDate);
      SpreadsheetApp.flush();
    } //FOR LOOP ENDS HERE

  //SEND ONE EMAIL
  MailApp.sendEmail(emailAddress, subject, message, {
    name: sender, 
    replyTo: replyTo, 
    htmlBody: html
  });
}