-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcheckReminder.gs
55 lines (42 loc) · 1.52 KB
/
checkReminder.gs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
function daysBetween( date1, date2 ) {
var one_day=1000*60*60*24;
date1.setHours(0,0,0,0);
date2.setHours(0,0,0,0);
var date1_ms = date1.getTime();
var date2_ms = date2.getTime();
var difference_ms = date2_ms - date1_ms;
return Math.round(difference_ms/one_day);
}
function checkReminder() {
// get the spreadsheet object
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
// set the first sheet as active
SpreadsheetApp.setActiveSheet(spreadsheet.getSheets()[0]);
// fetch this sheet
var sheet = spreadsheet.getActiveSheet();
// figure out what the last row is
var lastRow = sheet.getLastRow();
// the rows are indexed starting at 1, and the first row
// is the headers, so start with row 2
var startRow = 4;
// grab column 5 (the 'days left' column)
var range = sheet.getRange(startRow,1,lastRow-startRow+1,3 );
var numRows = range.getNumRows();
var grid = range.getValues();
var warning_count = 0;
var msg = "";
// Loop over the days left values
for (var i = 0; i <= numRows - 1; i++) {
var status = grid[i][0];
var days_between = grid[i][1] ? daysBetween(new Date(), grid[i][1]) : -1;
if(!status && (days_between == 7 || days_between == 2)) {
var reminder_name = grid[i][2];
msg = msg + "Reminder: "+reminder_name+" is due in "+days_between+" days.\n";
warning_count++;
}
}
if(warning_count) {
MailApp.sendEmail("[email protected],[email protected]",
"Reminder from To-do spreadsheet", msg);
}
};