Last active
June 9, 2016 22:12
-
-
Save jshbrntt/99f71474832e088607bc to your computer and use it in GitHub Desktop.
HSBC Personal Statement to CSV
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var statementDate = new Date(Date.parse(document.getElementsByClassName('hsbcTextRight')[0].textContent)); | |
var rows = | |
document | |
.getElementsByTagName('table')[1] // Second table | |
.getElementsByTagName('tbody')[0] // Table body | |
.getElementsByTagName('tr'); // Table body rows | |
// Convert HTMLCollection to Array. | |
rows = [].slice.call(rows); | |
// Trim top and bottom rows. | |
rows.shift(); | |
rows.pop(); | |
// Group by date. | |
var transactions = {}; | |
rows.map(function(row) { | |
var cells = row.children; | |
var date = new Date(new Date(Date.parse(cells[0].textContent)).setYear(statementDate.getFullYear())); | |
// Handle date crossover from one year to the next. | |
if (date > statementDate) { | |
date.setYear(date.getFullYear() - 1); | |
} | |
if (!transactions.hasOwnProperty(date.valueOf())) { | |
transactions[date.valueOf()] = []; | |
} | |
var transaction = { | |
description: cells[2].textContent.trim(), | |
amount: (parseFloat(cells[4].textContent) || 0) - (parseFloat(cells[3].textContent) || 0) | |
}; | |
transactions[date.valueOf()].push(transaction); | |
}); | |
// Get start and end date of the statement to iterate through data. | |
var dates = Object.keys(transactions); | |
var from = new Date(parseInt(dates[0])); | |
var to = new Date(statementDate); | |
// Handle date crossover from one year to the next. | |
if (from > to) { | |
from.setYear(from.getFullYear() - 1); | |
} | |
// Exporting CSV | |
var csv = ''; | |
for (var d = from; d <= to; d.setDate(d.getDate() + 1)) { | |
// If there are transactions from that date. | |
if (transactions.hasOwnProperty(d.valueOf())) { | |
var transactionsOnDate = transactions[d.valueOf()]; | |
var descriptions = transactionsOnDate.map(function(transaction) { | |
return transaction.description; | |
}); | |
var amounts = transactionsOnDate.map(function(transaction) { | |
return transaction.amount; | |
}); | |
csv += (descriptions.join(',')+'\n'); | |
csv += (amounts.join(',')+'\n'); | |
} | |
// ...else blanks space for blank rows. | |
else { | |
csv += '\n\n'; | |
} | |
} | |
console.log(csv); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var statementDate = new Date(Date.parse($('.extTableColumn2:eq(2)').text())); | |
var rawTransactions = $('table:eq(1) tbody tr').toArray(); | |
console.debug('statementDate', statementDate); | |
console.debug('rawTransactions', rawTransactions); | |
var parsedTransactions = $.map(rawTransactions, function (rawTransaction) { | |
var cells = $(rawTransaction).children(); | |
return { | |
date: new Date(Date.parse($(cells[0]).text())).setYear(statementDate.getFullYear()), | |
desc: $(cells[2]).text().trim(), | |
amount: (parseFloat($(cells[4]).text()) || 0)-(parseFloat($(cells[3]).text()) || 0) | |
}; | |
}); | |
console.debug('parsedTransactions ', parsedTransactions ); | |
var from = new Date(parsedTransactions[0].date); | |
var to = new Date(parsedTransactions[parsedTransactions.length - 1].date); | |
console.debug('from ', from); | |
console.debug('to', to); | |
function getTransactionsDated(date) { | |
var transactions = [[],[]]; | |
for (var i = 0; i < parsedTransactions.length; i++) { | |
var transactionDate = new Date(parsedTransactions[i].date); | |
if (transactionDate.getTime() === date.getTime()) { | |
transactions[0].push(parsedTransactions[i].desc.replace(/\r?\n|\r/g, '').replace(/\s+/g, " ")); | |
transactions[1].push(parsedTransactions[i].amount); | |
} | |
else if (transactionDate < d) { | |
return transactions; | |
} | |
} | |
return transactions; | |
} | |
var csv = ''; | |
for (var d = from; d >= to; d.setDate(d.getDate() - 1)) { | |
console.debug('d', d); | |
var transactions = getTransactionsDated(d); | |
var labels = transactions[0].join(','); | |
var amounts = transactions[1].join(','); | |
csv += (labels+'\n'); | |
csv += (amounts+'\n'); | |
} | |
console.log(csv); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment