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 Mar 26, 2024

I'm not good with understanding scripts in detail. I was able to use the first script with no issues. I really like the ideas and examples that followed, but I couldn't figure out how to add those functions into the original script. Basically I would like to see the "age" data for the next 3 years for the birthdays. Can someone break it down for the stupid one as to how the whole script would look? Thank you.

@Beeger
Copy link

Beeger commented May 2, 2024

I can't get the original script to work, can someone help me find out what's wrong? It also seems like the debugging breakpoints nor logging work so I can't see what's happening on the inside:

image

The log just says it ran successfully but the calendar event didn't change.

function myFunction() {
  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("35fcc071468e5458ae9260955551cdc7f91d06ec3b22c665c71c9e512d50e643@group.calendar.google.com");
  
  // 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();
}
}

@bene-we
Copy link
Author

bene-we commented May 3, 2024

@ruifchaves sorry for getting back to you this late. You really put some effort into this, didn't you! I think I never experienced this issue myself since I've set default reminders on the previous day, and so far I didn't have problems with pop-up reminders. Yet it sounds really buggy what you've experienced, I just don't have an idea what to do about it

@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