-
-
Save jjj120/1074d737478984d8d31fa1abacdab8c1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const TAG_NAME = 'calculatedAge'; | |
const TITLE_APPENDIX = "Geburtstag" | |
const CALENDAR_ID = "<your-calendar-id>" | |
const FORCE_OVERWRITE = true; | |
function calcAge5() { | |
calculateAgeForYears(5) | |
} | |
function calculateAgeForYears(years) { | |
for (var years_offset = 0; years_offset <= years; years_offset++) { | |
calculateAge(years_offset); | |
} | |
} | |
/* | |
* Calculate the age of the birthday people in each year and write it in the description | |
*/ | |
function calculateAge(years_offset) { | |
// Get Calendar 'Birthdays' | |
var birthdayCal = CalendarApp.getCalendarById(CALENDAR_ID); | |
// Select date range of current year | |
var currentYear = (new Date()).getFullYear() + years_offset; | |
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; | |
var old_title; | |
var new_title; | |
var name; | |
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())); | |
old_title = e.getTitle() | |
//name = old_title.substr(0, old_title.lastIndexOf(" ")); | |
name = getNameFromString(old_title) | |
new_title = insertAgeIntoString(old_title, calculatedAge); | |
// Customize the description here | |
e.setDescription(name + ' wird heute ' + calculatedAge + ' Jahre alt!'); | |
e.setTitle(new_title); | |
// Save calculated age in tag | |
e.setTag(TAG_NAME, calculatedAge); | |
Logger.log(name, ' | ', e.getTitle(), ' | ', e.getTag(TAG_NAME), ' | ', e.getDescription()); | |
} | |
} | |
} | |
} | |
/* | |
* Insert the age into the title | |
*/ | |
function insertAgeIntoString(originalString, age) { | |
// Regular expression to find the pattern "(age)" | |
const agePattern = /\(\d+\)/; | |
// Find the position where title appendix starts | |
const position = originalString.indexOf(TITLE_APPENDIX); | |
if (position === -1) { | |
// If the title appendix is not found, return the original string | |
if (agePattern.test(originalString)) { | |
// Replace the existing age with the new age | |
return originalString.replace(agePattern, `(${age})`); | |
} else { | |
return originalString + ` (${age})`; | |
} | |
} | |
// Check if there is already an age in parentheses before title appendix | |
const substringBefore = originalString.slice(0, position).trim(); | |
if (agePattern.test(substringBefore)) { | |
// Replace the existing age with the new age | |
return originalString.replace(agePattern, `(${age})`); | |
} else { | |
// Insert the new age before the title appendix | |
return substringBefore + ` (${age}) ` + originalString.slice(position); | |
} | |
} | |
/* | |
* Get Name from the title string | |
*/ | |
function getNameFromString(originalString) { | |
// Regular expression to find the pattern "(age)" | |
const agePattern = /\(\d+\)/; | |
// Find the position where title appendix starts | |
const position = originalString.indexOf(TITLE_APPENDIX); | |
if (position === -1) { | |
// If the title appendix is not found, return the original string | |
return originalString.replace(agePattern, '').trim(); | |
} | |
// Get the substring before title appendix | |
const substringBeforeGeburtstag = originalString.slice(0, position).trim(); | |
// Remove the age pattern if it exists | |
return substringBeforeGeburtstag.replace(agePattern, '').trim(); | |
} | |
/* | |
* 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(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment