|
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(); |
|
} |
@dribnus did you get it to work? Adding it to the title instead of the description should be as easy as
Didn't test it so, don't want to mess with my current script.