Skip to content

Instantly share code, notes, and snippets.

@tai-sho
Last active April 15, 2024 04:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tai-sho/cc1500832c9391d892c108da993aeb28 to your computer and use it in GitHub Desktop.
Save tai-sho/cc1500832c9391d892c108da993aeb28 to your computer and use it in GitHub Desktop.
Google Apps Scriptを使用し1ヶ月分のバイオリズムをGoogleカレンダーに登録します。トリガーで毎月月初で登録をする想定で実装。冪等性があり、複数回実行した場合も重複は有りません。 This script calculates biorhythms based on a given birthday and adds them as events to a specified Google Calendar. Each event's title includes the biorhythm type and its value. It is designed to run on the 1st of every month and is idempotent, me…
/**
* This script calculates biorhythms based on a given birthday and adds them as events
* to a specified Google Calendar. Each event's title includes the biorhythm type and its value.
* It is designed to run on the 1st of every month and is idempotent, meaning it can run
* multiple times a month without causing duplicate entries. It deletes any existing biorhythm
* events for the month before adding new ones.
*
* Cycles:
* - Physical: 23 days
* - Emotional: 28 days
* - Intellectual: 33 days
*/
/**
* Main function to run the script.
*/
const main = () => {
const BIRTHDAY = new Date('1990-08-23'); // Set the birthday
const CALENDAR_ID = 'YOUR_CALENDAR_ID@group.calendar.google.com'; // Your calendar ID
const calendar = CalendarApp.getCalendarById(CALENDAR_ID);
const today = new Date();
const startDate = new Date(today.getFullYear(), today.getMonth(), 1);
if (!calendar) {
console.error('Calendar not found');
return;
}
processMonthBiorhythms(startDate, calendar, BIRTHDAY);
console.log('Biorhythm events for the month have been updated in the calendar.');
};
/**
* Calculates the biorhythm index for a given day using the sine function.
*
* @param {number} days - The number of days elapsed since the start date.
* @param {number} cycle - The cycle period of the biorhythm.
* @return {number} The biorhythm index for the day.
*/
const calculateIndex = (days, cycle) => {
return Math.sin(2 * Math.PI * days / cycle);
};
/**
* Calculates the number of days between two dates.
*
* @param {Date} startDate - The start date.
* @param {Date} endDate - The end date.
* @return {number} The number of days between the two dates.
*/
const calculateDaysBetween = (startDate, endDate) => {
const msPerDay = 1000 * 3600 * 24;
return Math.round((endDate - startDate) / msPerDay);
};
/**
* Deletes all biorhythm events for a given month.
*
* @param {GoogleAppsScript.Calendar.Calendar} calendar - The calendar from which to delete events.
* @param {Date} startDate - The start date of the month.
* @param {Date} endDate - The end date of the month.
*/
const deleteExistingEvents = (calendar, startDate, endDate) => {
const events = calendar.getEvents(startDate, endDate);
events.forEach(event => {
if (event.getTitle().startsWith('Biorhythm')) {
event.deleteEvent();
}
});
};
/**
* Adds a biorhythm event to the calendar for a specific date.
*
* @param {GoogleAppsScript.Calendar.Calendar} calendar - The calendar to add events to.
* @param {string} type - The type of biorhythm ('physical', 'emotional', 'intellectual').
* @param {number} index - The biorhythm index for the day.
* @param {Date} date - The date for which the event is being added.
*/
const addBiorhythmEvent = (calendar, type, index, date) => {
const title = `Biorhythm ${type.charAt(0).toUpperCase() + type.slice(1)}: ${index.toFixed(2)}`;
const description = `Daily ${type} state index.`;
calendar.createAllDayEvent(title, date, {description: description});
};
/**
* Processes biorhythm data for a month and adds events to the calendar.
*
* @param {Date} startDate - The start date to process biorhythms from.
* @param {GoogleAppsScript.Calendar.Calendar} calendar - The calendar to add events to.
* @param {Date} birthday - The birthdate used for biorhythm calculations.
*/
const processMonthBiorhythms = (startDate, calendar, birthday) => {
const daysSinceBirth = calculateDaysBetween(birthday, startDate);
const endDate = new Date(startDate.getFullYear(), startDate.getMonth() + 1, 0);
deleteExistingEvents(calendar, startDate, endDate);
const totalDays = calculateDaysBetween(startDate, endDate) + 1;
const cycles = { physical: 23, emotional: 28, intellectual: 33 };
for (let day = 0; day < totalDays; day++) {
const currentDate = new Date(startDate);
currentDate.setDate(startDate.getDate() + day);
Object.keys(cycles).forEach(type => {
const cycle = cycles[type];
const index = calculateIndex(daysSinceBirth + day, cycle);
addBiorhythmEvent(calendar, type, index, currentDate);
});
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment