Skip to content

Instantly share code, notes, and snippets.

@dora1998
Last active December 7, 2019 19:47
Show Gist options
  • Save dora1998/5a626f20797bd305fd93c76faab9da1b to your computer and use it in GitHub Desktop.
Save dora1998/5a626f20797bd305fd93c76faab9da1b to your computer and use it in GitHub Desktop.
Add events from emails sent from Smart-EX
function checkSmartExEmail() {
var searchQuery = "from:([email protected]) subject:(新幹線予約)";
var checkSpanMinute= 5; // 起動時間間隔
const regexpObj = /[\r\n]+.+[\r\n]+ ([0-9]+)([0-9]+)[\r\n]+(.+)\(([0-9]+:[0-9]+)\)(.+)(.+)\(([0-9]+:[0-9]+)\)[\s\S]+/m
var dt = new Date();
dt.setMinutes(dt.getMinutes() - checkSpanMinute);
var threads = GmailApp.search(searchQuery, 0, 5);
var msgs = GmailApp.getMessagesForThreads(threads);
for(var i = 0; i < threads.length; i++) {
var lastMsgDt = threads[i].getLastMessageDate();
if(lastMsgDt.getTime() < dt.getTime()) {
break;
}
for(var j = 0; j < msgs[i].length; j++) {
var msgBody = msgs[i][j].getPlainBody();
var matches = msgBody.match(regexpObj)
Logger.log(matches)
if (!matches) continue;
var reservationDate = dt.getFullYear() + '/' + matches[1] + '/' + matches[2]
var event = CalendarApp.getDefaultCalendar().createEvent(matches[5] + ' ' + matches[3] + '→' + matches[6],
new Date(reservationDate + ' ' + matches[4]),
new Date(reservationDate + ' ' + matches[7]),
{description: matches[0].replace('(ご案内)', '')});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment