trevordixon / excel.js

Native node.js Excel file parser. Only supports xlsx for now.
MIT License
267 stars 85 forks source link

Cannot set property '0' of undefined #40

Closed rashthedude closed 8 years ago

rashthedude commented 8 years ago

Whilst parsing an xlsx file this is the error being thrown:

TypeError: Cannot set property '0' of undefined at /Users/rashthedude/work/lib/price_email_parser/papi/nodemodules/excel/excelParser.js:148:56 at Array.forEach (native) at Function..each._.forEach (/Users/rashthedude/work/lib/price_email_parser/papi/node_modules/excel/node_modules/underscore/underscore.js:76:11) at extractData (/Users/rashthedude/work/lib/price_email_parser/papi/node_modules/excel/excelParser.js:137:4) at /Users/rashthedude/work/lib/price_email_parser/papi/node_modules/excel/excelParser.js:156:12 at Object._onImmediate (/Users/rashthedude/work/lib/price_email_parser/papi/node_modules/node-promise/promise.js:164:27) at processImmediate as _immediateCallback

lucdetellis commented 8 years ago

Same issue:

.../node_modules/excel/node_modules/node-promise/promise.js:204
          throw error;
                ^
TypeError: Cannot set property '0' of undefined
    at .../node_modules/excel/excelParser.js:148:56
    at Array.forEach (native)
    at Function._.each._.forEach (.../node_modules/excel/node_modules/underscore/underscore.js:76:11)
    at extractData (.../node_modules/excel/excelParser.js:137:4)
    at .../node_modules/excel/excelParser.js:156:12
    at Object._onImmediate (.../node_modules/excel/node_modules/node-promise/promise.js:164:27)
    at processImmediate [as _immediateCallback] (timers.js:363:15)
Program node app.js exited with code 8

Mac OS X 10.11.3 Node.js 0.10.42

lucdetellis commented 8 years ago

I found a solution! In the file node_modules/excel/excelParser.js:

Change line 148: data[cell.row - d[0].row][cell.column - d[0].column] = value; to this: if (data[cell.row - d[0].row]) data[cell.row - d[0].row][cell.column - d[0].column] = value;


The problem here is that some Excel files include headers or titles above the table, which are at the position cell.row - d[0].row === -1. Since data[-1] is undefined, an error is thrown. The solution to this is to check if data[cell.row - d[0].row] exists before assigning that cell a value. In my case, the value of value at [-1, 0] was Table 1, and in my Excel document there was a header/title with the text "Table 1".

rashthedude commented 8 years ago

@lucdetellis thanks man. As a software dev I should maybe once in a while look into solving this type of issues myself. Appreciated.