Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use sheetjs for csv export #25

Merged
merged 1 commit into from
Feb 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 20 additions & 35 deletions src/lib/DataUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,27 +46,11 @@ export function exportCSV(data: any[]): string {
if (!Array.isArray(data) || data.length === 0) {
throw new Error('No data found');
}
const header = Object.keys(data[0]).join(',');
const csvRows = data.map((row) => {
const values = Object.values(row).map((value) => {
if (typeof value === 'string') {
let stringValue = value.replace(/"/g, '""'); // Escape double quotes
if (stringValue.includes('#@-@#')) {
const busTrainSplit = stringValue.split('#@-@#');
stringValue = busTrainSplit[1];
}
if (stringValue.includes(',') || stringValue.includes('\n')) {
stringValue = `"${stringValue}"`; // Quote fields containing commas or newlines
}
return stringValue;
}
return String(value);
});
return values.join(',');
});
csvRows.unshift(header); // Add header row at the beginning

const csvString = csvRows.join('\n');
const processedData = processExportData(data);
const wb = XLSX.utils.book_new();
const ws = XLSX.utils.json_to_sheet(processedData);
XLSX.utils.book_append_sheet(wb, ws, 'Data');
const csvString = XLSX.write(wb, { bookType: 'csv', type: 'string' });
const blob = new Blob([csvString], { type: 'text/csv' });
return URL.createObjectURL(blob);
}
Expand All @@ -75,30 +59,17 @@ export function exportXLS(data: any[]): string {
if (!Array.isArray(data) || data.length === 0) {
throw new Error('No data found');
}

const processedData = data.map((row) =>
Object.keys(row).reduce((acc, key) => {
let value: string | number = row[key];
if (typeof value === 'string' && value.includes('#@-@#')) {
[, value] = value.split('#@-@#');
}
acc[key] = value;
return acc;
}, {} as any)
);

const processedData = processExportData(data);
const wb = XLSX.utils.book_new();
const ws = XLSX.utils.json_to_sheet(processedData);
XLSX.utils.book_append_sheet(wb, ws, 'Data');
const wbout = XLSX.write(wb, { bookType: 'xls', type: 'binary' });

function s2ab(s: string): ArrayBuffer {
const buf = new ArrayBuffer(s.length);
const view = new Uint8Array(buf);
for (let i = 0; i < s.length; i++) view[i] = s.charCodeAt(i) & 0xff;
return buf;
}

const blob = new Blob([s2ab(wbout)], { type: 'application/vnd.ms-excel' });
return URL.createObjectURL(blob);
}
Expand All @@ -117,6 +88,20 @@ export function exportKML(data: Meeting[]): string {
return URL.createObjectURL(blob);
}

// This is for Bus/Train/Custom fields
function processExportData(data: any[]): any[] {
return data.map((row) =>
Object.keys(row).reduce((acc, key) => {
let value: string | number = row[key];
if (typeof value === 'string' && value.includes('#@-@#')) {
[, value] = value.split('#@-@#');
}
acc[key] = value;
return acc;
}, {} as any)
);
}

function createPlacemark(meeting: Meeting): string {
const name = meeting.meeting_name.trim() || 'NA Meeting';
const lng = parseFloat(meeting.longitude);
Expand Down