如果单元格与今天的日期匹配,如何通过电子邮件发送几行Google工作表

时间:2019-07-11 12:52:45

标签: google-apps-script google-sheets gmail

每天我必须通过电子邮件向其他收件人发送不同数量的Google工作表表格行(具体取决于出发的卡车数量)。

请帮助我,举例说明如何在单元格与当前日期匹配的情况下收集几行google表,然后通过电子邮件发送它。

例如,我需要获取具有今天日期的所有行,并通过电子邮件从A,B,C,E列发送数据。

非常感谢您的帮助:)

2 个答案:

答案 0 :(得分:0)

让我们一步一步走。我假设列A包含日期。如果不正确,请更改它:

  1. 声明我们将为此使用的变量
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. 由于我们在数据的每个位置都有每一行,因此我们将进行比较 每个带有“ today”的日期,我们将从中删除(拼接)行 其他日子。具有有效日期的行将保留在变量中:
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. 现在我们已经获得了每一行元素,我们可以发送电子邮件。我们将创建一个html表,以便于理解。 Mailapp函数使其非常简单:

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. 要创建html表,我们只需使用html编码一个字符串即可, 工作表中各列的名称。然后我们制作一个表数组, 我们将从data中将每个元素分隔为逗号(即每个工作表单元格),然后将其添加到变量的末尾。 将为我们创建行。
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;
}

答案 1 :(得分:0)

这是我的代码示例。但是我不知道如何实现防止重复发送邮件的功能。 EMAIL_SENT仅在“ for”循环中工作。但是在这种情况下,如果单元格中不包含“ EMAIL_SENT”字符串,则电子邮件发送的次数与空单元格一样。

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: "dymitrian.janowy@orsay.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: "dymitrian.janowy@orsay.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;

 }
}