Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🐛 fix(file): support xlsx workbook chunking (#20478)

LiJiane49ae3777928
diff --git a/src/libs/document-loaders/loaderType.test.ts b/src/libs/document-loaders/loaderType.test.ts
index 648d54ef3d2..4c11a966b55 100644
--- a/src/libs/document-loaders/loaderType.test.ts
+++ b/src/libs/document-loaders/loaderType.test.ts
@@ -9,11 +9,13 @@ describe('getChunkingLoaderType', () => {
expect(getChunkingLoaderType('notes.md')).toBe('markdown');
expect(getChunkingLoaderType('main.go')).toBe('code');
expect(getChunkingLoaderType('data.csv')).toBe('csv');
+ expect(getChunkingLoaderType('Report.XLSX')).toBe('excel');
});
it('returns undefined for formats without a chunking loader', () => {
expect(getChunkingLoaderType('floor-plan.dwg')).toBeUndefined();
expect(getChunkingLoaderType('photo.png')).toBeUndefined();
+ expect(getChunkingLoaderType('legacy.xls')).toBeUndefined();
expect(getChunkingLoaderType('no-extension')).toBeUndefined();
});
});
@@ -28,6 +30,7 @@ describe('isChunkingSupported', () => {
});
it('follows the parser registry rather than the MIME prefix when the name is known', () => {
+ expect(isChunkingSupported({ fileType: '', name: 'data.xlsx' })).toBe(true);
// browsers report `.ts` as MPEG transport stream video
expect(isChunkingSupported({ fileType: 'video/mp2t', name: 'index.ts' })).toBe(true);
expect(isChunkingSupported({ fileType: 'application/pdf', name: 'paper.pdf' })).toBe(true);
diff --git a/src/libs/document-loaders/loaderType.ts b/src/libs/document-loaders/loaderType.ts
index 7c7590e5f6b..fc0f3747e42 100644
--- a/src/libs/document-loaders/loaderType.ts
+++ b/src/libs/document-loaders/loaderType.ts
@@ -20,6 +20,10 @@ export const getChunkingLoaderType = (filename: string): FileLoaderType | undefi
return 'doc';
}
+ if (name.endsWith('.xlsx')) {
+ return 'excel';
+ }
+
if (name.endsWith('pdf')) {
return 'pdf';
}
diff --git a/src/libs/document-loaders/loaders/excel/__tests__/index.test.ts b/src/libs/document-loaders/loaders/excel/__tests__/index.test.ts
new file mode 100644
index 00000000000..a32df4a2f38
--- /dev/null
+++ b/src/libs/document-loaders/loaders/excel/__tests__/index.test.ts
@@ -0,0 +1,118 @@
+// @vitest-environment node
+import { Workbook } from 'exceljs';
+import { describe, expect, it, vi } from 'vitest';
+
+import { ChunkingLoader } from '../../index';
+import { ExcelLoader } from '../index';
+
+const buildWorkbook = async () => {
+ const workbook = new Workbook();
+
+ const pricing = workbook.addWorksheet('Pricing');
+ pricing.addRow(['Plan', 'Discount']);
+ pricing.addRow(['Basic', '10%']);
+ pricing.addRow(['Pro', '20%']);
+
+ const notes = workbook.addWorksheet('Notes');
+ notes.addRow(['Owner', 'Signed']);
+ notes.addRow(['Alice', '2026-10-07']);
+
+ return new Uint8Array(await workbook.xlsx.writeBuffer());
+};
+
+describe('ExcelLoader', () => {
+ it('should chunk every sheet row into a header/value document', async () => {
+ const data = await ExcelLoader(new Blob([Buffer.from(await buildWorkbook())]));
+
+ expect(data).toHaveLength(3);
+ expect(data[0].metadata).toMatchObject({ row: 2, sheetName: 'Pricing', source: 'blob' });
+ expect(data[0].pageContent).toBe('Plan: Basic\nDiscount: 10%');
+ expect(data[1].pageContent).toBe('Plan: Pro\nDiscount: 20%');
+ expect(data[2].metadata.sheetName).toBe('Notes');
+ expect(data[2].pageContent).toBe('Owner: Alice\nSigned: 2026-10-07');
+ });
+
+ it('normalizes cell values and skips empty sheets and rows', async () => {
+ const workbook = new Workbook();
+ workbook.addWorksheet('Empty');
+ const sheet = workbook.addWorksheet('Values');
+ sheet.addRow(['Rich text', 'Formula', 'Link', 'Date', 'Number', 'Boolean', 'Error', '']);
+ sheet.addRow([
+ { richText: [{ text: 'Hello ' }, { text: 'world' }] },
+ { formula: '1+1', result: 2 },
+ { text: 'Website', hyperlink: 'https://example.com' },
+ new Date('2026-10-07T00:00:00Z'),
+ 0,
+ false,
+ { error: '#DIV/0!' },
+ 'Extra',
+ ]);
+ sheet.addRow([' ']);
+ const chunks = await ExcelLoader(new Blob([Buffer.from(await workbook.xlsx.writeBuffer())]));
+
+ expect(chunks).toHaveLength(1);
+ expect(chunks[0].pageContent).toBe(
+ 'Rich text: Hello world\nFormula: 2\nLink: Website\nDate: 2026-10-07\nNumber: 0\nBoolean: false\nError: #DIV/0!\ncolumn 8: Extra',
+ );
+ });
+
+ it('uses populated columns without querying the worksheet width', async () => {
+ const workbook = new Workbook();
+ const sheet = workbook.addWorksheet('Sparse');
+ sheet.getCell('A1').value = 'Name';
+ sheet.getCell('XFD1').value = 'Amount';
+ sheet.getCell('A2').value = 'Alice';
+ sheet.getCell('XFD2').value = 42;
+ sheet.getCell('XFC3').value = 'No header';
+ const bytes = Buffer.from(await workbook.xlsx.writeBuffer());
+ const columnCount = vi.spyOn(Object.getPrototypeOf(sheet), 'columnCount', 'get');
+
+ try {
+ const chunks = await ExcelLoader(new Blob([bytes]));
+
+ expect(chunks.map((chunk) => chunk.pageContent)).toEqual([
+ 'Name: Alice\nAmount: 42',
+ 'column 16383: No header',
+ ]);
+ expect(chunks[1].metadata.row).toBe(3);
+ expect(columnCount).not.toHaveBeenCalled();
+ } finally {
+ columnCount.mockRestore();
+ }
+ });
+
+ it('uses the first non-empty row as headers and preserves physical row numbers', async () => {
+ const workbook = new Workbook();
+ const sheet = workbook.addWorksheet('Leading blanks');
+ sheet.getCell('B2').value = 'Plan';
+ sheet.getCell('D2').value = 'Price';
+ sheet.getCell('B4').value = 'Pro';
+ sheet.getCell('D4').value = 20;
+
+ const chunks = await ExcelLoader(new Blob([Buffer.from(await workbook.xlsx.writeBuffer())]));
+
+ expect(chunks).toHaveLength(1);
+ expect(chunks[0].pageContent).toBe('Plan: Pro\nPrice: 20');
+ expect(chunks[0].metadata).toMatchObject({ row: 4, sheetName: 'Leading blanks' });
+ });
+
+ it('reports malformed workbooks through the chunking error boundary', async () => {
+ await expect(
+ new ChunkingLoader().partitionContent('broken.xlsx', new TextEncoder().encode('invalid')),
+ ).rejects.toMatchObject({ name: 'DocumentLoaderError' });
+ });
+
+ /**
+ * Regression: `.xlsx` used to fall through `getType` and the whole file failed
+ * with `Unsupported file type [undefined]`, so nothing was ever indexed.
+ */
+ it('should be reachable through ChunkingLoader for .xlsx', async () => {
+ const chunks = await new ChunkingLoader().partitionContent(
+ 'pricing.xlsx',
+ await buildWorkbook(),
+ );
+
+ expect(chunks).toHaveLength(3);
+ expect(chunks[0].pageContent).toBe('Plan: Basic\nDiscount: 10%');
+ });
+});
diff --git a/src/libs/document-loaders/loaders/excel/index.ts b/src/libs/document-loaders/loaders/excel/index.ts
new file mode 100644
index 00000000000..cd2fb426ef5
--- /dev/null
+++ b/src/libs/document-loaders/loaders/excel/index.ts
@@ -0,0 +1,66 @@
+import type { CellValue } from 'exceljs';
+
+import type { DocumentChunk } from '../../types';
+
+const cellToText = (value: CellValue): string => {
+ if (value === null || value === undefined) return '';
+ if (value instanceof Date) return value.toISOString().slice(0, 10);
+
+ if (typeof value === 'object') {
+ if ('richText' in value) return value.richText.map((run) => run.text).join('');
+ if ('formula' in value || 'sharedFormula' in value) return cellToText(value.result);
+ if ('text' in value) return value.text;
+ if ('error' in value) return value.error;
+
+ return '';
+ }
+
+ return String(value);
+};
+
+/**
+ * Chunks a `.xlsx` workbook into one document per data row, reusing the sheet's
+ * first non-empty row as the column names — the same `header: value` shape `CsVLoader`
+ * emits, so tabular data reaches the index identically whichever format it was
+ * uploaded in.
+ */
+export const ExcelLoader = async (fileBlob: Blob): Promise<DocumentChunk[]> => {
+ const { Workbook } = await import('exceljs');
+
+ const workbook = new Workbook();
+ await workbook.xlsx.load(await fileBlob.arrayBuffer());
+
+ return workbook.worksheets.flatMap((sheet) => {
+ const chunks: DocumentChunk[] = [];
+ const headers = new Map<number, string>();
+ let hasHeader = false;
+
+ sheet.eachRow({ includeEmpty: false }, (row, rowNumber) => {
+ if (!hasHeader) {
+ row.eachCell({ includeEmpty: false }, (cell, column) => {
+ headers.set(column, cellToText(cell.value));
+ });
+ hasHeader = true;
+ return;
+ }
+
+ const lines: string[] = [];
+ row.eachCell({ includeEmpty: false }, (cell, column) => {
+ const value = cellToText(cell.value);
+ if (value.trim() === '') return;
+
+ lines.push(`${headers.get(column) || `column ${column}`}: ${value}`);
+ });
+ const content = lines.join('\n');
+
+ if (!content) return;
+
+ chunks.push({
+ metadata: { row: rowNumber, sheetName: sheet.name, source: 'blob' },
+ pageContent: content,
+ });
+ });
+
+ return chunks;
+ });
+};
diff --git a/src/libs/document-loaders/loaders/index.ts b/src/libs/document-loaders/loaders/index.ts
index e5b8b803e83..ca1a9090f99 100644
--- a/src/libs/document-loaders/loaders/index.ts
+++ b/src/libs/document-loaders/loaders/index.ts
@@ -6,6 +6,7 @@ import { CodeLoader } from './code';
import { CsVLoader } from './csv';
import { DocxLoader } from './docx';
import { EPubLoader } from './epub';
+import { ExcelLoader } from './excel';
import { LatexLoader } from './latex';
import { MarkdownLoader } from './markdown';
import { PdfLoader } from './pdf';
@@ -53,6 +54,10 @@ export class ChunkingLoader {
return await DocxLoader(fileBlob);
}
+ case 'excel': {
+ return await ExcelLoader(fileBlob);
+ }
+
case 'text': {
return await TextLoader(txt);
}
diff --git a/src/libs/document-loaders/types.ts b/src/libs/document-loaders/types.ts
index a8e379785ca..bb942a50702 100644
--- a/src/libs/document-loaders/types.ts
+++ b/src/libs/document-loaders/types.ts
@@ -13,5 +13,6 @@ export type FileLoaderType =
| 'text'
| 'latex'
| 'csv'
+ | 'excel'
| 'epub'
| 'ipynb';