Last active
December 7, 2019 19:47
-
-
Save dora1998/5a626f20797bd305fd93c76faab9da1b to your computer and use it in GitHub Desktop.
Add events from emails sent from Smart-EX
This file contains hidden or 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 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