exceljs / exceljs

Excel Workbook Manager
MIT License
13.09k stars 1.67k forks source link

[Q] Issue with Excel file parsing inconsistency in Node.js using ExcelJS #2718

Open Yasha-ops opened 2 months ago

Yasha-ops commented 2 months ago

Question / Help

I'm encountering an issue while parsing an Excel file using ExcelJS in a Node.js environment. The Excel file contains cells representing dates, but it seems that some of these cells are being treated as strings, while others are correctly converted to actual date objects. Despite trying various approaches, I haven't been able to resolve this issue.

Lib version: 4.4.0

Steps To Reproduce

Output Excel

export default async (req: Request, res: Response) => {
  const workbook = new ExcelJS.Workbook();

  if (!req.files?.length) {
    res.send("Files not found");
    return;
  }

  try {
    // @ts-ignore
    await workbook.xlsx.load(req.files[0].buffer);

    const worksheet = workbook.getWorksheet(1);

    if (worksheet?.actualColumnCount != EXCEL_COLUMN_NUMBER) {
      return res.status(400).send({
        user_message:
          "Not correct number of column",
      });
    }
    worksheet.spliceRows(1, 1);

    worksheet?.eachRow(async (row, rowIndex) => {
      const rowCellValue = row.values as CellValue[]
      console.log("Birthdate :", rowCellValue[ExcelColumns.BirthDate];
    });
  } catch (error) {
    console.error("Error processing Excel file:", error);
    res.status(500).send("Error processing Excel file");
  }
}

The expected behaviour:

I desire to ensure that all cells in the Excel file have a unique type (either text or date) so that I can parse them accurately. While I acknowledge that checking the type is a viable solution, I am reluctant to accept it as the sole approach.

Possible solution (optional, but very helpful):

Maybe checking the type ?