LoginSignup
3
6

More than 5 years have passed since last update.

ネットスーパーの配達予定日時をカレンダーに自動登録する

Last updated at Posted at 2018-05-23

はじめに

重いもの(お米やお水など)を買う際はネットスーパーを利用しています。
きちんと受け取るためには、その配達予定時間に外出しないように気をつけなければいけません。

そこで、注文時に送信されるメールを解析して配達予定日時をカレンダーに登録するようなスクリプトを作成しました。

イメージ

処理

GoogleAppsScriptで作成しました。

  • トリガー: 5分ごと
function main() {

  var predicate = function(thread) {
    return thread.getFirstMessageSubject() == "[ご注文確認]SEIYUドットコム";
  }
  var regex = new RegExp(/・お届け日時:([0-9]+\/[0-9]+\/[0-9]+) ([0-9]+)([0-9]+)分~([0-9]+)([0-9]+)分/);

  // カレンダーを取得
  var calendar = CalendarApp.getCalendarById('...@gmail.com');

  // スレッドを取得
  var threads = GmailApp.getInboxThreads().filter(predicate);
  threads.forEach(function(thread) {
    // メッセージを取得
    var messages = thread.getMessages();
    messages.forEach(function(message) {
      // 本文を取得
      var body = message.getPlainBody();

      // お届け日時を取得
      var match = body.match(regex);
      var date = match[1];
      var fromHour = match[2];
      var fromMinute = match[3];
      var toHour = match[4];
      var toMinute = match[5];

      // カレンダーに予定追加
      calendar.createEvent("[SEIYU]ネットスーパー",
                           new Date(date + " " + fromHour + ":" + fromMinute + ":00"),
                           new Date(date + " " + toHour + ":" + toMinute + ":00"));

      // メールを既読にする
      message.markRead();
    });
    // スレッドをアーカイブ
    thread.moveToArchive();
  });
}

動作確認

Googleカレンダーを確認します。

Gmailの受信BOXも確認し、処理されていることを確認しました。

感想

通販サービスはほぼ必ず注文確認メールを送ってくれるので、応用すれば他通販でも出来そうです。

3
6
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
3
6