Skip to content

Instantly share code, notes, and snippets.

@jhonnymichel
Last active July 7, 2023 06:28
Show Gist options
  • Save jhonnymichel/38da5a21b6f54c7d8e3fe6dacee44268 to your computer and use it in GitHub Desktop.
Save jhonnymichel/38da5a21b6f54c7d8e3fe6dacee44268 to your computer and use it in GitHub Desktop.
CIBC to Lunch Money CSV
// get the table
let table = document.querySelector('table.ember-view');
// get rows
let rows = Array.from(table.querySelectorAll('tbody tr.transaction-row')).map(
(tr) => {
let date = tr.querySelector('.transactionDate span').innerText.trim();
let description = tr
.querySelector('span.transactionDescription')
.innerText.trim();
let amount = tr.querySelector('.amount span').innerText.trim();
// convert date to YYYY-MM-DD
let dateObj = new Date(date);
let formattedDate = new Intl.DateTimeFormat('en-CA').format(dateObj); // 'en-CA' gives the date format in 'YYYY-MM-DD'
// convert amount to number
let formattedAmount = 0;
if (amount.indexOf('−$') != -1) {
// debit (negative) transaction
formattedAmount = parseFloat(amount.replace('−$', '').replace(',', '')) * -1;
} else {
// credit (positive) transaction
formattedAmount = parseFloat(amount.replace('$', '').replace(',', ''));
}
return [formattedDate, `"${description}"`, formattedAmount];
}
);
// format to CSV
let csvContent = '';
// add headers
csvContent += 'date,payee,amount\n';
// add rows
rows.forEach((row) => {
csvContent += row.join(',') + '\n';
});
// output CSV content
console.log(csvContent);
// Create a blob with the CSV content
let csvData = new Blob([csvContent], { type: 'text/csv;charset=utf-8;' });
// Create a download link
let link = document.createElement('a');
link.setAttribute('href', URL.createObjectURL(csvData));
link.setAttribute('download', 'creditcard.csv');
link.style.display = 'none';
// Append the link to the document body
document.body.appendChild(link);
// Trigger the download
link.click();
// Clean up the link element
document.body.removeChild(link);
// Extract data from table
let rows = Array.from(document.querySelectorAll('.transaction-row'));
let csvContent = 'date,payee,debit,credit\n'; // Start CSV string and include headers
rows.forEach((row) => {
let dateCell = row.querySelector('.date');
let transactionCell = row.querySelector('.transactionDescription');
let debitCell = row.querySelector('.debit span');
let creditCell = row.querySelector('.credit span:not(.hidden-text)');
let date = dateCell
? new Date(dateCell.textContent)
.toISOString()
.split('T')[0]
.replace(/-/g, '/')
: '';
let transaction = transactionCell ? transactionCell.innerText.trim() : '';
let debit =
debitCell && debitCell.textContent !== 'Not applicable'
? debitCell.textContent.trim().replace(/[$,]/g, '')
: '';
let credit =
creditCell && creditCell.textContent !== 'Not applicable'
? creditCell.textContent.trim().replace(/[$,]/g, '')
: '';
csvContent += `${date},${transaction},${debit},${credit}\n`; // Append new row data
});
// Create a download link and click it
let blob = new Blob([csvContent], { type: 'text/csv;charset=utf-8;' });
let link = document.createElement('a');
let url = URL.createObjectURL(blob);
link.setAttribute('href', url);
link.setAttribute('download', 'debit.csv');
link.style.visibility = 'hidden';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment