|
//inspired by https://gist.github.com/oshliaer/70e04a67f1f5fd96a708 |
|
|
|
var SHEET_ID = 'YOUR_SHEET_ID'; |
|
var CREDIT_LIMIT = 500000; //YOUR GRAB LIMIT |
|
var WARNING_THRESHOLD = 50000; //Will send warning email if credit balance is less than this threshold |
|
var email = Session.getEffectiveUser().getEmail(); |
|
|
|
// auto detect current period |
|
var curDate = new Date(); |
|
// if date > cutoff date (25) after_date's month is current month, else previous month |
|
if(curDate.getDate() > 25) { |
|
curDate.setMonth(curDate.getMonth()) |
|
} else { |
|
curDate.setMonth(curDate.getMonth() - 1) |
|
} |
|
|
|
// set date as cutoff date |
|
curDate.setDate(25) |
|
|
|
//gmail before and after filter, read: https://support.google.com/mail/answer/7190 |
|
var after_date = Utilities.formatDate(curDate, Session.getScriptTimeZone(), 'yyyy/MM/dd'); |
|
curDate.setMonth(curDate.getMonth() + 1) |
|
var before_date = Utilities.formatDate(curDate, Session.getScriptTimeZone(), 'yyyy/MM/dd'); |
|
|
|
//uncomment these 2 lines for manual date override |
|
//after_date = '2017/12/15'; |
|
//before_date = '2018/01/25'; |
|
|
|
function getEmails(query,sheet){ |
|
var thds = GmailApp.search(query); |
|
for(var i in thds){ |
|
var msgs = thds[i].getMessages(); |
|
for(var j in msgs){ |
|
var data = ''; |
|
//get da plain body |
|
content = msgs[j].getPlainBody(); |
|
|
|
//regex is love. https://xkcd.com/208/ |
|
//lazy matching XD might need improvement |
|
var regExpDateTime = new RegExp("TANGGAL.*\n.*","gi"); |
|
var regExpDateTimeReplace = new RegExp("TANGGAL.*\n|\\+0700","gi"); |
|
var regExpType = new RegExp("Jenis Kendaraan:.*\n.*","gm"); |
|
var regExpTypeReplace = new RegExp("Jenis Kendaraan:.*\n","g"); |
|
var regExpTotalIsZero = new RegExp("TOTAL\n.*RP 0", "gm"); |
|
var regExpIsCorporateBilling = new RegExp("Detail Pembayaran:\n.*Corporate Billing", "gm"); |
|
var regExpRate = new RegExp("Tarif Perjalanan.*", "g"); |
|
var regExpRateReplace = new RegExp("Tarif Perjalanan.*RP", "g"); |
|
var regExpCheckExtra = new RegExp("Biaya Tol & Lainnya", "g"); |
|
var regExpExtraFees = new RegExp("Biaya Tol & Lainnya.*", "g"); |
|
var regExpExtraFeesReplace = new RegExp("Biaya Tol & Lainnya.*RP", "g"); |
|
|
|
var extras = 0; |
|
|
|
//only appendData if total == 0 (total RP 0 means promo code is used) |
|
if(regExpTotalIsZero.exec(content) || regExpIsCorporateBilling.exec(content)){ |
|
data += regExpDateTime.exec(content).toString().replace(regExpDateTimeReplace,''); |
|
data += '|'+regExpType.exec(content).toString().replace(regExpTypeReplace,''); |
|
var rate = parseInt(regExpRate.exec(content).toString().replace(regExpRateReplace,'').trim().replace(",","")); |
|
|
|
if(regExpCheckExtra.exec(content)) { |
|
extras = parseInt(regExpExtraFees.exec(content).toString().replace(regExpExtraFeesReplace,'').trim().replace(",","")); |
|
} |
|
|
|
var sum = (+rate) + (+extras); |
|
data += '|'+ sum; |
|
appendData(data,sheet); |
|
} |
|
} |
|
} |
|
} |
|
|
|
function appendData(data,sheet){ |
|
var token = data.split("|"); |
|
var lastRow = sheet.getLastRow() + 1; |
|
sheet.getRange(lastRow, 1).setNumberFormat("yyyy-mm-dd hh:mm:ss") ; //date formatting |
|
sheet.getRange(lastRow, 1).setValue(token[0]); //datetime |
|
sheet.getRange(lastRow, 2).setValue(token[1]); //type(GrabBike, etc.) |
|
sheet.getRange(lastRow, 3).setValue(token[2]); //fare |
|
} |
|
|
|
function sendWarningMail(sheet){ |
|
MailApp.sendEmail( |
|
email, |
|
"", |
|
"Kudo Grab Promo Usage Warning", |
|
'Dear Myself, \n\nYour credit balance is less than your specified threshold of Rp ' + formatNum(WARNING_THRESHOLD) + |
|
'\nCredit usage: Rp ' + formatNum(sheet.getRange('F5').getValues()) + |
|
'\nCredit balance: Rp ' + formatNum(sheet.getRange('F6').getValues()) + |
|
'\n\nCheck your spreadsheet for more details: https://docs.google.com/spreadsheets/d/'+ SHEET_ID); |
|
|
|
//write sent mail flag in sheet |
|
sheet.getRange(10, 5).setValue("Email sent, period:"); |
|
sheet.getRange(10, 6).setValue(curDate.getMonth()); |
|
} |
|
|
|
function formatNum(s){ |
|
return String(s).replace(/(.)(?=(\d{3})+$)/g,'$1.'); |
|
} |
|
|
|
function run(){ |
|
var ss = SpreadsheetApp.openById(SHEET_ID); |
|
//force set locale to en_US because grab receipt is using (,) as thousand separator |
|
ss.setSpreadsheetLocale('en_US'); |
|
|
|
var sheet = ss.getActiveSheet(); |
|
var sendEmailFlag = false; |
|
var emailFlag = sheet.getRange('E10').getValues(); |
|
var period = sheet.getRange('F10').getValues(); |
|
|
|
//check email sent flag & check period |
|
if(sheet.getRange('E10').getValues() == "" || sheet.getRange('F10').getValues() != curDate.getMonth()){ |
|
sendEmailFlag = true |
|
} |
|
|
|
//clear sheet on every run |
|
sheet.getDataRange().clear(); |
|
|
|
getEmails("Your GRAB E-Receipt after:"+after_date+" before:"+before_date, sheet); |
|
|
|
//preset cells |
|
sheet.getRange(1, 5).setValue("After Date:"); |
|
sheet.getRange(1, 6).setValue(after_date); |
|
sheet.getRange(2, 5).setValue("Before Date:"); |
|
sheet.getRange(2, 6).setValue(before_date); |
|
sheet.getRange(3, 5).setValue("Total Trip(s):"); |
|
sheet.getRange(4, 5).setValue("Credit Limit:"); |
|
sheet.getRange(4, 6).setNumberFormat("#,###").setValue(CREDIT_LIMIT); |
|
sheet.getRange(5, 5).setValue("Credit Usage:"); |
|
sheet.getRange(6, 5).setValue("Credit Balance:"); |
|
sheet.getRange(8, 5).setValue("Last Update:"); |
|
sheet.getRange(8, 6).setValue(new Date().toLocaleString()); |
|
|
|
//formula cells |
|
sheet.getRange(3, 6).setValue("=COUNT(A1:A)"); //count rows |
|
sheet.getRange(5, 6).setValue("=SUM(C1:C)"); //sum fare column |
|
sheet.getRange(6, 6).setValue("=F4-F5"); //credit balance |
|
|
|
//reapply sent mail flag |
|
if(!sendEmailFlag){ |
|
sheet.getRange(10, 5).setValue(emailFlag); |
|
sheet.getRange(10, 6).setValue(period); |
|
} |
|
|
|
if(sheet.getRange('F6').getValues() < WARNING_THRESHOLD && sendEmailFlag){ |
|
sendWarningMail(sheet); |
|
} |
|
} |
09/01/2018
update: