Skip to content

Commit

Permalink
refactor: added error handling, removednot required functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ayeks committed Mar 12, 2024
1 parent 5106319 commit 0b5c954
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 26 deletions.
21 changes: 21 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"milligram": "^1.4.1",
"papaparse": "^5.4.1",
"vue": "^3.2.13",
"vue-sweetalert2": "^5.0.5",
"xlsx": "^0.18.5"
},
"devDependencies": {
Expand Down
50 changes: 24 additions & 26 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
<div id="app" class="container">
<div>
<h3 class="title">Column Name Mapping</h3>
<p>Change only if you want to map different collumns of the XLSX to the CSV files or if your export is not in German:</p>
<p>Change only if you want to map different collumns of the XLSX to the CSV files or if your export is not in
German:</p>
</div>
<table>
<thead>
Expand All @@ -24,10 +25,10 @@
</tr>
</tbody>
</table>

<div>
<label for="fileInput" class="button file-label">Select a Finanzguru XLSX file to generate YNAB CSV files</label>
<input @change="handleFileUpload" class="hidden" type="file" id="fileInput" accept=".xlsx" hidden/>
<input @change="handleFileUpload" class="hidden" type="file" id="fileInput" accept=".xlsx" hidden />
</div>

</div>
Expand All @@ -36,6 +37,7 @@
<script>
import * as XLSX from "xlsx";
import * as Papa from "papaparse";
import Swal from 'sweetalert2';
export default {
data() {
Expand All @@ -49,19 +51,32 @@ export default {
},
methods: {
// Error handling
showErrorDialog(errorMessage) {
Swal.fire({
icon: 'error',
title: 'Error',
text: errorMessage,
});
},
// Trigger parsing on file upload
handleFileUpload() {
const fileInput = document.getElementById("fileInput");
const file = fileInput.files[0];
if (!file) {
// Handle the case where no file is selected
console.error("No file selected.");
this.showErrorDialog(`No file selected.`);
return;
}
this.readFile(file);
try {
this.readFile(file);
} catch (error) {
this.showErrorDialog(`Error processing Excel data: ${error.message}`);
}
},
// Read in the Excel file
readFile(file) {
const reader = new FileReader();
Expand All @@ -73,16 +88,14 @@ export default {
reader.readAsArrayBuffer(file);
},
// Parse the Excel file
processExcelData(data) {
const workbook = XLSX.read(data, { type: "array" });
const sheet = workbook.Sheets[workbook.SheetNames[0]];
const jsonData = XLSX.utils.sheet_to_json(sheet, {
raw: false, // This ensures that dates are parsed as JavaScript Date objects
});
// Extract headers
const headers = Object.keys(jsonData[0]);
// Set default values if not provided
const {
string_account,
Expand All @@ -92,17 +105,6 @@ export default {
string_amount,
} = this;
// Rename columns
headers.forEach((header) => {
sheet[header] = this.renameColumn(sheet[header], {
[string_account]: "Account",
[string_date]: "Date",
[string_payee]: "Payee",
[string_memo]: "Memo",
[string_amount]: "Amount",
});
});
// Group by account using reduce
const grouped = jsonData.reduce((acc, item) => {
const account = item[string_account];
Expand All @@ -126,14 +128,10 @@ export default {
});
},
renameColumn(column, mapping) {
return mapping[column] || column;
},
// Trigger CSV download
downloadCSV(data, filename) {
const csvContent = Papa.unparse(data);
const blob = new Blob([csvContent], { type: "text/csv;charset=utf-8;" });
const link = document.createElement("a");
link.setAttribute("href", URL.createObjectURL(blob));
link.setAttribute("download", `${filename}.csv`);
Expand Down
Binary file removed src/assets/logo.png
Binary file not shown.

0 comments on commit 0b5c954

Please sign in to comment.