LoginSignup
3
1

More than 3 years have passed since last update.

Gmailの本文から一部を抽出してスプレッドシートに書き出す

Posted at

はじめに

とある事情でGmailで受信しているメール本文から
一部を抽出してスプレッドシートに書き出したいということがあり
GoogleAppsScriptで対応した際の備忘録です。

環境

Windows 10

手順

  • スプレッドシート開く
  • ツール>スクリプトエディタをクリック
  • 以下スクリプトを記述

function myFunction(){
  var mailQuery = 'from:(xxx@oooo.jp) 【hogehoge】';
  var threads = GmailApp.search(mailQuery);
  var messages = GmailApp.getMessagesForThreads(threads);
  var sheet = SpreadsheetApp.getActiveSheet();
  for(var i=0; i<messages.length; i++){
    var plainBody = messages[i][0].getPlainBody();
    sheet.appendRow([plainBody.match(/日付.*/)[0].replace('日付', ''), plainBody.match(/ID.*? /)[0].replace('ID', ''),])
  }
}

補足ポイント

  • mailQueryで対象のメール条件を定義
  • 本文内にある日付、IDを正規表現を使って抽出しています

参考リンク

https://liginc.co.jp/418649
https://blog.8basetech.com/google-apps-script/gmail_extraction/

さいごに

今回受信した対象メールが結構多く、時間に余裕もなかったこともあって
GoogleAppsScriptを使って抽出することにしました。
参考リンクの記事のおかげでGoogleAppsScriptを初めて使ったのですが、
小一時間で目的を果たすことができました。

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