-
Notifications
You must be signed in to change notification settings - Fork 0
/
contacts_birthdays_to_calendar.gs
67 lines (51 loc) · 1.98 KB
/
contacts_birthdays_to_calendar.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
56
57
58
59
60
61
62
63
64
65
66
67
function contactsBirthdayToCalendar() {
const myContacts = ContactsApp.getContacts();
const myCalendar = CalendarApp.getDefaultCalendar();
const currentYear = new Date().getFullYear();
const currentMonth = new Date().getMonth()+1; //month id goes from 0 to 11
for (var i=0; i<myContacts.length; i++) {
var birthday = myContacts[i].getDates(ContactsApp.Field.BIRTHDAY);
if (birthday.length>0) {
var day = birthday[0].getDay().toString();
var contactMonth = birthday[0].getMonth();
var month = monthToNumber(contactMonth);
if ((month == currentMonth & day != 1) |
(month == currentMonth+1 & day == 1) |
(month == 1 & currentMonth == 12 & day == 1)) {
var name = myContacts[i].getFullName().toString();
var birthdate = new Date(currentYear, month-1, day); //needs month id
var event = myCalendar.createAllDayEvent('cumple ' + name, //set the name of the event
birthdate,
new Date(birthdate).setDate(birthdate + 1));
//set the event color in calendar. More options here: https://developers.google.com/apps-script/reference/calendar/event-color.html
event.setColor("6");
event.removeAllReminders();
//set popup and email reminders (minutes before event)
event.addPopupReminder(480);
event.addEmailReminder(480);
}
}
}
}
function monthToNumber(m) {
const months = ContactsApp.Month;
const monthToEnum = {
1: months.JANUARY,
2: months.FEBRUARY,
3: months.MARCH,
4: months.APRIL,
5: months.MAY,
6: months.JUNE,
7: months.JULY,
8: months.AUGUST,
9: months.SEPTEMBER,
10: months.OCTOBER,
11: months.NOVEMBER,
12: months.DECEMBER
};
for (const [key, value] of Object.entries(monthToEnum)) {
if (value === m) {
return key;
}
}
}