Skip to content

Instantly share code, notes, and snippets.

@catdance124
Last active September 12, 2021 09:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save catdance124/7c8e75470d35c05afce43424af63d947 to your computer and use it in GitHub Desktop.
Save catdance124/7c8e75470d35c05afce43424af63d947 to your computer and use it in GitHub Desktop.
[GAS] 楽天銀行利用メールから履歴を抽出 -> スプレッドシートに記録
var BUDGET = PropertiesService.getScriptProperties().getProperty("BUDGET");
var DEFAULT_PAYDAY = PropertiesService.getScriptProperties().getProperty("DEFAULT_PAYDAY");
var LINE_NOTIFY = false;
function set_sheet(name){
// ref: https://qiita.com/crawd4274/items/13120429cb3328e8ace2
//同じ名前のシートがなければ作成
var sheet = SpreadsheetApp.getActive().getSheetByName(name)
if(sheet)
return sheet
sheet=SpreadsheetApp.getActiveSpreadsheet().insertSheet();
sheet.setName(name);
sheet.getRange("A1:D1").setValues([["推定残額:", "=D1-SUM(B2:B1000)", "予算:", BUDGET]]);
return sheet;
}
function isHoliday_(date) {
// ref: https://moripro.net/gas-check-holiday/
// ①土日の判定
const day = date.getDay(); //曜日取得
if (day === 0 || day === 6) return true;
// ②祝日の判定
const id = 'ja.japanese#holiday@group.v.calendar.google.com'
const cal = CalendarApp.getCalendarById(id);
const events = cal.getEventsForDay(date);
//なんらかのイベントがある=祝日
if (events.length) return true;
}
function calc_payday(date) {
if (isHoliday_(date)){
date.setDate(date.getDate() -1);
return calc_payday(date);
} else {
return date;
}
}
function main() {
// 未処理のスレッドを取得
var threads = GmailApp.search('subject:(デビットカードご利用通知メール OR ATM出金 OR コンビニ支払サービス) -label:starred');
threads.forEach(function(thread) {
// スレッド内メール一覧を取得
var messages = thread.getMessages();
messages.forEach(function(message) {
if (!message.isStarred()){
// タイトル/本文取得
var subject = message.getSubject();
var plainBody = message.getPlainBody();
// メール受信日時を取得
var message_date = message.getDate();
// 翌月扱いとするか否かを設定
var surrogate_date = new Date(message_date);
var default_payday = new Date(message_date);
default_payday.setDate(DEFAULT_PAYDAY);
var payday = calc_payday(default_payday);
// 給料日を過ぎていれば翌月扱いとする
if (payday <= message_date){
surrogate_date.setMonth(surrogate_date.getMonth()+1);
}
// Debit or else
var is_debit = subject.match(/デビットカード/);
var amount = 0;
if (is_debit) {
amount = plainBody.match(/口座引落分:(.*)円/)[1];
} else {
amount = "*"+subject;
}
// 年月ごとのシートに書き込む
var yyyy_MM = Utilities.formatDate(surrogate_date,"JST", "yyyy/MM");
var sheet = set_sheet(yyyy_MM);
var lastRow = sheet.getLastRow() + 1;
sheet.getRange(lastRow, 1).setValue(message_date);
sheet.getRange(lastRow, 2).setValue(amount);
// LINE 通知用
if (LINE_NOTIFY){
var line_message = Utilities.formatDate(message_date,"JST", "yyyy/MM/dd") + "\n" + amount;
if (isNaN(amount)){
line_message += "\nデビットカード利用以外でした.手動で更新してください";
} else {
line_message += "\n推定残額: " + sheet.getRange("B1").getValue() + "円";
}
postMessage(line_message);
}
// 日付で降順ソート
var value_range = sheet.getRange("A2:B"+String(lastRow+1));
value_range.sort({column: 1, ascending: false});
message.star();
}
});
});
}
function setVal(){
PropertiesService.getScriptProperties().setProperty("DEFAULT_PAYDAY", /*SET VALUE*/);
PropertiesService.getScriptProperties().setProperty("BUDGET", /*SET VALUE*/);
Logger.log(PropertiesService.getScriptProperties().getProperty("DEFAULT_PAYDAY"));
Logger.log(PropertiesService.getScriptProperties().getProperty("BUDGET"));
}
var LINE_ACCESS_TOKEN = PropertiesService.getScriptProperties().getProperty('LINE_ACCESS_TOKEN');
var LINE_GROUP_ID = PropertiesService.getScriptProperties().getProperty('LINE_GROUP_ID');
function postMessage(message){
var options = {
'method' : 'post',
'headers': {
"Content-Type": "application/json",
'Authorization': 'Bearer ' + LINE_ACCESS_TOKEN
},
'payload' : JSON.stringify({
"to": LINE_GROUP_ID,
"messages": [{
"type": "text",
"text": message,
}],
})
};
var response = UrlFetchApp.fetch('https://api.line.me/v2/bot/message/push', options);
}
function setVal_LINE(){
PropertiesService.getScriptProperties().setProperty("LINE_ACCESS_TOKEN", /*SET VALUE*/);
PropertiesService.getScriptProperties().setProperty("LINE_GROUP_ID", /*SET VALUE*/);
Logger.log(PropertiesService.getScriptProperties().getProperty("LINE_ACCESS_TOKEN"));
Logger.log(PropertiesService.getScriptProperties().getProperty("LINE_GROUP_ID"));
}
@catdance124
Copy link
Author

動作例
image

  • ATM出金/公共料金のコンビニ支払いはメールから金額が読めないので手動でやる必要があるのが難点
    • 頻度としては月2,3回なので妥協
  • 通知メールをLINEに飛ばしたい & LINEから↑を記入したい

@catdance124
Copy link
Author

LINE通知機能を追加 Revision2

LINE通知機能なしのシンプルバージョン Revision1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment