This repository was archived by the owner on Feb 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.html
More file actions
135 lines (112 loc) · 5.45 KB
/
index.html
File metadata and controls
135 lines (112 loc) · 5.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Transferwise CSV to FreeAgent CSV</title>
</head>
<body>
<h1>Convert your Transferwise Statements to import into FreeAgent</h1>
<p>FreeAgent doesn't integrate with <a href="https://transferwise.com/u/peterb1144" target="_blank">Transferwise</a>, so
we're reduced to exporting a CSV statement, <a target="_blank"
href="https://support.freeagent.com/hc/en-gb/articles/115001222564-CSV-file-formats-for-bank-uploads">reformatting
it to FreeAgent's format</a>, and importing into FreeAgent.</p>
<p>This tool automates the reformatting.</p>
<h3>Select a file, and download the new one.</h3>
<input id="file" type="file" accept=".csv,.xlsx,.xls,text/csv,application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet">
<p>
<small>No data is uploaded to my server - the manipulation is done on your computer.</small>
</p>
<h4>Advanced Options</h4>
<p>
Change the description formatting. Use the CSV column names and the
<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals" target="_blank">Template
Literal syntax</a><br>
<input type="text" style="width: 100%; padding: 5px" id="template-description"
value="${ data['Description'] } (${ data['Exchange From'] ? `${ data['Exchange From'] } ${ data['Exchange To'] } ${ data['Exchange Rate'] }, ` : '' }Fee ${ data['Total fees'] })">
</p>
<p>
Show fees on a separate line:
<input type="checkbox" id="fees-on-separate-line" name="fees-on-separate-line" value="Show fees on a separate line">
</p>
<h3>Notes</h3>
<p>
Despite what
<a target="_blank" href="https://support.freeagent.com/hc/en-gb/articles/115001222564-CSV-file-formats-for-bank-uploads">
FreeAgent's documentation
</a>
says, FreeAgent will import CSVs containing double quotes around strings, and commas within those
double quoted strings. I make use of that for the description field, to allow better data formatting.
</p>
<p>
What FreeAgent cannot cope with is <code>></code> in a string. It renders it as a literal <code>&gt;</code>.
It's a bug in FreeAgent, but not going to be high priority for them to fix.
</p>
<script src="js/papaparse.min.js"></script>
<script>
// From: https://github.com/mholt/PapaParse/issues/175#issuecomment-395978144
// For a bigger alternative, see https://github.com/eligrey/FileSaver.js/
function openSaveFileDialog(data, filename, mimetype) {
if (!data) return;
var blob = data.constructor !== Blob
? new Blob([data], {type: mimetype || 'application/octet-stream'})
: data;
if (navigator.msSaveBlob) {
navigator.msSaveBlob(blob, filename);
return;
}
var lnk = document.createElement('a'),
url = window.URL,
objectURL;
if (mimetype) {
lnk.type = mimetype;
}
lnk.download = filename || 'untitled';
lnk.href = objectURL = url.createObjectURL(blob);
lnk.dispatchEvent(new MouseEvent('click'));
setTimeout(url.revokeObjectURL.bind(url, objectURL));
}
</script>
<script>
fileInput = document.getElementById('file');
fileInput.addEventListener('change', function (event) {
/** @var file File */
var file = event.target.files[0];
Papa.parse(file, {
header: true,
delimiter: ',',
complete: function (results) {
console.log(results);
// if (results.errors.length > 0) {
// console.log('CSV parsing errors: ...');
// return;
// }
var out = [];
// Get rid of the header
//results.data.shift();
results.data.forEach(function (data) {
if (Object.keys(data).length < 15) return;
console.log('total fees are ' + parseFloat(data['Total fees']));
// feesAmount is always positive
let feesAmount = parseFloat(data['Total fees']);
if (document.getElementById("fees-on-separate-line").checked === true && feesAmount > 0) {
let origAmount = parseFloat(data['Amount']);
let amount = origAmount < 0 ? origAmount + feesAmount : origAmount - feesAmount;
out.push([data['Date'], amount, eval('`' + document.getElementById('template-description').value + '`')]);
out.push([data['Date'], '-' + data['Total fees'], 'Transferwise Fees: ' + data['Description']]);
} else {
//out.push([data['Date'], data['Amount'], `${ data['Description'] } (${ data['Exchange From'] ? `${ data['Exchange From'] }->${ data['Exchange To'] } ${ data['Exchange Rate'] }, ` : '' }Fee ${ data['Total fees'] })`]);
out.push([data['Date'], data['Amount'], eval('`' + document.getElementById('template-description').value + '`')]);
}
console.log(data)
});
console.log(out);
var csv = Papa.unparse(out);
console.log(csv);
openSaveFileDialog(csv, 'FreeAgent formatted - ' + file.name, 'text/csv')
}
});
console.log(event.target.files);
})
</script>
</body>
</html>