Fixing the error when importing a large Excel file

This commit is contained in:
2026-10-03 15:01:01 +03:30
parent f8313c0ea5
commit 2e315c9cfd
15 changed files with 503 additions and 86 deletions
+17
View File
@@ -1,5 +1,6 @@
import { BadRequestException } from '@nestjs/common';
import ExcelJS from 'exceljs';
import { EXCEL_IMPORT_LARGE_THRESHOLD } from './excel-import-session';
export function cellStr(value: ExcelJS.CellValue | undefined): string {
try {
@@ -229,6 +230,8 @@ export type ExcelPreviewResult = {
fields: ExcelFieldOption[];
suggested: Record<string, string>;
needsMapping: boolean;
rowCount: number;
largeFile?: boolean;
};
export function excelFieldsFromColumns(
@@ -315,11 +318,25 @@ export async function previewExcel(
const mapped = new Set(Object.values(suggested));
const missingRequired = required.some((key) => !mapped.has(key));
const unrecognized = columns.some((col) => !suggested[col.header]);
let rowCount = 0;
sheet.eachRow((row, rowNumber) => {
if (rowNumber === 1) return;
let any = false;
row.eachCell((cell) => {
if (any) return;
if (cellStr(cell.value)) any = true;
});
if (any) rowCount += 1;
});
return {
columns,
fields,
suggested,
needsMapping: unrecognized || missingRequired || !mapped.size,
rowCount,
largeFile: rowCount >= EXCEL_IMPORT_LARGE_THRESHOLD,
};
}