sheetjs icon indicating copy to clipboard operation
sheetjs copied to clipboard

Angular2 - ERROR TypeError: Cannot read property '0' of undefined

Open Radhakanta opened this issue 8 years ago • 7 comments

When tried Angular2 Demo, I got below issue: core.es5.js:1020 ERROR TypeError: Cannot read property '0' of undefined at parse_xlml_xml (xlsx.js:14380) at parse_xlml (xlsx.js:14763) at Object.readSync [as read] (xlsx.js:18888) at FileReader.reader.onload (sheetjs.component.ts:39) at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invoke (zone.js:388) at Object.onInvoke (core.es5.js:3890) at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invoke (zone.js:387) at Zone.webpackJsonp.../../../../zone.js/dist/zone.js.Zone.runGuarded (zone.js:151) at FileReader. (zone.js:129)

Radhakanta avatar Jan 04 '18 02:01 Radhakanta

Can you share the file you tested with?

SheetJSDev avatar Jan 04 '18 02:01 SheetJSDev

I use the demo source from Git repository : https://github.com/SheetJS/js-xlsx/tree/a7d37797240c38ee0d84132b26733cb4955b687b/demos/angular2

Radhakanta avatar Jan 04 '18 02:01 Radhakanta

The error stack shows that you tried to read a file (FileReader.reader.onload) -- test if that file is a valid Excel file

SheetJSDev avatar Jan 04 '18 03:01 SheetJSDev

I used the same excel sheet provided in demo : https://obamawhitehouse.archives.gov/sites/default/files/omb/budget/fy2014/assets/receipts.xls

Radhakanta avatar Jan 04 '18 03:01 Radhakanta

That error shows up on files that are suspected to be XML or HTML files. The linked file is an actual BIFF8 XLS file. Can you look at the contents of the file and see if you have the original data?

This is what I see when running ng serve in the demo directory and drag-dropping the receipts file:

SheetJSDev avatar Jan 04 '18 03:01 SheetJSDev

I checked the downloaded excel file and it has content.

Radhakanta avatar Jan 04 '18 04:01 Radhakanta

I was having this issue with Nodejs, trying to read the xlsx file from an axios http GET. Turns out if I changed the GET request responseType to 'arraybuffer' and then read it like so: const workbook = xlsx.read(file.data, { type: 'array' }); it worked. Hope this helps anyone else, might be applicable to set your angular httpClient request options.

benjamin-wilson avatar Feb 02 '19 23:02 benjamin-wilson