Skip to content

Instantly share code, notes, and snippets.

@cantlin
Created June 17, 2013 16:20
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 cantlin/5798166 to your computer and use it in GitHub Desktop.
Save cantlin/5798166 to your computer and use it in GitHub Desktop.
// Convert an HSBC "previous statement" page into a CSV
var year = 2013,
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
var output = '',
rows = $('table').last().find('tr');
rows.each(function(i, row) {
if(i === 1 || i === rows.length - 1) return;
var cells = $(row).find('td');
var date = $(cells[0]).text().trim(),
desc = $(cells[2]).text().trim(),
debited = $(cells[3]).text().trim()
credited = $(cells[4]).text().trim();
// Reformat date
var day = date.split(' ')[0],
month = months.indexOf(date.split(' ')[1]);
date = (new Date(year, month, day)).toISOString().split('T')[0];
var transaction = '';
if(debited === "") {
transaction = credited;
} else {
transaction = '-' + debited;
}
output += [date, desc, transaction].join(',') + "\n"
});
console.log(output);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment