Skip to content

Instantly share code, notes, and snippets.

@azrsh
Last active February 18, 2024 16:35
Show Gist options
  • Save azrsh/5512f7602174749ceda4f447b1e5adff to your computer and use it in GitHub Desktop.
Save azrsh/5512f7602174749ceda4f447b1e5adff to your computer and use it in GitHub Desktop.
カーシェアの予約時間帯をカレンダーに入れる
const CALENDARIZED_LABEL = "carshare-calendarized";
function isCalendarizable(m) {
return [
"新規予約受付のご案内",
"予約延長受付のご案内",
"予約変更受付のご案内",
].includes(m.getSubject());
}
function extractTime(lines, permlink) {
const keywordIndex = lines.indexOf("■予約時間");
if (keywordIndex === -1) {
throw new Error(`malformed mail: ${permlink}`);
}
const start = Utilities.parseDate(
lines[keywordIndex + 1],
"JST",
"yyyy/MM/dd HH:mm~"
);
const end = Utilities.parseDate(
lines[keywordIndex + 2],
"JST",
"yyyy/MM/dd HH:mm"
);
return { start, end };
}
function extractId(lines, permlink) {
const keywordIndex = lines.indexOf("■予約番号");
if (keywordIndex === -1) {
throw new Error(`malformed mail: ${permlink}`);
}
return lines[keywordIndex + 1];
}
function main() {
const threads = GmailApp.search(
`from:upr.cssystem@upr-net.co.jp -label:${CALENDARIZED_LABEL}`
);
for (const t of threads) {
for (const m of t.getMessages()) {
if (isCalendarizable(m)) {
const lines = m.getPlainBody().split(/\r?\n/);
const { start, end } = extractTime(lines, t.getPermalink());
const id = extractId(lines);
const userProperties = PropertiesService.getUserProperties();
const eventId = userProperties.getProperty(id);
const calendar = CalendarApp.getCalendarById(
"calendar-id@group.calendar.google.com"
);
if (eventId == null) {
const event = calendar.createEvent("カーシェア予約時間", start, end);
event.setDescription(`予約番号: ${id}\n[remind end time] 15min`);
userProperties.setProperty(id, event.getId());
} else {
const event = calendar.getEventById(eventId);
event.setTime(start, end);
}
} else {
console.log(`ignore message: ${t.getPermalink()}`);
}
}
t.addLabel(GmailApp.getUserLabelByName(CALENDARIZED_LABEL));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment