0
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 1 year has passed since last update.

[GAS] Gmailで受信した楽天ペイの利用明細をGoogleスプレッドシートに転記

Posted at

対象

  • 楽天ペイの利用明細をGmailで受け取っている方
  • 本スクリプトを理解して自分用にカスタマイズ可能な方

利用方法

Googleドライブ -> 新規 -> Googleスプレッドシート -> 拡張機能 -> App Script
myFunctionを差し替えて[実行]

スクリプト

function myFunction() {
  
  const setting = {
    query:"from:楽天ペイ after:2024/1/1 before:2025/1/1 ご利用明細",
    max:10,
    keywords:["ご利用日時","伝票番号","ご利用店舗","電話番号","決済総額","獲得予定ポイント","お取引内容","お支払方法"],
  }
 
  const threads = GmailApp.search(setting.query, 0, setting.max);
  const messagesForThreads = GmailApp.getMessagesForThreads(threads);
 
  const rows = [setting.keywords];
  for(const messages of messagesForThreads){
    for(const message of messages){
      const plainBody = message.getPlainBody();
      const columns = [];
      for(const keyword of setting.keywords) {
        const matches = plainBody.match(keyword + ".*");
        let cellText = "---";
        if (matches) {
          cellText = matches[0].replace(keyword, "").trim();
        }
        columns.push(cellText);
      }
      rows.push(columns);
    }
  }
 
  const sheet = SpreadsheetApp.getActiveSpreadsheet().insertSheet();
  sheet.getRange(1, 1, rows.length, rows[0].length).setValues(rows);
}

注意事項

  • 検索条件は2024年分としています。setting.queryを調整して下さい
  • 1日の検索数に制限があるため上限を10スレッドとしています。試して問題が無ければsetting.maxを調整して下さい
  • 特殊なケースは一切考慮していません。自己責任でご利用、改変お願いします

実行結果

一部マスクしています
image.png

0
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
0
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?