From 84d8136c4b8a7eb9c6b43beb1a7b490d9191d63c Mon Sep 17 00:00:00 2001 From: Tale Agent Date: Mon, 5 Oct 2026 19:19:04 +0000 Subject: [PATCH 1/3] fix(platform): read Excel headers from worksheet --- .../platform/lib/utils/file-parsing.test.ts | 62 ++++++++++++++++++- services/platform/lib/utils/file-parsing.ts | 20 ++++-- 2 files changed, 76 insertions(+), 6 deletions(-) diff --git a/services/platform/lib/utils/file-parsing.test.ts b/services/platform/lib/utils/file-parsing.test.ts index 0370d9b39a..8594efab28 100644 --- a/services/platform/lib/utils/file-parsing.test.ts +++ b/services/platform/lib/utils/file-parsing.test.ts @@ -1,12 +1,35 @@ -import { describe, it, expect } from 'vitest'; +import { describe, it, expect, vi } from 'vitest'; import * as XLSX from 'xlsx'; import { excelRecords, + parseImportFile, parseCSVWithMapper, type RequiredColumn, } from './file-parsing'; +class TestFileReader { + result: string | ArrayBuffer | null = null; + private listeners = new Map void)[]>(); + + addEventListener(type: string, listener: (event: Event) => void) { + this.listeners.set(type, [...(this.listeners.get(type) ?? []), listener]); + } + + readAsArrayBuffer(file: Blob) { + void file.arrayBuffer().then((result) => { + this.result = result; + this.listeners + .get('load') + ?.forEach((listener) => listener({ target: this } as unknown as Event)); + }); + } +} + +// parseImportFile uses the browser FileReader API; this small adapter exercises +// the same byte path in the server test environment. +vi.stubGlobal('FileReader', TestFileReader); + type Row = { email: string; name?: string }; // A record mapper that accepts a couple of header aliases for email/name. @@ -231,3 +254,40 @@ describe('excelRecords line numbers', () => { ]); }); }); + +describe('Excel header validation', () => { + it('uses the worksheet header when the first data row has blank cells', async () => { + const workbook = XLSX.utils.book_new(); + XLSX.utils.book_append_sheet( + workbook, + XLSX.utils.aoa_to_sheet([ + ['name', 'price', 'stock'], + ['Blank amounts', '', ''], + ['Full amounts', 12, 3], + ]), + 'Products', + ); + const file = new File( + [XLSX.write(workbook, { bookType: 'xlsx', type: 'array' })], + 'products.xlsx', + ); + const result = await parseImportFile( + file, + () => null, + (record) => record, + { + requiredColumns: [ + { label: 'price', aliases: ['price'] }, + { label: 'stock', aliases: ['stock'] }, + ], + }, + ); + + expect(result.errors).toEqual([]); + expect(result.data).toEqual([ + { name: 'Blank amounts', price: '', stock: '' }, + { name: 'Full amounts', price: 12, stock: 3 }, + ]); + expect(result.rows).toEqual([2, 3]); + }); +}); diff --git a/services/platform/lib/utils/file-parsing.ts b/services/platform/lib/utils/file-parsing.ts index 8efd6146b7..40842709b9 100644 --- a/services/platform/lib/utils/file-parsing.ts +++ b/services/platform/lib/utils/file-parsing.ts @@ -434,14 +434,26 @@ export function excelRecords( * Parse an Excel file and return its rows with their lines. * Dynamically imports xlsx to reduce initial bundle size. */ -async function parseExcelFile(file: File): Promise { +async function parseExcelFile( + file: File, +): Promise<{ headers: string[]; records: ExcelRecord[] }> { const XLSX = await import('xlsx'); const buffer = await readFileAsArrayBuffer(file); const data = new Uint8Array(buffer); const workbook = XLSX.read(data, { type: 'array' }); const sheetName = workbook.SheetNames[0]; const worksheet = workbook.Sheets[sheetName]; - return excelRecords(XLSX, worksheet); + const headerRows = XLSX.utils.sheet_to_json(worksheet, { + header: 1, + range: 0, + blankrows: false, + }); + const headers = (headerRows[0] ?? []).map((header) => + String(header ?? '') + .trim() + .toLowerCase(), + ); + return { headers, records: excelRecords(XLSX, worksheet) }; } function isCSVFile(file: File): boolean { @@ -473,12 +485,10 @@ export async function parseImportFile( }); return result; } else if (isExcelFile(file)) { - const records = await parseExcelFile(file); + const { headers: headerKeys, records } = await parseExcelFile(file); // Validate the header row (the keys of the first record) so a // mismatched schema fails loudly rather than dropping data silently. - const headerKeys = - records.length > 0 ? Object.keys(records[0].record) : []; const missing = detectMissingColumns(headerKeys, options.requiredColumns); if (missing.length > 0) { return emptyResult([missingColumnsError(missing, headerKeys)]); From fd2ade09db63872952cf594e861230b3ea6f8d70 Mon Sep 17 00:00:00 2001 From: Tale Agent Date: Mon, 5 Oct 2026 20:10:30 +0000 Subject: [PATCH 2/3] fix(platform): safely convert Excel header cells --- .../platform/lib/utils/file-parsing.test.ts | 16 ++++++++++++++++ services/platform/lib/utils/file-parsing.ts | 19 ++++++++++++++++--- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/services/platform/lib/utils/file-parsing.test.ts b/services/platform/lib/utils/file-parsing.test.ts index 8594efab28..52084d5d2b 100644 --- a/services/platform/lib/utils/file-parsing.test.ts +++ b/services/platform/lib/utils/file-parsing.test.ts @@ -3,6 +3,7 @@ import * as XLSX from 'xlsx'; import { excelRecords, + excelHeaderText, parseImportFile, parseCSVWithMapper, type RequiredColumn, @@ -255,6 +256,21 @@ describe('excelRecords line numbers', () => { }); }); +describe('Excel header cell conversion', () => { + it('accepts primitive, date, and rich-text values without object coercion', () => { + expect(excelHeaderText(' Email ')).toBe(' Email '); + expect(excelHeaderText(42)).toBe('42'); + expect(excelHeaderText(false)).toBe('false'); + expect(excelHeaderText(new Date('2024-01-02T03:04:05.000Z'))).toBe( + '2024-01-02T03:04:05.000Z', + ); + expect(excelHeaderText({ text: 'Email address' })).toBe('Email address'); + expect(excelHeaderText({ w: 'Formatted header' })).toBe('Formatted header'); + expect(excelHeaderText({ unexpected: 'object' })).toBe(''); + expect(excelHeaderText(null)).toBe(''); + }); +}); + describe('Excel header validation', () => { it('uses the worksheet header when the first data row has blank cells', async () => { const workbook = XLSX.utils.book_new(); diff --git a/services/platform/lib/utils/file-parsing.ts b/services/platform/lib/utils/file-parsing.ts index 40842709b9..23f59f3296 100644 --- a/services/platform/lib/utils/file-parsing.ts +++ b/services/platform/lib/utils/file-parsing.ts @@ -430,6 +430,21 @@ export function excelRecords( })); } +/** Convert a worksheet header cell to its textual column name safely. */ +export function excelHeaderText(value: unknown): string { + if (typeof value === 'string') return value; + if (typeof value === 'number' || typeof value === 'boolean') + return String(value); + if (value instanceof Date) return value.toISOString(); + if (typeof value === 'object' && value !== null) { + const text = (value as { text?: unknown; w?: unknown }).text; + if (typeof text === 'string') return text; + const formatted = (value as { text?: unknown; w?: unknown }).w; + if (typeof formatted === 'string') return formatted; + } + return ''; +} + /** * Parse an Excel file and return its rows with their lines. * Dynamically imports xlsx to reduce initial bundle size. @@ -449,9 +464,7 @@ async function parseExcelFile( blankrows: false, }); const headers = (headerRows[0] ?? []).map((header) => - String(header ?? '') - .trim() - .toLowerCase(), + excelHeaderText(header).trim().toLowerCase(), ); return { headers, records: excelRecords(XLSX, worksheet) }; } From b289ff26e0a242d544f6430a99d468f9025a7ae4 Mon Sep 17 00:00:00 2001 From: Tale Agent Date: Thu, 8 Oct 2026 03:15:17 +0000 Subject: [PATCH 3/3] fix(platform): reproduce absent Excel first-row cells --- services/platform/lib/utils/file-parsing.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/services/platform/lib/utils/file-parsing.test.ts b/services/platform/lib/utils/file-parsing.test.ts index 52084d5d2b..c0529b72bb 100644 --- a/services/platform/lib/utils/file-parsing.test.ts +++ b/services/platform/lib/utils/file-parsing.test.ts @@ -278,7 +278,7 @@ describe('Excel header validation', () => { workbook, XLSX.utils.aoa_to_sheet([ ['name', 'price', 'stock'], - ['Blank amounts', '', ''], + ['Blank amounts'], ['Full amounts', 12, 3], ]), 'Products', @@ -301,7 +301,7 @@ describe('Excel header validation', () => { expect(result.errors).toEqual([]); expect(result.data).toEqual([ - { name: 'Blank amounts', price: '', stock: '' }, + { name: 'Blank amounts' }, { name: 'Full amounts', price: 12, stock: 3 }, ]); expect(result.rows).toEqual([2, 3]);