diff --git a/admin-dashboard.html b/admin-dashboard.html index 882c97c..9c2ac0c 100644 --- a/admin-dashboard.html +++ b/admin-dashboard.html @@ -4382,7 +4382,7 @@ function filterReservations() { displayReservations(filtered); } -// Function to export reservations to CSV +// Function to export reservations to CSV with proper column separation function exportReservations() { if (!window.allReservations || !window.allReservations.length) { showNotification('Žádné rezervace k exportu', 'warning'); @@ -4421,50 +4421,48 @@ function exportReservations() { // Format date and time as DD.MM.YYYY HH:MM const formatDateTime = (dateString) => { - const date = new Date(dateString); - const day = String(date.getDate()).padStart(2, '0'); - const month = String(date.getMonth() + 1).padStart(2, '0'); - const year = date.getFullYear(); - const hours = String(date.getHours()).padStart(2, '0'); - const minutes = String(date.getMinutes()).padStart(2, '0'); - return `${day}.${month}.${year} ${hours}:${minutes}`; - }; - - // Escape CSV values (handles quotes and special characters) - const escapeCsv = (value) => { - if (value === null || value === undefined) return ''; - const stringValue = String(value); - // Escape double quotes by doubling them - const escaped = stringValue.replace(/"/g, '""'); - // Wrap in quotes if contains comma, newline, or quote - if (escaped.includes(',') || escaped.includes('\n') || escaped.includes('"')) { - return `"${escaped}"`; + if (!dateString) return ''; + try { + const date = new Date(dateString); + if (isNaN(date.getTime())) return ''; + const day = String(date.getDate()).padStart(2, '0'); + const month = String(date.getMonth() + 1).padStart(2, '0'); + const year = date.getFullYear(); + const hours = String(date.getHours()).padStart(2, '0'); + const minutes = String(date.getMinutes()).padStart(2, '0'); + return `${day}.${month}.${year} ${hours}:${minutes}`; + } catch (e) { + console.error('Error formatting date:', e); + return ''; } - return escaped; }; - // Create CSV content + // Create CSV content with semicolon delimiter const headers = ['Řidič', 'Vozidlo', 'Od', 'Do', 'Účel']; - const csvRows = [ - headers.join(';'), // Header row - ...dataToExport.map(res => { - const row = [ - escapeCsv(res.driverName || ''), - escapeCsv(res.vehicle || ''), - escapeCsv(formatDateTime(res.start)), - escapeCsv(formatDateTime(res.end)), - escapeCsv(res.purpose || '') - ]; - return row.join(';'); - }) - ]; + // Create CSV rows + const csvRows = []; + + // Add header row + csvRows.push(headers.join(';')); + + // Add data rows + for (const res of dataToExport) { + const row = [ + `"${String(res.driverName || '').replace(/"/g, '""')}"`, + `"${String(res.vehicle || '').replace(/"/g, '""')}"`, + `"${formatDateTime(res.start) || ''}"`, + `"${formatDateTime(res.end) || ''}"`, + `"${String(res.purpose || '').replace(/"/g, '""')}"` + ]; + csvRows.push(row.join(';')); + } // Create CSV string with BOM for Excel const csvString = '\uFEFF' + csvRows.join('\r\n'); // Create and trigger download - const blob = new Blob([csvString], { type: 'text/csv;charset=utf-8;' }); + const blob = new Blob([csvString], { type: 'text/csv;charset=utf-8' }); const link = document.createElement('a'); const timestamp = new Date().toISOString().split('T')[0];