upraven index.html,přidán testovací csv soubor a upraven skrip.js
This commit is contained in:
107
skript.js
107
skript.js
@ -1,56 +1,59 @@
|
||||
document.addEventListener('DOMContentLoaded', function() {
|
||||
fetch('finance.csv')
|
||||
.then(response => {
|
||||
if (!response.ok) {
|
||||
throw new Error('Network response was not ok');
|
||||
}
|
||||
return response.text();
|
||||
})
|
||||
.then(data => {
|
||||
let rows = data.split('\n').slice(1);
|
||||
rows = rows.filter(row => row.trim() !== ""); // Remove empty rows
|
||||
rows.reverse(); // Reverse the order of rows
|
||||
const tableBody = document.querySelector('#finance-table tbody');
|
||||
let accountBalance = 0;
|
||||
rows.forEach(row => {
|
||||
const columns = row.split(',');
|
||||
if (columns.length >= 5) {
|
||||
const tr = document.createElement('tr');
|
||||
columns.forEach(column => {
|
||||
const td = document.createElement('td');
|
||||
td.textContent = column.trim();
|
||||
tr.appendChild(td);
|
||||
});
|
||||
tableBody.appendChild(tr);
|
||||
|
||||
// Debug output
|
||||
console.log('Row:', row);
|
||||
console.log('Columns:', columns);
|
||||
|
||||
// Calculate account balance
|
||||
const amount = parseFloat(columns[2].replace(/,/g, '').replace(/[^0-9.-]/g, '')); // Remove any invalid characters and ensure proper decimal handling
|
||||
const currency = columns[3].trim();
|
||||
|
||||
// Debug output
|
||||
console.log('Amount:', amount);
|
||||
console.log('Currency:', currency);
|
||||
|
||||
if (!isNaN(amount)) {
|
||||
if (currency === 'CZK') {
|
||||
accountBalance += amount;
|
||||
} else if (currency === 'EUR') {
|
||||
// For simplicity, assume 1 EUR = 25 CZK (you can adjust the conversion rate)
|
||||
accountBalance += amount * 25;
|
||||
}
|
||||
} else {
|
||||
console.error('Invalid amount:', columns[2]);
|
||||
}
|
||||
// Funkce pro načítání financí
|
||||
function loadFinanceData(csvFile) {
|
||||
console.log(`Načítání souboru: ${csvFile}`); // Debug výpis
|
||||
fetch(csvFile)
|
||||
.then(response => {
|
||||
if (!response.ok) {
|
||||
throw new Error('Network response was not ok');
|
||||
}
|
||||
return response.text();
|
||||
})
|
||||
.then(data => {
|
||||
console.log('Načtená data:', data); // Debug výpis načtených dat
|
||||
let rows = data.split('\n').slice(1); // Odstraníme hlavičku CSV souboru
|
||||
rows = rows.filter(row => row.trim() !== ""); // Odstranit prázdné řádky
|
||||
rows.reverse(); // Obrátit pořadí řádků
|
||||
const tableBody = document.querySelector('#finance-table tbody');
|
||||
tableBody.innerHTML = ''; // Vyprázdnit tabulku před načtením nových dat
|
||||
let accountBalance = 0;
|
||||
rows.forEach(row => {
|
||||
const columns = row.split(',');
|
||||
if (columns.length >= 5) {
|
||||
const tr = document.createElement('tr');
|
||||
columns.forEach(column => {
|
||||
const td = document.createElement('td');
|
||||
td.textContent = column.trim();
|
||||
tr.appendChild(td);
|
||||
});
|
||||
tableBody.appendChild(tr);
|
||||
|
||||
// Výpočet zůstatku
|
||||
const amount = parseFloat(columns[2].replace(/,/g, '').replace(/[^0-9.-]/g, '')); // Ošetření čísel
|
||||
const currency = columns[3].trim();
|
||||
|
||||
if (!isNaN(amount)) {
|
||||
if (currency === 'CZK') {
|
||||
accountBalance += amount;
|
||||
} else if (currency === 'EUR') {
|
||||
accountBalance += amount * 25; // Pro jednoduchost: 1 EUR = 25 CZK
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
document.getElementById('account-balance').textContent = accountBalance.toFixed(2) + ' CZK';
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Chyba při načítání CSV souboru:', error);
|
||||
document.getElementById('account-balance').textContent = 'Chyba při načítání dat';
|
||||
});
|
||||
document.getElementById('account-balance').textContent = accountBalance.toFixed(2) + ' CZK';
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Error fetching the CSV file:', error);
|
||||
document.getElementById('account-balance').textContent = 'Chyba při načítání dat';
|
||||
});
|
||||
}
|
||||
|
||||
// Načítání aktuálního souboru finance.csv
|
||||
loadFinanceData('finance.csv'); // Soubor je ve stejné složce, proto nemusíš zadávat cestu
|
||||
|
||||
// Přidání funkce pro načítání archivovaných dat za rok 2024
|
||||
document.getElementById('load-archive-2024').addEventListener('click', function() {
|
||||
loadFinanceData('finance_2024_corrected.csv'); // Stejná složka pro archivovaný soubor
|
||||
});
|
||||
});
|
||||
|
Reference in New Issue
Block a user