17
17

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 5 years have passed since last update.

Amazonの注文確認のgmailから、googleカレンダーにその受け取り時刻の予定を自動で入れる

Posted at

http://gawawa124.hatenablog.com/entry/2017/06/15/155242 と同様の内容です。

Google Apps Scriptデビューしました。
未読のAmazonからの注文確認メールを2時間おきに確認して、Googleカレンダーにその受け取り時刻の予定を自動で入れています。
2時間おきというのは任意で変更できて、Google Apps Scriptエディタの時計のアイコンからトリガーを設定させればできます。
便利。

コメントは意図や出力例、つまずいたところを書きました。
よろしくお願いいたします。

function createAmazonEvent() {
  var criteria = "is:unread from:(Amazon.co.jp) ご注文の確認";
  var dateExp = /\d{2}\/\d{2}\s\d{2}:\d{2}/g; // ex. 06/15 16:00
  var dateExpWithSubString = /(\d{2})\/(\d{2})\s(\d{2}):(\d{2})/; // ()をつけることで部分文字列として返却される
  
  GmailApp.search(criteria).forEach(function(thread) {
    var messages = thread.getMessages();
    messages.forEach(function(message) {
      var body = message.getPlainBody(); //getBodyだとHTMLメールのため
      
      if (!(body.match(/お届け予定/))) {
        // Kindleの注文を除くため
        return;
      }
      
      var year = message.getDate().getFullYear();
      var myArray = body.match(dateExp); // ex. [06/15 16:00, 06/15 18:00]
      var [sMatched, sMonth, sDay, sHour, sMin] = myArray[0].match(dateExpWithSubString);
      var [eMatched, eMonth, eDay, eHour, eMin] = myArray[1].match(dateExpWithSubString);
      var startDate = new Date(year, sMonth - 1, sDay, sHour, sMin);
      var endDate = new Date(year, eMonth - 1, eDay, eHour, eMin);
      CalendarApp.getDefaultCalendar().createEvent("Amazon荷物", startDate, endDate);
      message.markRead();
    });
  });
}

参考記事:
http://blog.shotarok.com/post/2016-12-09-auto_creation_of_eikaiwa_events
https://developer.mozilla.org/ja/docs/Web/JavaScript/Guide/Regular_Expressions
https://developers.google.com/apps-script/reference/calendar

17
17
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
17
17

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?