How to send several rows of google sheet table via email if cell match today's date

本秂侑毒 提交于 2021-02-11 15:47:22

问题


daily I have to send different qty of google sheet table rows (it depends on how many trucks were departed) via e-mail to a few recipients.

Could you help me, please, with an example of how to collect several rows of google table if cell match today's date and then send it via email?

For example, I need to grab all rows with today's date and send data from columns A, B, C, E via e-mail.

Thanks for any help in advance :)


回答1:


Let's go step by step. I'm supposing the column A contains the dates. Change it if it's not correct:

  1. Declare the variables we will use for this
function main() {  
  var sheet = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = sheet.getActiveSheet();
  var row_count = sheet.getLastRow() - 1;  
  var today = Utilities.formatDate(new Date(), "UTC", "dd.MM.yyyy"); //You can use the date format you prefer
  var data = sheet.getRange("A2:E"+ (row_count + 1)).getValues(); //We get all the values from A to E and from row 2 to the last one.

  countToday(sheet, today, row_count, data);
  sendEmails(data);


}
  1. Since we have each row in each position of data, we will compare each date with "today", and we will remove (splice) the rows from other days. The rows with valid dates will remain in the variable:
function countToday(sheet,today, row, data){

  var lastrow = "A" + (row + 1);
  var col_A = sheet.getRange('A2:'+lastrow).getValues();


  for (var i = row; i >= 0; i--){
    if (col_A[i] != today){
      data.splice(i, 1); //
    }

  }

}
  1. Now that we got each row of elements, we can send the email. We will create an html table so it will be easier to understand. The Mailapp function makes it very simple:

function sendEmails(data){

 MailApp.sendEmail({
    to: "example1@mail.com" + "example2@mail.com", //You can put as many emails you want
    subject: "Example",
    htmlBody:"<html><body>" + createTable(data)+ "</body></html>"});

}
  1. To create the html table, we just make a string coded in html with the names of the columns from the Sheet. Then we make a table array, where we will split each element from data separated by coma (that's each sheet cell) and just add it to the end of the variable. < /td> will create the rows for us.
function createTable(data){
  var cells = [];

  var table = "<html><body><br><table border=1><tr><th>Date</th><th>Column B</th><th>Column C</th><th>Column D</th><th>Column E</tr></br>";

  for (var i = 0; i < data.length; i++){
      cells = data[i].toString().split(",");
      table = table + "<tr></tr>";

      for (var u = 0; u < cells.length; u++){
          table = table + "<td>"+ cells[u] +"</td>";
      }
  }

  table=table+"</table></body></html>";
  return table;
}



回答2:


Here is my example of code. But I can't figure out how to implement preventing function for duplicating emails sending. EMAIL_SENT is working only with the "for" cycle. But in that case, if the cell doesn't contain "EMAIL_SENT" string, the email is sending as many times as there are the empty cells.

function main() {  
  var sheet = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = sheet.getSheetByName("HR-SI B2C");
  var row_count = sheet.getLastRow() - 1;  
  var today = Utilities.formatDate(new Date(), "UTC", "dd/MM/yyyy"); //You can use the 
  date format you prefer
  var data = sheet.getRange("A3:K"+ (row_count + 1)).getValues(); //We get all the 
  values from A to E and from row 2 to the last one.
  var lastrow = "A" + (row_count + 1);
  var col_A = sheet.getRange('A3:'+lastrow).getDisplayValues();
  var row = data[i];

for (var i = row_count; i >= 0; i--){

  if (col_A[i] != today){
  data.splice(i, 1);
  }
}

if (("K" + (row_count+2)) == "EMAIL_SENT"){

}else if(("D" + (row_count)) == "" || ("F" + (row_count)) == ""){
  dontSendEmail();
}else{
  sendEmailsQHNBRGR(data);

 }
}

//-----------------------------------------------------------------------------------

function dontSendEmail(){

 var today = Utilities.formatDate(new Date(), "UTC", "dd/MM/yyyy"); //You can use the 
  date format you prefer

 MailApp.sendEmail({
   to: "xxxxxxxxxx@gmail.com", //You can put as many emails you want
   subject: "Brak wpisanych ilości dla HR i SI za " + today,
   htmlBody:"<html><body> Witam,<br><br>" + 
   "<span><b>Proszę bardzo wpisać ilości dla HR i SI do tabeli</b></span>" +
   "</body></html>"});  

} 

//-----------------------------------------------------------------------------------

function sendEmailsQHNBRGR(data, platesNum){

  var sheet = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = sheet.getSheetByName("HR-SI B2C");
  var row_count = sheet.getLastRow() - 1;  
  var today = Utilities.formatDate(new Date(), "UTC", "dd/MM/yyyy"); //You can use the date format you prefer
  var data = sheet.getRange("A3:G"+ (row_count + 1)).getDisplayValues(); //We get all the values from A to D and from row 2 to the last one.  
  var cells = [];
  var table = "<html><body><table border=3><tr><th>Date</th><th>Day</th><th>HR [pcs]</th><th>HR [ep]</th><th>SI [pcs]</th><th>SI [ep]</th></tr>";
  var lastrow = "A" + (row_count + 1);
  var col_A = sheet.getRange('A3:'+lastrow).getDisplayValues();

  MailApp.sendEmail({
    to: "xxxxxxx@gmail.com", //You can put as many emails you want
    subject: "(HR+SI) Orsay prealert " + today,
    htmlBody:"<html><body> Dear All,<br><br>" + 
    "<span style=background-color:rgb(217,234,211)><b>Today we've sent e-com goods in listed quantity:</b></span>" +
    createTable(data) + "<br>" + 
    "<span style=background-color:rgb(255,242,204)><b>@DPD team, could you pick it up 
tomorrow at 02:00 PM?</b></span><br>" + 
    "</body></html>"});  

}

//-----------------------------------------------------------------------------------

 function createTable(data){

  var sheet = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = sheet.getSheetByName("HR-SI B2C");
  var row_count = sheet.getLastRow() - 1;  
  var today = Utilities.formatDate(new Date(), "UTC", "dd/MM/yyyy"); //You can use the date format you prefer
  var data = sheet.getRange("A3:G"+ (row_count + 1)).getDisplayValues(); //We get all the values from A to D and from row 2 to the last one.  
  var cells = [];
  var table = "<html><body><table border=3><tr><th>Date</th><th>Day</th> 
    <th>Plates number</th><th>HR [pcs]</th><th>HR [ep]</th><th>SI [pcs]</th> 
    <th>SI [ep]</th></tr>";
  var lastrow = "A" + (row_count + 1);
  var col_A = sheet.getRange('A3:'+lastrow).getDisplayValues();

  for (var i = row_count; i >= 0; i--){

    if (col_A[i] != today){
       data.splice(i, 1);
    }

  }

   for (var i = 0; i < data.length; i++){

      cells = data[i].toString().split(",");
      table = table + "<tr></tr>";

      for (var u = 0; u < cells.length; u++){
        table = table + "<td>"+ cells[u] +"</td>";
        sheet.getRange("K3:K"+ (row_count + 1)).setValue("EMAIL_SENT");

    }
  }

  table=table+"</table></body></html>";
  return table;

 }
}


来源:https://stackoverflow.com/questions/56989699/how-to-send-several-rows-of-google-sheet-table-via-email-if-cell-match-todays-d

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!