feat: Excel import/export module via SheetJS

This commit is contained in:
Ferdinand
2026-04-08 13:17:12 +02:00
parent adf656c9f3
commit 72a2af3a74

35
js/excel.js Normal file
View File

@@ -0,0 +1,35 @@
// Reads employee names from first column of xlsx (skips header row 0)
export function readEmployeeNames(arrayBuffer) {
const wb = XLSX.read(arrayBuffer, { type: 'array' });
const sheet = wb.Sheets[wb.SheetNames[0]];
const rows = XLSX.utils.sheet_to_json(sheet, { header: 1 });
return rows.slice(1)
.map(row => String(row[0] ?? '').trim())
.filter(name => name.length > 0);
}
// Exports schedule as xlsx blob
// assignments: [{ date: "2026-04-01", employeeId }]
// employees: [{ id, name }]
export function exportSchedule(assignments, employees) {
const nameById = Object.fromEntries(employees.map(e => [e.id, e.name]));
const names = assignments.map(a => nameById[a.employeeId] ?? '');
const dates = assignments.map(a => {
const [y, m, d] = a.date.split('-');
return d + '.' + m + '.' + y;
});
const ws = XLSX.utils.aoa_to_sheet([names, dates]);
const wb = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(wb, ws, 'Moderationsplan');
const buf = XLSX.write(wb, { type: 'array', bookType: 'xlsx' });
return new Blob([buf], { type: 'application/octet-stream' });
}
export function downloadBlob(blob, filename) {
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = filename;
a.click();
URL.revokeObjectURL(url);
}