exceljs: TypeError: Cannot read property 'sheets' of undefined

I got this error when I tested the parsing process from an excel file. Hope someone can help me with this

TypeError: Cannot read property 'sheets' of undefined
    at _callee10$ (/Users/tvi/Documents/BCM projects/cvd-genomics/node_modules/exceljs/dist/es5/xlsx/xlsx.js:553:43)
    at tryCatch (/Users/tvi/Documents/BCM projects/cvd-genomics/node_modules/regenerator-runtime/runtime.js:65:40)
    at Generator.invoke [as _invoke] (/Users/tvi/Documents/BCM projects/cvd-genomics/node_modules/regenerator-runtime/runtime.js:303:22)
    at Generator.prototype.(anonymous function) [as next] (/Users/tvi/Documents/BCM projects/cvd-genomics/node_modules/regenerator-runtime/runtime.js:117:21)
    at asyncGeneratorStep (/Users/tvi/Documents/BCM projects/cvd-genomics/node_modules/exceljs/dist/es5/xlsx/xlsx.js:3:103)
    at _next (/Users/tvi/Documents/BCM projects/cvd-genomics/node_modules/exceljs/dist/es5/xlsx/xlsx.js:5:194)
    at <anonymous>

Excel File: TEST.xlsx

exceljs version: 2.0.1

About this issue

  • Original URL
  • State: open
  • Created 5 years ago
  • Comments: 34 (3 by maintainers)

Most upvoted comments

Not sure of the exact root cause, but opening the file in Excel and saving it again works.

Hi Everyone,

I reproduced this issue: Failed: Cannot read property ‘sheets’ of undefined

Configuration settings:

  1. node - v: 14.16.0

  2. “exceljs”: “^4.2.1”

  3. test File: testFile.xlsx

  4. Code:

const ExcelJS = require('exceljs');
const pageComparisonXLSXFilePath = './testFile.xlsx';
const workbook = new ExcelJS.Workbook();
await workbook.xlsx.readFile(pageComparisonXLSXFilePath); 
const worksheet = workbook.getWorksheet(1);
const rowC = worksheet.rowCount;
for (const row of worksheet.getRows(1, rowC)) {
  console.log(row.values);
}

Error raises during next line execution: await workbook.xlsx.readFile(pageComparisonXLSXFilePath);

  1. Steps to reproduce: 1. Download file -> exec code -> exception: Failed: Cannot read property ‘sheets’ of undefined. but 2. If I open this file by MSExcel and then simply save this file and close it -> code executes correctly.

Could you help me?

Regards, Sergei.

I’ve fixed this as a part of this PR: https://github.com/exceljs/exceljs/pull/1570

@Privatecoder Thought that would be the case - I saw the same issue on 12 and 13, I make sure to just use 15 because streams had a few odd bugs in those versions. Even 14 had some weird issues.

@Siemienik thanks for your reply, but the error happens before we can get the worksheet. It’s more related to workbook than worksheet.

    //        place where the error happens
    //                       v
    return this.workbook.xlsx.read(sheet).then(() => {
      if (sheetNameOrIndex) this.worksheet = this.workbook.getWorksheet(sheetNameOrIndex);

Lib code that throws the error:

// exceljs/lib/xlsx/xlsx.js:269
              case 'xl/workbook.xml': {
                const workbook = await this.parseWorkbook(entry);
                model.sheets = workbook.sheets;