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

问题描述 投票:0回答:1

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

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

问题是什么?:

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

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

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

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

你可以尝试一下吗?:如果它有帮助,HERE是有问题的工作表的可编辑副本。

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

代码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 = '[email protected]';
   //   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 = '[email protected]'
      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();
    }
  }
google-apps-script google-sheets
1个回答
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 = '[email protected]';
     //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 = '[email protected]'
      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
  });
}
© www.soinside.com 2019 - 2024. All rights reserved.