Skip to content

Instantly share code, notes, and snippets.

@bene-we
Last active May 3, 2024 13:23
Show Gist options
  • Save bene-we/e0a306ad6788fec5dbe45cde2de2f140 to your computer and use it in GitHub Desktop.
Save bene-we/e0a306ad6788fec5dbe45cde2de2f140 to your computer and use it in GitHub Desktop.
Calculate the age of a person and write it to the event's description in your birthday calendar using Google Apps Script

Calculate the age of a person and write it to the event's description in your birthday calendar using Google Apps Script

This script uses Google Apps Script to access one's Google Calendar and calculate the age on a person's birthday. In the best case you have a custom calendar where your birthdays are stored at. If not, uncomment line 20 and use the filter function at the bottom.

Steps to setup everything

  1. Head to https://script.google.com/home/my and create a new project. Rename the existing file Code.gs to your liking and paste the code from calculateAge.gs.
  2. Paste your calendar ID in line 9 (you can find it in the Google Calendar Settings)
  3. Make sure to add the birth year of a person to the location field (or customize the script)
  4. Customize the message in line 36
  5. Hit run and see the messages appear in your calendar events for the current year!
  6. Create a trigger to run this every year to calculate the correct age

const TAG_NAME = 'calculatedAge';
const FORCE_OVERWRITE = false;
/*
* Calculate the age of the birthday people in each year and write it in the description
*/
function calculateAge() {
// Get Calendar 'Birthdays'
var birthdayCal = CalendarApp.getCalendarById("calendarIdGoesHere");
// Select date range of current year
var currentYear = (new Date()).getFullYear();
var start = new Date(currentYear + '-01-01');
var 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);
var calculatedAge;
for (var i = 0; i < birthdays.length; i++) {
e = birthdays[i];
// Year of birth is stored in the Location field of the event
if (e.getLocation() !== "") {
// Calculate the age if it has not been done before OR FORCE_OVERWRITE is true
if ((e.getDescription() === "" && isNaN(parseInt(e.getTag(TAG_NAME)))) || FORCE_OVERWRITE) {
calculatedAge = Math.round(currentYear - parseInt(e.getLocation()));
// Customize the description here
e.setDescription(e.getTitle() + ' wird heute ' + calculatedAge + ' Jahre!');
// Save calculated age in tag
e.setTag(TAG_NAME, calculatedAge);
// Logger.log(e.getTitle(), ' | ', e.getTag(TAG_NAME), ' | ', e.getDescription());
}
}
}
}
/*
* 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();
}
@dribnus
Copy link

dribnus commented May 3, 2024

Thank you

@bene-we
Copy link
Author

bene-we commented May 3, 2024

@Beeger just an idea from my side, I've seen that you wrapped the script with myFunction, are you sure that you call that function somewhere? I just tested my version and logs are working correctly, but they only show up if the code actually runs.

Apart from that, I cannot see the full calendar event so I cannot see anything wrong, and second it shouldn't be a big deal but I would try to hide personal details like the calendarId when posting on the internet.

@bene-we
Copy link
Author

bene-we commented May 3, 2024

Thank you

No worries! Let me know if you get it to work! Apart from that, nothing dumb about not being experienced in a topic yet, keep it up 🥳

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment