let _xlsx: typeof import("xlsx") | null = null; async function getXLSX() { if (!_xlsx) { _xlsx = await import("xlsx"); } return _xlsx; } /** * Parse an Excel (.xlsx, .xls) or CSV file to an array of row objects. * Keys come from the first row (headers). */ export async function parseSpreadsheet(file: File): Promise[]> { const XLSX = await getXLSX(); const buffer = await file.arrayBuffer(); const data = new Uint8Array(buffer); const workbook = XLSX.read(data, { type: "array" }); const sheetName = workbook.SheetNames[0]; if (!sheetName) { return []; } const sheet = workbook.Sheets[sheetName]; if (!sheet) { return []; } return XLSX.utils.sheet_to_json>(sheet, { raw: false, defval: "", }); } export function isSpreadsheetFile(file: File): boolean { return ( file.type === "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" || file.type === "application/vnd.ms-excel" || file.name.endsWith(".xlsx") || file.name.endsWith(".xls") || file.name.endsWith(".csv") ); }