使用脚本应用程序从Google表格中的单元格中提取日期

时间:2016-11-10 16:12:59

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

作为免责声明,我对编程概念很陌生。如果你真的想成为我最好的朋友,解释越具体越好。但任何帮助表示赞赏。

我的小型企业使用QuickBooks进行工资核算,但出于后勤目的,我们让员工使用谷歌电子表格输入他们的时间。

我想编写一个脚本,每天两次检查第一列日期(根据我设置的触发器)。如果日期等于或大于今天的日期,我需要脚本也检查同一行中几列的单元格以查看该单元格是否已填写。如果它是0或null,我想发送通知。

请参阅下面的图片以供参考。

Test

到目前为止,这就是我所拥有的:

function values2() 
{
    var ss = SpreadsheetApp.getActiveSpreadsheet(),
      sheet = ss.getSheetByName("TimeSheet"),
      range = sheet.getDataRange(),
      values = range.getValues(),
      indx, logToday, today, body,
      recipients = "xxxxx@xxx.zzz",
      subject = "Timesheet Notification" + ss.getName();

//Finds current date and creates string. I would prefer to have a numeric value associated to the current date, as well as a numeric value associated with the date for each row.
  var CurrentDate = new Date();
  Logger.log(CurrentDate);
      logToday = Logger.getLog();
      indx = logToday.indexOf("INFO");
      today = logToday.substring(indx + 6, indx + 16);

//I'm having things sent to my email for test purposes
  MailApp.sendEmail(recipients, subject, today);  
//Clears Log
  Logger.clear();
  for (var r=1; r<values.length; r++) 
  {
  var row = values[r],
      date = row[0],
      price = row[1],
      units = row[2],
      total = row[3],
      name = row[4];

   Logger.log(date);  
   Logger.log(price);
   Logger.log(units),
   Logger.log(total),
   Logger.log(name);
  }

  body = Logger.getLog();
  MailApp.sendEmail(recipients, subject, body);
}

此时,我希望能够从第一列中捕获每个日期,将其与今天的日期进行比较,检查同一行中的另一个单元格,然后转到第一列中的下一个日期。我玩过IF / Else的声明,但无济于事。

1 个答案:

答案 0 :(得分:0)

如果你想将包含空单元格的第一列的日期元素与今天进行比较,那么下面的脚本怎么样?

function values2() 
{
    var ss = SpreadsheetApp.getActiveSpreadsheet(),
      sheet = ss.getSheetByName("TimeSheet"),
      range = sheet.getDataRange(),
      values = range.getValues(),
      indx, logToday, today, body,
      recipients = "xxxxx@xxx.zzz",
      subject = "Timesheet Notification" + ss.getName();

    var lR = sheet.getLastRow();
    var dateinfo = sheet.getRange('a1').offset(0, 0, lR, 1).getValues();
    var today = new Date();
    var y0 = today.getFullYear();
    var m0 = today.getMonth() + 1;
    var d0 = today.getDate();

    for (var i=0; i<dateinfo.length; i++){
        x = Date.parse(dateinfo[i]);
        var date = new Date(x);
        var y = date.getFullYear();
        var m = date.getMonth() + 1;
        var d = date.getDate();
        if (y0 === y && m0 === m && d0 === d) {
            Logger.log("ok:" + i) // same date
        } else {
            Logger.log("error:" + i)  // difference date
        };
    };
}