Skip to content

Instantly share code, notes, and snippets.

@daikiojm
Last active September 30, 2021 08:48
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 daikiojm/a144ae76e68afbdba2decfc65aa707d5 to your computer and use it in GitHub Desktop.
Save daikiojm/a144ae76e68afbdba2decfc65aa707d5 to your computer and use it in GitHub Desktop.
calendar aggregate with google app script
const calenderId = 'xxxxxxxxxxx@group.calendar.google.com';
function aggregateCalender() {
const calender = CalendarApp.getCalendarById(calenderId);
Logger.log(`対象カレンダー: ${calender.getName()}`);
const startTime = new Date('2021/6/1 00:00:00');
const endTime = new Date('2021/6/30 00:00:00');
const events = calender.getEvents(startTime, endTime);
Logger.log(`イベント件数: ${events.length}`);
if (events.length < 1) {
Logger.log('イベントがありません。スクリプトを停止しました');
}
const aggregated = events
.map((e) => calculateActivityTime(e))
.reduce((prev, curr) => prev + curr, 0);
Logger.log(`合計時間(h): ${aggregated}`);
}
function calculateActivityTime(event: GoogleAppsScript.Calendar.CalendarEvent) {
const end = event.getEndTime();
const start = event.getStartTime();
// @ts-ignore
return (end - start) / 1000 / 60 / 60
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment