|
const CALENDAR_ID = "[email protected]"; |
|
const TAG_NAME = 'calculatedAge'; |
|
const FORCE_OVERWRITE = false; |
|
|
|
// Control for how many years in the future the age should be calculated |
|
// Set to `1` to only cover the current year |
|
const CALCULATE_FOR_YEARS = 2; |
|
|
|
// Set to `true` to enable debug logging |
|
const DEBUG = false; |
|
|
|
/* |
|
* Calculate the age of birthday people in each year and add a short note in the description |
|
*/ |
|
function calculateAge() { |
|
// Get Calendar 'Birthdays' |
|
const birthdayCal = CalendarApp.getCalendarById(CALENDAR_ID); |
|
|
|
const yearOfExecution = (new Date()).getFullYear() |
|
|
|
for (var i = 0; i < CALCULATE_FOR_YEARS; i++) { |
|
|
|
// Select date range of the year that is currently processed |
|
const currentYear = yearOfExecution + i; |
|
const start = new Date(currentYear + '-01-01'); |
|
const end = new Date(currentYear + '-12-31'); |
|
|
|
// Fetch events from Birthday Calendar |
|
var birthdays = birthdayCal.getEvents(start, end); |
|
|
|
// Filter Birthdays out of Default Calendar (if no specific Birthday Calendar is present) |
|
// birthdays = birthdays.filter(filterBirthdays); |
|
|
|
if (DEBUG) { |
|
Logger.log(`Scanning ${birthdays.length} birthdays in year ${currentYear} ...`) |
|
} |
|
|
|
for (var j = 0; j < birthdays.length; j++) { |
|
e = birthdays[j]; |
|
|
|
// Year of birth is stored in the Location field of the event |
|
const birthYear = e.getLocation() |
|
const birthYearIsNotEmpty = birthYear !== "" |
|
|
|
if (birthYearIsNotEmpty) { |
|
const descriptionIsEmpty = e.getDescription() === "" |
|
const tagIsEmpty = isNaN(parseInt(e.getTag(TAG_NAME))) |
|
|
|
// Calculate the age if it has not been done before OR `FORCE_OVERWRITE` is true |
|
if ((descriptionIsEmpty && tagIsEmpty) || FORCE_OVERWRITE) { |
|
|
|
const calculatedAge = Math.round(currentYear - birthYear); |
|
|
|
// Customize the description here |
|
const description = e.getTitle() + ' wird heute ' + calculatedAge + ' Jahre!' |
|
e.setDescription(description); |
|
|
|
// Save calculated age in tag |
|
e.setTag(TAG_NAME, calculatedAge); |
|
|
|
if (DEBUG) { |
|
const logEntry = { |
|
name: e.getTitle(), |
|
birthyear: e.getLocation(), |
|
currentYear: Math.round(currentYear), |
|
calculatedAge: Math.round(calculatedAge), |
|
} |
|
|
|
Logger.log(`${description} \n ${JSON.stringify(logEntry, null, 2)}`) |
|
} |
|
} |
|
} |
|
} |
|
} |
|
} |
|
|
|
/* |
|
* Filter the birthdays out of the fetched events |
|
* Add additional filters if you don't have a specific Birthday Calendar |
|
*/ |
|
function filterBirthdays(event) { |
|
// Add filter here (e.g. event.getColor === CalendarApp.Color.YELLOW) |
|
return event.isAllDayEvent(); |
|
} |
First of all, thanks for sharing the code! It has been really helpful!
I never got the reminders to work properly so I did some experiments. I tried to get the all-day default notifications to be assigned to a new calendar event and it does work. The problem is that when executing the script (specifically when setting the description - e.setDescription()) the reminders/notifications are deleted!! I tried checking if each event was in fact an all day event (isAllDayEvent()), then I tried e.removeAllReminders() followed by e.resetRemindersToDefault(). What this did was applying the non all-day default notifications to the event (in spite of being an all day event) and deleting the previous ones set when creating the event.
Later I tested the effects of the scripts when setting different defaults to different times:
I wondered if it was the API that was creating a new event and deleting the previous one whenever the Description of the event is set, meaning that the other event metadata would be copied using its own API(????), meaning that it would abide to the restrictions of the api itself (addPopupReminder() -> must be at least 5 minutes before the event) but in this case the midnight reminder would be deleted!!!
In conclusion, it seems that every all-day default reminder/notification set to the actual day of the event is removed (except for the one at midnight) and only the ones set to prior of the day of the event are kept. I couldn't find anything that could lead to this problem.
Do you think this is a bug? Do you think there's a workaround?
EDIT:


-It seems that running the script is locking the ability to edit notification time! When I try to change the notification parameters of an all day event that repeats yearly it doesn't save, despite giving that feedback. All other parameters (title, description, date and time, visibility, etc) are changeable and the changes saved. Non all day and events that don't repeat, do repeat and all day events that don't repeat are not affected, meaning that we can edit every parameter of the event after running the script.
-Another weird thing: whenever I try to change the notification time of a problematic event (event subject to the script's changes) it does not get saved but changing the events date to any other date, makes the supposed unsaved changes appear!
Example:
1- create an all day yearly event with location 2020, a default all day notification of 00:00, at date 24/03/2023
2- run the script
3- description of the event now has changed to "its rachels 30th birthday" and still has the 00 notification
4- I change notification to 00:50 and save
5- IT DOES NOT GET SAVED appearing as follow:
6- change the event date to 25/03/2023
7- IT APPEARS WITH CORRECT NOTIFICATION TIME!!??? as follow:
8- change it back to day 24/03 -> notification time changes back to 00:00!!!!?
9- change the event to 2 days 24/03 to 25/03 -> notification time stays at 00:00!!!!?