2016-11-10 95 views
0

作爲免責聲明,我對編程概念頗爲陌生。如果你真的想成爲我最好的朋友,那麼解釋越具體越好。但任何幫助表示讚賞。使用腳本應用從Google表格中的單元格拉取日期

我的小型企業使用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 = "[email protected]", 
     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的聲明,但無濟於事。

+0

向我們展示您的嘗試和出錯 –

回答

0

如果你想比較第一列包含空單元格的日期元素到今天,那麼下面的腳本怎麼樣?

function values2() 
{ 
    var ss = SpreadsheetApp.getActiveSpreadsheet(), 
     sheet = ss.getSheetByName("TimeSheet"), 
     range = sheet.getDataRange(), 
     values = range.getValues(), 
     indx, logToday, today, body, 
     recipients = "[email protected]", 
     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 
     }; 
    }; 
} 
+0

謝謝。經過大量的探索,這就是我想出的。對不起,我以前沒有看到你的帖子,K. Tanaike。 – KayZak

相關問題