-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCode.js
More file actions
211 lines (147 loc) · 7.49 KB
/
Code.js
File metadata and controls
211 lines (147 loc) · 7.49 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// Provided for free by GCalTools www.gcaltools.com. Use at your own risk!
// VIDEO: https://youtu.be/8GrGT8SWs-8?si=4ZdRLjQTOx7vApET
// INSTRUCTIONS to Create Birthdays and Special Events from Google Contacts in any Google Calendar
// 1) Make a COPY of this project
// 2) Configure the settings for this script under "CONFIGURATION" below AND CLICK "SAVE PROJECT" ABOVE BEFORE YOU RUN!
// 3) Make sure "updateBirthdays" is selected in the menu above, then click "Run"
// 4) Click 'Advanced' during warnings to proceed, give permissions (to your own account, not mine!)
// 5) DONE! You can run this script again to add new birthdays without creating duplicates
// 6) Optional: Create a "Trigger" to run updateBirthdays daily to add birthdays from new contacts (see VIDEO linked above)
// ******** CONFIGURATION START : CLICK "SAVE PROJECT" ABOVE BEFORE YOU RUN! ********
// REQUIRED:
// CHANGE THIS to 'var useOriginalBirthdayCalendar = true' TO USE GOOGLE's DEFAULT BIRTHDAY CALENDAR
// THIS WILL CREATE DUPLICATES BIRTHDAYS/SPECIAL EVENTS IF YOU HAVE ENABLED SYNCING FROM CONTACTS IN BIRTHDAY SETTINGS
var useOriginalBirthdayCalendar = false;
// CHANGE THIS ID TO THE CALENDAR ID FOR THE CALENDAR YOU WANT TO USE
// THIS WILL BE IGNORED IF useOriginalBirthdayCalendar ABOVE IS true
var calendarId = "xxxxxxxxxxxxxxxxxxxxxxx@group.calendar.google.com";
// ALL SETTINGS BELOW ARE OPTIONAL:
// CHANGE THIS TO THE NAME YOU WANT WHEN THERE IS NO LABEL FOR THE SPECIAL EVENT
var noLabelTitle = "Special Event";
// CHANGE THIS TO 'var onlyBirthdays = true' IF YOU ONLY WANT TO COPY BIRTHDAYS, NOT SPECIAL EVENTS
var onlyBirthdays = false;
// CHANGE THIS TO 'var onlyContactLabel = true' IF YOU ONLY WANT TO COPY BIRTHDAYS FOR CONTACTS WITH A SPECIFIC LABEL
// DON'T FORGET TO SET THE contactLabelID BELOW IF THIS IS true
// USEFUL IF YOU HAVE TOO MANY CONTACTS AND THE SCRIPT WON'T FINISH
var onlyContactLabel = false;
// TO GET THE contactLabelID OPEN https://contacts.google.com/ CLICK YOUR LABEL AND NOTE THE PAGE ADDRESS
// THE LAST PART OF THE ADDRESS IS THE contactLabelID: https://contacts.google.com/label/[contactLabelID]
var contactLabelID = "xxxxxxxxxxxxxx";
// REMINDER IN MINUTES
// Use the following variables to configure your reminders
// Set to 0 to disable a particular reminder type
// Set useDefaultReminders to true to use the calendar's default reminder settings
// USE CALENDAR'S DEFAULT REMINDERS?
// WARNING: GOOGLE REMINDERS BUGS!
// When using Original Birthday Calendar with Default Reminders, the Main Calendar 'Event notifications' will be applied, NOT 'All Day Event notifications' as you'd expect - and also NOT the 'Birthday' calendar reminder settings!
// When using a Secondary Calendar, that calendar's default reminders will be correctly applied
// PREFER USING DEFAULT REMINDERS SO YOU CAN CHANGE THEM IN THE CALENDAR SETTINGS LATER.
var useDefaultReminders = true;
// EMAIL REMINDERS (set to 0 to disable)
var emailReminder1 = 0; // minutes before (e.g., 60 for 1 hour, 1440 for 1 day)
var emailReminder2 = 0; // minutes before (second email reminder, 0 to disable)
// POPUP REMINDERS (set to 0 to disable)
var popupReminder1 = 60 * 12; // 12 hours before (720 minutes)
var popupReminder2 = 0; // minutes before (second popup reminder, 0 to disable)
// For hours/days, here are some examples:
// 30 minutes = 30
// 1 hour = 60
// 12 hours = 60 * 12 = 720
// 1 day = 60 * 24 = 1440
// 2 days = 60 * 24 * 2 = 2880
// 1 week = 60 * 24 * 7 = 10080
// *** DON'T MODIFY THIS SECTION ***
// This creates the reminders array from your settings above
var reminders = [];
if (!useDefaultReminders) {
if (popupReminder1 > 0) reminders.push({ method: "popup", minutes: popupReminder1 });
if (popupReminder2 > 0) reminders.push({ method: "popup", minutes: popupReminder2 });
if (emailReminder1 > 0) reminders.push({ method: "email", minutes: emailReminder1 });
if (emailReminder2 > 0) reminders.push({ method: "email", minutes: emailReminder2 });
}
// *** END OF REMINDERS CONFIGURATION ***
// ADDITIONAL CONFIGURATION OPTIONS:
// CHANGE THIS TO true TO PREVIEW CHANGES WITHOUT ACTUALLY CREATING/DELETING EVENTS
// Or run the "dryRunUpdate" function to see what would be changed
var dryRun = false;
// CUSTOMIZE EVENT TITLE FORMATS (leave blank to use default format)
// Available variables: {name} - contact's name, {eventType} - the type of event
var birthdayTitleFormat = ""; // Default: "{name}'s Birthday"
var specialEventTitleFormat = ""; // Default: "{name}'s {eventType}"
// ADD CUSTOM DESCRIPTIONS TO EVENTS (Only possible if you are using a secondary calendar)
var addCustomDescriptions = false;
var birthdayDescription = "Birthday celebration for {name}";
var specialEventDescription = "{eventType} for {name}";
// FILTER EVENTS BY DATE RANGE
// USEFUL IF YOU HAVE TOO MANY CONTACTS AND THE SCRIPT WON'T FINISH
// Set specific months or days to include, or leave empty [] for all
// Examples:
// var filterMonths = [1, 2]; // Only January and February
// var filterMonths = [12]; // Only December
// var filterMonths = []; // All months (default)
var filterMonths = []; // 1-12 for specific months, empty array for all months
// Examples:
// var filterDays = [1, 15, 31]; // Only 1st, 15th and 31st days of the month
// var filterDays = [25]; // Only 25th day of the month
// var filterDays = []; // All days (default)
var filterDays = []; // 1-31 for specific days, empty array for all days
// MORE FLEXIBLE CLEANUP OPTIONS
var deleteSearchPattern = ""; // Custom text to search for when deleting events in secondary calendars (empty for default)
var deleteOnlyFutureEvents = false; // Set to true to keep past events when deleting
// ******** CONFIGURATION END : CLICK "SAVE PROJECT" ABOVE BEFORE YOU RUN! ********
// DO NOT EDIT BELOW THIS LINE UNLESS YOU UNDERSTAND WHAT YOU ARE DOING
/**
* Displays the current configuration in the Execution Log
*/
function showConfiguration() {
return GCalTools.showConfiguration();
}
/**
* Displays what would be changed in the Execution Log without editing the calendar
* (useful for testing)
*/
function dryRunUpdate() {
dryRun = true;
updateBirthdays();
dryRun = false;
}
/**
* Displays what would be deleted in the Execution Log without editing the calendar
* (useful for testing)
*/
function dryRunDelete() {
dryRun = true;
deleteEvents();
dryRun = false;
}
/**
* Updates birthdays and special events from Google Contacts to your calendar
*/
function updateBirthdays() {
if (dryRun) {
Logger.log("DRY RUN MODE: No events will be created, only logged");
}
GCalTools.createSpecialEventsForAllContacts(useOriginalBirthdayCalendar ? "primary" : calendarId);
}
/**
* Deletes birthday and special events from the calendar based on your configuration
*/
function deleteEvents() {
const calendarToDeleteId = useOriginalBirthdayCalendar ? 'primary' : calendarId;
let patterns = [];
if (useOriginalBirthdayCalendar) {
// For primary calendar, use the standard pattern that matches the birthday flag
patterns.push(deleteSearchPattern || "Birthday");
} else {
// For secondary calendars, we'll gather labels from contacts in the utility function
if (deleteSearchPattern) {
patterns.push(deleteSearchPattern);
} else {
patterns.push("Birthday"); // Always include Birthday
}
}
if (dryRun) {
Logger.log("DRY RUN MODE: No events will be deleted, only logged");
}
return GCalTools.deleteEvents(calendarToDeleteId, patterns, deleteOnlyFutureEvents, dryRun);
}