Search code examples
htmlstringgoogle-apps-scriptgoogle-sheetsline-breaks

How can I add a line break in an email body after each row


I've created this script to send the none empty rows of a sheet as an email. So far it sends the email, but it places all the found rows in a single continuous line in the email body.

I've tried placing a line break <br/> in various places in the HTML part of the script but no luck so far.

 function sendSheetDataViaEmail() {
 var SSID = SpreadsheetApp.openById('1dfgdfgdf').getSheetByName('sheet1');
var lastRow = SSID.getLastRow();
 var cell = SSID.getRange(12, 1,lastRow-3,9).getDisplayValues();
var data2D = cell.filter(function(item) {//filter by user Id and by spliting month and year
       
return item[0] != "" // if not empty
});
  Array.prototype.transpose = function() { var a = this; return Object.keys(a[0]).map(function (c) { 
return a.map(function (r) { return r[c]; }); }); }
  
//transpose the array, because getValues() returns an array of rows
data2D = data2D.transpose();

// data2D.splice(1,1); // removes the second column... count starts at 0

//if now you need an array or rows again, just transpose again
data2D = data2D.transpose();

var L = data2D.length;

var html = "";

html = "<table id='loadListOfFaturasDeHojeTable' class='order-table table dark3Dtable'>   <thead><tr> 
 <th '>F. ID 🔃</th><th>Trans ID</th> <th>C. ID</th> <th >C. NOME</th> <th >CODDIGO</th> <th >NU DE 
   SERIE</th>   <th >VALOR</th><th >DATA</th>   </tr>  </thead>  <tbody>";


   for (var i=0;i<L;i++) {
  var thisRow = data2D[i];
  var rowHTML = "";
  rowHTML = "<tr>";
  var rowHTML0 =  '<td  style="color: red;">'+ thisRow[0] + '</td>'
  var rowHTML1 =  '<td >'+ thisRow[1] + '</td>'
  var rowHTML2 =  '<td >'+ thisRow[2] + '</td>'
  var rowHTML3 =  '<td >'+ thisRow[3] + '</td>' 
  var rowHTML4 =  '<td >'+ thisRow[4] + '</td>' 
  var rowHTML5 =  '<td >'+ thisRow[5] + '</td>' 
  var rowHTML6 =  '<td >'+ thisRow[6] + '</td>' 
  var rowHTML7 =  '<td >'+ thisRow[7] + '</td>'
  var rowHTML8 =  '<td ">'+ thisRow[8] + '</td>'

 
     rowHTML = rowHTML0 +rowHTML1+rowHTML3 + rowHTML4 + rowHTML5 + rowHTML6 + rowHTML7  + rowHTML8  + 
  "</tr><br/>";
   html = html  + rowHTML ;
  }

   //   Logger.log('html: ' + html)
  html = html + "</table>";


    var settings = {
    mailTo: SSID.getRange('J3')//email address
        .getValue(),
    subject: SSID.getRange('J4')
        .getValue(),
    body:  html,

   }

 MailApp.sendEmail(settings.mailTo, settings.subject,"" , {
    htmlBody: settings.body,

    });

   }

Solution

  • Try it this way:

    function sendSheetDataViaEmail() {
      var ss=SpreadsheetApp.openById('ssid');
      var sh=ss.getSheetByName('sheet1');
      var lastRow=sh.getLastRow();
      var vA=sh.getRange(12, 1,sh.getLastRow()-11,9).getDisplayValues().filter(function(item){return item[0]});
      var html="";
      html='<table id="loadListOfFaturasDeHojeTable" class="order-table table dark3Dtable"><tr><th>F.ID 🔃</th><th>TransID</th><th>C.ID</th><th>C.NOME</th><th>CODDIGO</th><th>NUDESERIE</th><th>VALOR</th><th>DATA</th></tr>';
      for(var i=0;i<vA.length;i++) {
        html+='<tr><td  style="color:red;">'+ thisRow[0] + '</td><td >'+ thisRow[1] + '</td><td >'+ thisRow[2] + '</td><td >'+ thisRow[3] + '</td>';
        html+='<td >'+ thisRow[4] + '</td><td >'+ thisRow[5] + '</td><td >'+ thisRow[6] + '</td><td >'+ thisRow[7] + '</td>'<td ">'+ thisRow[8] + '</td>';
      }
      MailApp.sendEmail(sh.getRange('J3').getValue(),sh.getRange('J4').getValue() ,"",{htmlBody:html});
    }