Skip to content

Instantly share code, notes, and snippets.

@azrsh
Last active February 19, 2024 02:01
Show Gist options
  • Save azrsh/f015f3d2da171bdcaa31b3181977d4b8 to your computer and use it in GitHub Desktop.
Save azrsh/f015f3d2da171bdcaa31b3181977d4b8 to your computer and use it in GitHub Desktop.
カレンダーのイベントの終了時刻を通知する。無限に user properties が増えるのでエラーが出たら掃除するといい
function remind() {
const calendars = CalendarApp.getAllCalendars();
for (const calendar of calendars) {
const now = new Date();
for (const event of calendar.getEventsForDay(now)) {
const endTime = event.getEndTime();
const minutesBefore = (minutes) => {
const date = new Date(endTime);
date.setMinutes(endTime.getMinutes() - minutes);
return date;
};
const description = event.getDescription();
const keyword = "[remind end time]";
const keywordIndex = description.indexOf(keyword);
if (keywordIndex === -1) {
continue;
}
const minutes = description
.slice(keywordIndex + keyword.length)
.match(/^ (?<minutes>\d+)min$/).groups.minutes;
const userProperties = PropertiesService.getUserProperties();
if (!userProperties.getProperty(event.getId()) && minutesBefore(minutes) < now && now < endTime) {
MailApp.sendEmail({
to: Session.getActiveUser().getEmail(),
subject: `[終了時刻通知] 「${event.getTitle()}」終了${minutes}分前`,
body: `カレンダー「${calendar.getName()}」のイベント「${event.getTitle()}」の終了${minutes}分前をお知らせします。`,
});
userProperties.setProperty(event.getId(), true);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment