Skip to content

Instantly share code, notes, and snippets.

@bene-we
Last active June 13, 2025 15:15

Revisions

  1. bene-we revised this gist Jul 26, 2024. 1 changed file with 66 additions and 37 deletions.
    103 changes: 66 additions & 37 deletions calculateAge.gs
    Original file line number Diff line number Diff line change
    @@ -1,48 +1,77 @@
    const CALENDAR_ID = "calendarIdGoesHere@group.calendar.google.com";
    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 the birthday people in each year and write it in the description
    * Calculate the age of birthday people in each year and add a short note 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());

    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)}`)
    }
    }
    }
    }
    }
    }
    }
    }

    /*
  2. bene-we revised this gist Jun 9, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,7 @@ This script uses Google Apps Script to access one's Google Calendar and calculat
    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 the see the messages appear in your calendar events for the current year!
    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

    <hr>
  3. bene-we revised this gist Jun 6, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -13,4 +13,4 @@ This script uses Google Apps Script to access one's Google Calendar and calculat

    <hr>

    <kbd><img src="https://imgur.com/L9Dqq6q"></kbd>
    <kbd><img src="https://i.imgur.com/L9Dqq6q.png"></kbd>
  4. bene-we revised this gist Jun 6, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -13,4 +13,4 @@ This script uses Google Apps Script to access one's Google Calendar and calculat

    <hr>

    <kbd><img src="https://i.imgur.com/7sQko0c.png"></kbd>
    <kbd><img src="https://imgur.com/L9Dqq6q"></kbd>
  5. bene-we revised this gist Jun 6, 2020. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -11,4 +11,6 @@ This script uses Google Apps Script to access one's Google Calendar and calculat
    5. Hit run and the 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

    <img src="https://i.imgur.com/7sQko0c.png">
    <hr>

    <kbd><img src="https://i.imgur.com/7sQko0c.png"></kbd>
  6. bene-we revised this gist Jun 6, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -11,4 +11,4 @@ This script uses Google Apps Script to access one's Google Calendar and calculat
    5. Hit run and the 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

    [Result](https://i.imgur.com/7sQko0c.png)
    <img src="https://i.imgur.com/7sQko0c.png">
  7. bene-we revised this gist Jun 6, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -11,4 +11,4 @@ This script uses Google Apps Script to access one's Google Calendar and calculat
    5. Hit run and the 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

    <img src="https://imgur.com/7sQko0c" alt="result">
    [Result](https://i.imgur.com/7sQko0c.png)
  8. bene-we revised this gist Jun 6, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -11,4 +11,4 @@ This script uses Google Apps Script to access one's Google Calendar and calculat
    5. Hit run and the 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

    <img src="https://ibb.co/26KfcqY" alt="result">
    <img src="https://imgur.com/7sQko0c" alt="result">
  9. bene-we revised this gist Jun 6, 2020. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -9,4 +9,6 @@ This script uses Google Apps Script to access one's Google Calendar and calculat
    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 the 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
    6. Create a trigger to run this every year to calculate the correct age

    <img src="https://ibb.co/26KfcqY" alt="result">
  10. bene-we created this gist Jun 6, 2020.
    12 changes: 12 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    # 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 the 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
    55 changes: 55 additions & 0 deletions calculateAge.gs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,55 @@
    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();
    }