1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

GE〇オンラインレンタルの55円キャンペーンメールを受信したらGoogleカレンダーに登録するGoogle Apps Script

Posted at

#作成動機
なんとなくやってみたかった

#コード

Code.gs
function createEvents(mail) {
  var calendar = CalendarApp.getDefaultCalendar();
  var title = mail['subject'];
  var startTime = new Date(mail['date']);
  var endTime = new Date(mail['date']);
  var location = '';
  
  if(mail['duedate']) {
    endTime = new Date(mail['duedate']);
  }
  
  if(mail['location']) {
    location = mail['location'];
  }
  
  var option = {
    description: mail['body'],
    location: location
  }
  
  console.log("add event detail")
  console.log("title = " + title)
  console.log("start = " + startTime)
  console.log("end = " + endTime)
  console.log("body = " + mail['body'])
  console.log("location = " + location)
  
  //return calendar.createEvent(title, startTime, endTime, option);
  calendar.createEvent(title, startTime, endTime);
}

function extractDueDate(target,pattern) {

  var regexp = new RegExp(pattern,'i')
  var match_result = target.match(regexp);

  if( match_result.length > 0 ) {
    return match_result
  } else {
    return "" 
  }
  
}

function fetchContactMail(filterPattern) {
 var now_time= Math.floor(new Date().getTime() / 1000) ;
 var time_term = now_time - ((60 * get_interval) + 3);
 
 var strTerms = '(is:unread after:'+ time_term + ')';
 
 var myThreads = GmailApp.search(strTerms);
 var myMsgs = GmailApp.getMessagesForThreads(myThreads);
 var valMsgs = [];

 for(var i = 0; i < myMsgs.length;i++){
   
   var contents = myMsgs[i][0].getFrom() + myMsgs[i][0].getSubject() + myMsgs[i][0].getPlainBody()
   
   regexp = new RegExp(filterPattern,'i')
     
   if(! contents.match(regexp) )
   {
     continue
   }
  
   var mailcontents = {
     "date": myMsgs[i][0].getDate()
     , "from" : myMsgs[i].slice(-1)[0].getFrom()
     , "subject" : myMsgs[i].slice(-1)[0].getSubject()
     , "body" : myMsgs[i].slice(-1)[0].getPlainBody()
   }
   valMsgs[i] = mailcontents
 }
 
 return valMsgs;
}

function main() {
 newmail = fetchContactMail("旧作全品55円")
 if(newmail.length > 0){
   for(var i = newmail.length-1; i >= 0; i--){
     //send_line(newmail[i])
     var dueDates = extractDueDate(newmail[i]['body'],"【期間限定】(.*?)年(.*?)月(.*?)日.*?\\)(.*?)まで")
     
     console.log(dueDates[1])
     console.log(dueDates[2])
     console.log(dueDates[3])
     console.log(dueDates[4])
     
     newmail[i]['duedate'] = new Date(dueDates[1] + "/" + dueDates[2] + "/" + dueDates[3] + " " + dueDates[4])
     
     createEvents(newmail[i])
   }
 }
}
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?