Skip to content

Instantly share code, notes, and snippets.

@nekonenene
Last active March 3, 2024 06:01
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nekonenene/da815d862b2238e2c30307842d012c37 to your computer and use it in GitHub Desktop.
Save nekonenene/da815d862b2238e2c30307842d012c37 to your computer and use it in GitHub Desktop.
QQEnglish のレッスン予約完了メールを元に、Googleカレンダーのスケジュールを作成
// 1時間おきのトリガーを設定すること
// appsscript.json の timeZone を Asia/Tokyo にしておかないと変な時間にスケジュールが作られてしまうので注意
// QQEnglish のレッスン予約完了メールを元に、Googleカレンダーのスケジュールを作成
function getQQReservedLessonMail() {
const targetHours = 1;
const targetHoursInMilliSeconds = 60 * 60 * 1000 * targetHours;
const subject = "【QQEnglish】レッスン予約完了";
const query = (`newer_than:${targetHours}h subject:"${subject}"`);
const threads = GmailApp.search(query);
const messagesEachThreads = GmailApp.getMessagesForThreads(threads);
messagesEachThreads.forEach(messages => {
messages.forEach(message => {
const currentTime = new Date();
const messageTime = message.getDate();
Logger.log(messageTime);
if (currentTime - messageTime > targetHoursInMilliSeconds) return;
const messageSubject = message.getSubject();
if (!messageSubject.includes(subject)) return;
const messageBody = message.getBody();
const lessonDate = messageBody.match(/\d{4}-\d{2}-\d{2}/)[0];
const lessonTimeRange = messageBody.match(/\d{2}:\d{2}-\d{2}:\d{2}/)[0];
const teacherName = /教師: (.+)/.exec(messageBody)[1];
const splitLessonDate = lessonDate.split('-');
const splitLessonTimeRange = lessonTimeRange.split('-');
createCalendarEvent(
`QQEnglish (${teacherName})`,
"https://www.qqeng.com/q/mypage/",
splitLessonDate[0],
splitLessonDate[1],
splitLessonDate[2],
splitLessonTimeRange[0].split(':')[0],
splitLessonTimeRange[0].split(':')[1],
splitLessonTimeRange[1].split(':')[0],
splitLessonTimeRange[1].split(':')[1],
);
});
});
}
// Googleカレンダーにスケジュール登録 (参考: https://aakira.app/blog/2021/01/gas-gmail-to-calendar )
function createCalendarEvent(title, description, year, month, dayOfMonth, startTimeHour, startTimeMinutes, endTimeHour, endTimeMinutes) {
const calendar = CalendarApp.getDefaultCalendar();
const startTime = new Date(year, month - 1, dayOfMonth, startTimeHour, startTimeMinutes, 0);
const endTime = new Date(year, month - 1, dayOfMonth, endTimeHour, endTimeMinutes, 0);
const option = {
description: description,
}
// 同じ時間に同じタイトルのイベントがあるなら作らない
const events = calendar.getEvents(startTime, endTime);
for (let event of events) {
if (event.getTitle() === title) return;
}
calendar.createEvent(title, startTime, endTime, option);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment