Google脚本 - 仅当今天相等时才从电子表格中提取行数据

时间:2016-11-29 18:57:03

标签: javascript date google-apps-script google-sheets

情况:
我有以下脚本给我发送一封关闭机票#的电子邮件。

问题:

此脚本向我发送任何截止日期的所有已关闭的门票。我需要你只寄给我当天关闭的门票。

关闭日期列的格式为" 29/11/2016 12:34:00"

  function emailv2() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var Support = ss.getSheetByName("Historic");
  var lastrow = Support.getLastRow();

  var data1 = Support.getRange(3, 1, lastrow).getValues(); // Ticket# Column
  var data2 = Support.getRange(3, 2, lastrow).getValues(); // Comment Column
  var data3 = Support.getRange(3, 3, lastrow).getValues(); // Status Column
  var data4 = Support.getRange(3, 9, lastrow).getValues(); // Close Date Column

  var date = Utilities.formatDate(new Date(), "GMT-3", "dd/MM/yyyy HH:mm"); // Funtion Date + Format

  var report = " "
  var supportTable ="";
  for(var i in data3){
    if(data3[i]=='Closed') supportTable += "<tr><td style='color:blue;padding:5px'>" + "<a href=http://otrserver/otrs/index.pl?Action=AgentTicketZoom;TicketNumber="+data1[i]+">"+data1[i]+"</a>" 
                                      + "</td><td style='padding:5px'>" + data2[i] 
                                      + "</td><td style='padding:5px'>" + data3[i] 
                                      + "</td></tr>";
  }

  report ="Estimados: <br><br> Les envio el shift <br><br><table style='border-collapse:collapse;'border = 1 cellpadding = 5; align='center'><tr><th style='background-color:black;color:white'>Ticket#</th><th style='background-color:black;color:white'>Comment</th><th style='background-color:black;color:white'>Status</tr>" + supportTable + "</table>";

  MailApp.sendEmail("example@gmail.com",
                    "Shift Change -" + date, 
                    report, 
                    {name: 'Automatic Emailer Script',
                    htmlBody: report});
}

2 个答案:

答案 0 :(得分:1)

我添加了一个测试日期的函数,并编辑了if语句以便使用它:

function emailv2() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var Support = ss.getSheetByName("Historic");
  var lastrow = Support.getLastRow();

  var data1 = Support.getRange(3, 1, lastrow).getValues(); // Ticket# Column
  var data2 = Support.getRange(3, 2, lastrow).getValues(); // Comment Column
  var data3 = Support.getRange(3, 3, lastrow).getValues(); // Status Column
  var data4 = Support.getRange(3, 9, lastrow).getValues(); // Close Date Column

  var date = new Date()

  var report = " "
  var supportTable ="";
  for(var i in data3){
    if((data3[i]=='Closed') && (testDate(data4[i][0], date))) supportTable += "<tr><td style='color:blue;padding:5px'>" + "<a href=http://drsc.nubicua.net/otrs/index.pl?Action=AgentTicketZoom;TicketNumber="+data1[i]+">"+data1[i]+"</a>" 
                                      + "</td><td style='padding:5px'>" + data2[i] 
                                      + "</td><td style='padding:5px'>" + data3[i] 
                                      + "</td></tr>";

  }

  report ="Estimados: <br><br> Les envio el shift <br><br><table style='border-collapse:collapse;'border = 1 cellpadding = 5; align='center'><tr><th style='background-color:black;color:white'>Ticket#</th><th style='background-color:black;color:white'>Comment</th><th style='background-color:black;color:white'>Status</tr>" + supportTable + "</table>";

  MailApp.sendEmail("example@gmail.com",
                    "Shift Change -" + date, 
                    report, 
                    {name: 'Automatic Emailer Script',
                    htmlBody: report});
}

function testDate (dateToTest1,dateToTest2) {
    return (dateToTest1.getFullYear() === dateToTest2.getFullYear()) &&
           (dateToTest1.getMonth() === dateToTest2.getMonth()) &&
           (dateToTest1.getDate() == dateToTest2.getDate());
}

旧回应

更改您的IF以比较日期。如果两者都是日期,则将它们格式化为日期,没有时间

var date = Utilities.formatDate(new Date(), "GMT-3", "dd/MM/yyyy"); // Function Date + Format

然后将If更改为:

if((data3[i]=='Closed') && (date === data4))

请注意,这是未经测试的,因为我没有您的电子表格的副本,但应该得到您所追求的。

答案 1 :(得分:0)

由于电子表格以日期格式存储值,您可以使用简单的日期方法将存储的日期与今天的日期进行比较,而不是将它们转换为字符串。

  var today = new Date();
  today.setHours(0,0,0,0);

  var status = Support.getRange(3, 3, lastrow).getValue(); // Status Column
  var closed = Support.getRange(3, 9, lastrow).getValue(); // Close Date Column
  closed.setHours(0,0,0,0);

  if ((status === "CLOSED") && (today - closed === 0))
    // do processing