Skip to content

Commit

Permalink
use sheetjs for csv export (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
pjaudiomv authored Feb 18, 2024
1 parent 0f29a3b commit e980703
Showing 1 changed file with 20 additions and 35 deletions.
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

0 comments on commit e980703

Please sign in to comment.