简体   繁体   中英

Return empty date field when sending it by email (Google Spreadsheet)

I got the below code running ok, but when a date field is empty, I get the email with related field containing 01/01/1970. How can I insert a condition that prevents these date fields from getting filled in?

function sendEmailbyTransporter() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Status de Embarque Integrado");

  // Get your table data
  var startRow = 27;  // First row of data to process
  var numRows = sheet.getLastRow(); // Number of rows to process
  var dataRange = sheet.getRange(startRow, 1, numRows, 12); // Fetch the range including the Minimums
  var data = dataRange.getValues(); // Fetch values for each row in the Range.

  // Loop through the data to build your table
  var message = "<html><body><table style=\"text-align:center\"><tr><th>NF</th><th>CT-E</th><th>Cliente</th><th>Cidade</th><th>UF</th><th> Previsão de Entrega</th><th>Ocorrência</th><th>Detalhe da Ocorr.</th><th>Data da Entrega</th>";
  var sendEmail = false; // If there is content to send, will be set to true
  for (var i = 0; i < data.length; ++i) {
    var rowData = data[i];
    var NF = rowData[0];
    var Email = sheet.getRange("I24:l24").getValue();
    if (NF != "") { //Checks for blank Code. If blank, then skip and don't add to the table.
      var CTE = rowData[1];
      var CTEAsDate = Utilities.formatDate(CTE, "GMT" , "dd/MM/yyyy" );
      var Cliente = rowData[4];
      var Cidade = rowData[5];
      var UF = rowData[6];
      var PrevEntrega = new Date(rowData[7]);
      var PrevEntregaAsDate = Utilities.formatDate(PrevEntrega, "GMT" , "dd/MM/yyyy" );
      var Ocorr1 = rowData[10];
      var Ocorr2 = rowData[11];
      var Entrega = new Date(rowData[12]);
      var EntregaAsDate = Utilities.formatDate(Entrega, "GMT" , "dd/MM/yyyy" );

        message += "<tr><td>"+NF+"</td><td style=\"text-align:left\">"+CTE+"</td><td>"+Cliente+"</td><td>"+Cidade+"</td><td>"+UF + "</td><td>"+PrevEntregaAsDate+"</td><td>"+Ocorr1 + "</td><td>"+Ocorr2 + "</td><td>"+EntregaAsDate + "</td></tr>";
        sendEmail = true; //There is content to send
    }
  }
  message += "</table></body></html>";
    if (sendEmail) {
      var subject = "Status de Embarque da NF " + NF + "!";
      MailApp.sendEmail({
        name: 'XXXl',
        to: Email, 
        subject: subject, 
        htmlBody: message
      });
   }
}

The reason you are getting is the rowData[7] is null , you can add a check for null value before date calculation

if(rowData[7]===null){
 // do anything you want to handle this condition
}

Here the above check , validates the null value , in your case you are not handling the null value, hence

The following condition :

var PrevEntrega = new Date(rowData[7]);

Evaluates to

var PrevEntrega = new Date(null);

giving the result :

Thu Jan 01 1970 05:30:00 GMT+0530 (India Standard Time)

So here is your updated code :

  // Loop through the data to build your table
  var message = "<html><body><table style=\"text-align:center\"><tr><th>NF</th><th>CT-E</th><th>Cliente</th><th>Cidade</th><th>UF</th><th> Previsão de Entrega</th><th>Ocorrência</th><th>Detalhe da Ocorr.</th><th>Data da Entrega</th>";
  var sendEmail = false; // If there is content to send, will be set to true
  for (var i = 0; i < data.length; ++i) {
    var rowData = data[i];
    var NF = rowData[0];
    var Email = sheet.getRange("I24:l24").getValue();
    if (NF != "") { //Checks for blank Code. If blank, then skip and don't add to the table.
      var CTE = rowData[1];
      var CTEAsDate = Utilities.formatDate(CTE, "GMT" , "dd/MM/yyyy" );
      var Cliente = rowData[4];
      var Cidade = rowData[5];
      var UF = rowData[6];
      var PrevEntregaAsDate ="";
      if(rowData[7]){
         var PrevEntrega = new Date(rowData[7]);
         PrevEntregaAsDate = Utilities.formatDate(PrevEntrega, "GMT" , "dd/MM/yyyy" );
      }
      var Ocorr1 = rowData[10];
      var Ocorr2 = rowData[11];
      var EntregaAsDate = "";
      if(rowData[12]){
          var Entrega = new Date(rowData[12]);
          EntregaAsDate = Utilities.formatDate(Entrega, "GMT" , 
          "dd/MM/yyyy" );
      }

        message += "<tr><td>"+NF+"</td><td style=\"text-align:left\">"+CTE+"</td><td>"+Cliente+"</td><td>"+Cidade+"</td><td>"+UF + "</td><td>"+PrevEntregaAsDate+"</td><td>"+Ocorr1 + "</td><td>"+Ocorr2 + "</td><td>"+EntregaAsDate + "</td></tr>";
        sendEmail = true; //There is content to send
    }
  }
  message += "</table></body></html>";
    if (sendEmail) {
      var subject = "Status de Embarque da NF " + NF + "!";
      MailApp.sendEmail({
        name: 'XXXl',
        to: Email, 
        subject: subject, 
        htmlBody: message
      });
   }
}

If the date is null , new Date(null) will return the epoch time - 01/01/1970 (from which the current time is calculated). Just calculate the date only if it is present.

var EntregaAsDate = "";
if(rowdata[12])
{
    var Entrega = new Date(rowData[12]);
    EntregaAsDate = Utilities.formatDate(Entrega, "GMT" , "dd/MM/yyyy" );
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM