fix csv download
This commit is contained in:
parent
1498ae5670
commit
6c2417aa20
1 changed files with 26 additions and 8 deletions
|
@ -300,6 +300,18 @@ export default function Attendees() {
|
||||||
|
|
||||||
// Function to download attendees as CSV
|
// Function to download attendees as CSV
|
||||||
const downloadAttendeesCSV = () => {
|
const downloadAttendeesCSV = () => {
|
||||||
|
// Function to sanitize and format CSV cell content
|
||||||
|
const escapeCSV = (cell: any): string => {
|
||||||
|
// Convert to string and replace any newlines with spaces
|
||||||
|
const value = (cell?.toString() || '').replace(/[\r\n]+/g, ' ').trim();
|
||||||
|
|
||||||
|
// If the value contains quotes or commas, wrap in quotes and escape internal quotes
|
||||||
|
if (value.includes('"') || value.includes(',')) {
|
||||||
|
return `"${value.replace(/"/g, '""')}"`;
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
};
|
||||||
|
|
||||||
// Create CSV headers
|
// Create CSV headers
|
||||||
const headers = [
|
const headers = [
|
||||||
'Name',
|
'Name',
|
||||||
|
@ -311,7 +323,7 @@ export default function Attendees() {
|
||||||
'Major',
|
'Major',
|
||||||
'Check-in Time',
|
'Check-in Time',
|
||||||
'Food Choice'
|
'Food Choice'
|
||||||
];
|
].map(escapeCSV);
|
||||||
|
|
||||||
// Create CSV rows
|
// Create CSV rows
|
||||||
const rows = attendeesList.map(attendee => {
|
const rows = attendeesList.map(attendee => {
|
||||||
|
@ -327,26 +339,32 @@ export default function Attendees() {
|
||||||
user?.major || 'N/A',
|
user?.major || 'N/A',
|
||||||
checkInTime,
|
checkInTime,
|
||||||
attendee.food || 'N/A'
|
attendee.food || 'N/A'
|
||||||
];
|
].map(escapeCSV);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Combine headers and rows
|
// Combine headers and rows with Windows-style line endings
|
||||||
const csvContent = [
|
const csvContent = [
|
||||||
headers.join(','),
|
headers.join(','),
|
||||||
...rows.map(row => row.map(cell => `"${cell}"`).join(','))
|
...rows.map(row => row.join(','))
|
||||||
].join('\n');
|
].join('\r\n');
|
||||||
|
|
||||||
// Create blob and download
|
// Create blob with UTF-8 BOM for better Excel compatibility
|
||||||
const blob = new Blob([csvContent], { type: 'text/csv;charset=utf-8;' });
|
const BOM = '\uFEFF';
|
||||||
|
const blob = new Blob([BOM + csvContent], { type: 'text/csv;charset=utf-8;' });
|
||||||
const link = document.createElement('a');
|
const link = document.createElement('a');
|
||||||
const url = URL.createObjectURL(blob);
|
const url = URL.createObjectURL(blob);
|
||||||
|
|
||||||
|
// Create filename with date and time
|
||||||
|
const timestamp = new Date().toISOString().replace(/[:.]/g, '-').slice(0, -5);
|
||||||
|
const filename = `${eventName.replace(/[^a-z0-9]/gi, '_').toLowerCase()}_attendees_${timestamp}.csv`;
|
||||||
|
|
||||||
link.setAttribute('href', url);
|
link.setAttribute('href', url);
|
||||||
link.setAttribute('download', `${eventName.replace(/[^a-z0-9]/gi, '_').toLowerCase()}_attendees.csv`);
|
link.setAttribute('download', filename);
|
||||||
link.style.visibility = 'hidden';
|
link.style.visibility = 'hidden';
|
||||||
document.body.appendChild(link);
|
document.body.appendChild(link);
|
||||||
link.click();
|
link.click();
|
||||||
document.body.removeChild(link);
|
document.body.removeChild(link);
|
||||||
|
URL.revokeObjectURL(url); // Clean up the URL object
|
||||||
};
|
};
|
||||||
|
|
||||||
if (loading) {
|
if (loading) {
|
||||||
|
|
Loading…
Reference in a new issue