From 72a2af3a74da3bca115c347722e79f56789ae1ac Mon Sep 17 00:00:00 2001 From: Ferdinand Date: Wed, 8 Apr 2026 13:17:12 +0200 Subject: [PATCH] feat: Excel import/export module via SheetJS --- js/excel.js | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 js/excel.js diff --git a/js/excel.js b/js/excel.js new file mode 100644 index 0000000..36f6e78 --- /dev/null +++ b/js/excel.js @@ -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); +}