Преглед на файлове

Fix Excel import: use sheet_to_json to handle multi-line cells correctly

sheet_to_csv was splitting on embedded newlines within cells (e.g. Long
Description column), causing column misalignment for all subsequent rows.
Switch to sheet_to_json with header:1, replace in-cell newlines with
spaces, then build a flat CSV our parseCSV can handle correctly.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Tony T преди 1 ден
родител
ревизия
c52d0b95a8
променени са 1 файла, в които са добавени 11 реда и са изтрити 3 реда
  1. 11 3
      client/src/pages/DataSources.tsx

+ 11 - 3
client/src/pages/DataSources.tsx

@@ -285,9 +285,17 @@ export default function DataSources() {
           const data = new Uint8Array(ev.target?.result as ArrayBuffer);
           const wb = XLSX.read(data, { type: "array" });
           const ws = wb.Sheets[wb.SheetNames[0]];
-          const csv = XLSX.utils.sheet_to_csv(ws);
-          setter(csv);
-          toast.success(`Excel parsed: ${wb.SheetNames[0]}`);
+          // Use sheet_to_json so multi-line cells are kept intact per cell,
+          // then sanitise newlines before building a flat CSV our parser can handle.
+          const rawRows = XLSX.utils.sheet_to_json<any[]>(ws, { header: 1, defval: "" });
+          const safeCsv = rawRows.map((row: any[]) =>
+            row.map((cell: any) => {
+              const s = String(cell ?? "").replace(/\r?\n/g, " ").trim();
+              return s.includes(",") || s.includes('"') ? `"${s.replace(/"/g, '""')}"` : s;
+            }).join(",")
+          ).join("\n");
+          setter(safeCsv);
+          toast.success(`Excel parsed: ${wb.SheetNames[0]} (${rawRows.length - 1} rows)`);
         } catch {
           toast.error("Failed to parse Excel file");
         }