Last active
February 19, 2024 02:01
-
-
Save azrsh/f015f3d2da171bdcaa31b3181977d4b8 to your computer and use it in GitHub Desktop.
カレンダーのイベントの終了時刻を通知する。無限に user properties が増えるのでエラーが出たら掃除するといい
This file contains 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
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