Skip to content

Instantly share code, notes, and snippets.

@bene-we
Last active March 26, 2025 16:59
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 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();
}
@trivers27
Copy link

Another error I am running into is

"Exceeded maximum execution time"

@dribnus
Copy link

dribnus commented Oct 28, 2024

I've been trying to play around with this script and now have all kinds of confusion. What I'm trying to accomplish may be a blend of the different scripts that are already posted. I love the option to choose how many years to forward on the calculation, but also really want the age to show up in the title instead of description. I can't seem to get both of those to happen at the same time.

@bene-we
Copy link
Author

bene-we commented Mar 26, 2025

@dribnus did you get it to work? Adding it to the title instead of the description should be as easy as

const previousTitle = e.getTitle()
e.setTitle(previousTitle + ' | Turns ' + calculatedAge + ' today!')

Didn't test it so, don't want to mess with my current script.

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