-
Notifications
You must be signed in to change notification settings - Fork 1
/
nGCDE.js
139 lines (116 loc) · 5.21 KB
/
nGCDE.js
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/****************************************************************
* USER CONFIG
****************************************************************/
// who should the e-mail go to
var EMAIL_TO = "[email protected]";
// how many previous and next days to also show
var NUM_ADDITIONAL_DAYS = 8;
/****************************************************************
* CODE
****************************************************************/
var TODAY = new Date((new Date()).toDateString());
var ONE_DAY = 24 * 60 * 60 * 1000;
// gets the dates
function getContactDates()
{
var todayYear = TODAY.getFullYear();
var todayNumber = TODAY.getTime();
// date we want to go back to for previous dates
var previousNumber = todayNumber + (NUM_ADDITIONAL_DAYS * -1 * ONE_DAY);
// date we want to go forward to for future dates
var futureNumber = todayNumber + (NUM_ADDITIONAL_DAYS * ONE_DAY);
// get all contacts
var allContacts = ContactsApp.getContacts();
var allContactDates = {};
var justDates = [];
// go through all contacts
for(var i = 0, numContacts = allContacts.length; i < numContacts; ++i)
{
var oneContact = allContacts[i];
// get contact details
var contactName = oneContact.getFullName();
var contactNickName = oneContact.getNickname();
var contactEMail = oneContact.getPrimaryEmail();
var contactPhoneNumber = oneContact.getPhones(ContactsApp.Field.MOBILE_PHONE);
var contactDates = oneContact.getDates();
var contactInformation = [];
if(contactEMail) contactInformation.push(contactEMail);
if(contactPhoneNumber.length) contactInformation.push("<a href='tel:" + contactPhoneNumber[0].getPhoneNumber() + "'>" + contactPhoneNumber[0].getPhoneNumber() + "</a>");
contactInformation = contactInformation.join(", ");
// go through all the dates for this contact
for(var j = 0, numDates = contactDates.length; j < numDates; ++j)
{
var oneDate = contactDates[j];
var dateLabel = oneDate.getLabel().toString().toLowerCase();
var dateDate = new Date(todayYear, oneDate.getMonth().ordinal(), oneDate.getDay());
var dateNumber = dateDate.getTime();
var age = todayYear - oneDate.getYear();
if(!(dateNumber in allContactDates))
{
justDates.push(dateNumber);
allContactDates[dateNumber] = {"dateDate" : dateDate, "contactDateDetails" : []};
}
var contactDateDetails = [];
contactDateDetails.push(contactName + (contactNickName ? " (" + contactNickName + ")" : ""));
contactDateDetails.push(dateLabel);
contactDateDetails.push(age + " years");
if(contactInformation) contactDateDetails.push(contactInformation);
allContactDates[dateNumber]["contactDateDetails"].push(contactDateDetails.join('<br />'));
}
}
// sort them
justDates.sort();
var message = "";
// print today's dates
if(todayNumber in allContactDates) message += printDateList(allContactDates, [todayNumber]);
else message += 'No Events Today';
message += '<hr />';
// print next NUM_ADDITIONAL_DAYS number of future dates
message += printDateList(allContactDates, justDates.filter(function(dateNumber)
{
return dateNumber > todayNumber && dateNumber <= futureNumber;
}));
message += '<hr />';
// print previous NUM_ADDITIONAL_DAYS number of past dates
message += printDateList(allContactDates, justDates.filter(function(dateNumber)
{
return dateNumber < todayNumber && dateNumber >= previousNumber;
}).reverse());
message += '<hr />';
message += '<hr />';
message += '<hr />';
// print all everything
message += printDateList(allContactDates, justDates);
// send the email
MailApp.sendEmail({
"to" : EMAIL_TO,
"subject" : "Today's Special Dates - " + TODAY.toISOString().split("T")[0] + " - " + TODAY.toDateString(),
"htmlBody" : message
});
}
// prints the dates
function printDateList(contactDates, printList)
{
var out = "";
for(var i = 0, numDates = printList.length; i < numDates; ++i)
{
var oneDate = contactDates[printList[i]];
var dateDate = oneDate["dateDate"];
var contactDateDetails = oneDate["contactDateDetails"];
out += dateDifference(TODAY, dateDate) + ' - ' + dateDate.toDateString();
out += '<ul><li>' + contactDateDetails.join('<br /><br /></li><li>') + '</li></ul>';
}
return out;
}
// returns a pretty date difference (e.g. Today, Tomorrow, Yesterday, etc...)
function dateDifference(startDate, endDate)
{
var numDaysBetween = Math.round((endDate.getTime() - startDate.getTime())/ONE_DAY);
if(numDaysBetween == 0) return "Today";
if(numDaysBetween == 1) return "Tomorrow";
if(numDaysBetween == -1) return "Yesterday";
if(numDaysBetween == 2) return "Day After Tomorrow";
if(numDaysBetween == -2) return "Day Before Yesterday";
if(numDaysBetween > 2) return "in " + numDaysBetween + " days";
return (numDaysBetween * -1) + " days ago";
}