This commit is contained in:
Tomas Dvorak
2025-06-11 23:04:34 +02:00
parent 2e78f34388
commit 57e0e6b570
+33 -35
View File
@@ -4382,7 +4382,7 @@ function filterReservations() {
displayReservations(filtered); displayReservations(filtered);
} }
// Function to export reservations to CSV // Function to export reservations to CSV with proper column separation
function exportReservations() { function exportReservations() {
if (!window.allReservations || !window.allReservations.length) { if (!window.allReservations || !window.allReservations.length) {
showNotification('Žádné rezervace k exportu', 'warning'); showNotification('Žádné rezervace k exportu', 'warning');
@@ -4421,50 +4421,48 @@ function exportReservations() {
// Format date and time as DD.MM.YYYY HH:MM // Format date and time as DD.MM.YYYY HH:MM
const formatDateTime = (dateString) => { const formatDateTime = (dateString) => {
const date = new Date(dateString); if (!dateString) return '';
const day = String(date.getDate()).padStart(2, '0'); try {
const month = String(date.getMonth() + 1).padStart(2, '0'); const date = new Date(dateString);
const year = date.getFullYear(); if (isNaN(date.getTime())) return '';
const hours = String(date.getHours()).padStart(2, '0'); const day = String(date.getDate()).padStart(2, '0');
const minutes = String(date.getMinutes()).padStart(2, '0'); const month = String(date.getMonth() + 1).padStart(2, '0');
return `${day}.${month}.${year} ${hours}:${minutes}`; const year = date.getFullYear();
}; const hours = String(date.getHours()).padStart(2, '0');
const minutes = String(date.getMinutes()).padStart(2, '0');
// Escape CSV values (handles quotes and special characters) return `${day}.${month}.${year} ${hours}:${minutes}`;
const escapeCsv = (value) => { } catch (e) {
if (value === null || value === undefined) return ''; console.error('Error formatting date:', e);
const stringValue = String(value); return '';
// 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}"`;
} }
return escaped;
}; };
// Create CSV content // Create CSV content with semicolon delimiter
const headers = ['Řidič', 'Vozidlo', 'Od', 'Do', 'Účel']; const headers = ['Řidič', 'Vozidlo', 'Od', 'Do', 'Účel'];
const csvRows = [ // Create CSV rows
headers.join(';'), // Header row const csvRows = [];
...dataToExport.map(res => {
const row = [ // Add header row
escapeCsv(res.driverName || ''), csvRows.push(headers.join(';'));
escapeCsv(res.vehicle || ''),
escapeCsv(formatDateTime(res.start)), // Add data rows
escapeCsv(formatDateTime(res.end)), for (const res of dataToExport) {
escapeCsv(res.purpose || '') const row = [
]; `"${String(res.driverName || '').replace(/"/g, '""')}"`,
return row.join(';'); `"${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 // Create CSV string with BOM for Excel
const csvString = '\uFEFF' + csvRows.join('\r\n'); const csvString = '\uFEFF' + csvRows.join('\r\n');
// Create and trigger download // 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 link = document.createElement('a');
const timestamp = new Date().toISOString().split('T')[0]; const timestamp = new Date().toISOString().split('T')[0];