Fixing Excel login error
This commit is contained in:
@@ -2,23 +2,37 @@ import { BadRequestException } from '@nestjs/common';
|
||||
import ExcelJS from 'exceljs';
|
||||
|
||||
export function cellStr(value: ExcelJS.CellValue | undefined): string {
|
||||
if (value == null) return '';
|
||||
if (typeof value === 'string' || typeof value === 'number') {
|
||||
return String(value).trim();
|
||||
try {
|
||||
if (value == null) return '';
|
||||
if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
|
||||
return String(value).trim();
|
||||
}
|
||||
if (value instanceof Date) {
|
||||
return Number.isNaN(value.getTime()) ? '' : value.toISOString();
|
||||
}
|
||||
if (typeof value === 'object' && 'richText' in value && Array.isArray(value.richText)) {
|
||||
return value.richText.map((part) => part?.text ?? '').join('').trim();
|
||||
}
|
||||
if (typeof value === 'object' && 'text' in value && typeof value.text === 'string') {
|
||||
return value.text.trim();
|
||||
}
|
||||
if (typeof value === 'object' && 'result' in value && value.result != null) {
|
||||
const result = value.result;
|
||||
if (typeof result === 'string' || typeof result === 'number' || typeof result === 'boolean') {
|
||||
return String(result).trim();
|
||||
}
|
||||
if (result instanceof Date) {
|
||||
return Number.isNaN(result.getTime()) ? '' : result.toISOString();
|
||||
}
|
||||
return '';
|
||||
}
|
||||
if (typeof value === 'object' && 'error' in value && value.error != null) {
|
||||
return String(value.error).trim();
|
||||
}
|
||||
return '';
|
||||
} catch {
|
||||
return '';
|
||||
}
|
||||
if (typeof value === 'object' && 'richText' in value && Array.isArray(value.richText)) {
|
||||
return value.richText.map((part) => part.text).join('').trim();
|
||||
}
|
||||
if (typeof value === 'object' && 'text' in value && typeof value.text === 'string') {
|
||||
return value.text.trim();
|
||||
}
|
||||
if (typeof value === 'object' && 'result' in value && value.result != null) {
|
||||
return String(value.result).trim();
|
||||
}
|
||||
if (value instanceof Date) {
|
||||
return value.toISOString();
|
||||
}
|
||||
return String(value).trim();
|
||||
}
|
||||
|
||||
export function normalizeHeader(raw: string) {
|
||||
@@ -182,7 +196,17 @@ export async function workbookFromUpload(file: Express.Multer.File | undefined)
|
||||
throw new BadRequestException('فایل اکسل انتخاب نشده است');
|
||||
}
|
||||
const wb = new ExcelJS.Workbook();
|
||||
await wb.xlsx.load(Uint8Array.from(file.buffer) as unknown as ExcelJS.Buffer);
|
||||
try {
|
||||
// Pass Node Buffer directly — Uint8Array copies can break ExcelJS on some runtimes.
|
||||
await wb.xlsx.load(file.buffer as unknown as ExcelJS.Buffer);
|
||||
} catch (err) {
|
||||
const detail = err instanceof Error ? err.message : '';
|
||||
throw new BadRequestException(
|
||||
detail.includes('central directory') || detail.includes('zip')
|
||||
? 'فایل اکسل نامعتبر است. فقط فرمت .xlsx پشتیبانی میشود (نه .xls یا csv).'
|
||||
: 'خواندن فایل اکسل ناموفق بود. فایل را با فرمت .xlsx ذخیره کنید و دوباره تلاش کنید.',
|
||||
);
|
||||
}
|
||||
const sheet = wb.worksheets[0];
|
||||
if (!sheet) throw new BadRequestException('شیت اکسل پیدا نشد');
|
||||
return sheet;
|
||||
|
||||
Reference in New Issue
Block a user