bigabdoul / fast-xlsx-reader

A Node package used for efficiently reading row-by-row an Excel worksheet.
MIT License
8 stars 3 forks source link

Can't use schema if header is not in first row #6

Open Zireael opened 3 years ago

Zireael commented 3 years ago

Hi, My xlsx file has header in second row instead of the top first row. I can't use schema to output rows as object with {columnName:value}.

Is there a way to either: a) set which row contains header and set reader.startRow and reader.endRow to use schema only against rows in that range b) set which row contains header and just use titles from that row as keys for other rows without creating a schema.

Trying to use

 const reader = excel.createReader({
    input: filePath,
    hasHeader: true,
  });

 console.log(reader.readMany(1, 4))

doesnt skip row 1 (as I'd expect by setting hasHeader: true) and doesn't make {key:value} output. Instead I get an array of values for each row 1 to 4. Behaviour from above point b) should work here.

Trying to use

  const reader = excel.createReader({
    input: filePath,
    hasHeader: true,
    schema: schema,
  });
reader.moveNext()
  while (reader.moveNext()) {
    console.log(reader.current);
  }

I get either

    this._header.forEach((column, index) => {
                 ^

TypeError: Cannot read property 'forEach' of null
    at FastXlsxReader._rowFromSchema (v:\test\node_modules\fast-xlsx-reader\lib\FastXlsxReader.js:307:18)
    at FastXlsxReader._readRow (v:\test\node_modules\fast-xlsx-reader\lib\FastXlsxReader.js:290:21)
    at FastXlsxReader._handleCallbackEvent (v:\test\node_modules\fast-xlsx-reader\lib\FastXlsxReader.js:208:23)
    at FastXlsxReader.<anonymous> (v:\test\node_modules\fast-xlsx-reader\lib\FastXlsxReader.js:185:14)
    at FastXlsxReader.onrecord (v:\test\node_modules\fast-xlsx-reader\lib\FastXlsxReader.js:489:36)
    at FastXlsxSheetReader.read (v:\test\node_modules\fast-xlsx-reader\lib\FastXlsxSheetReader.js:276:35)
    at FastXlsxSheetReader.readMany (v:\test\node_modules\fast-xlsx-reader\lib\FastXlsxSheetReader.js:309:34)
    at Object.readExcel (v:\test\server\excelRead.js:60:21)
    at Object.<anonymous> (v:\test\server\index.js:17:22)
    at Module._compile (node:internal/modules/cjs/loader:1108:14)

or

[
  'File exported on 08 Jan 21',
  '',
  '',
  '',
  ''
]
#ERR_SCHEMA: Invalid schema! No mapping for column "File exported on 08 Jan 21". {
  ID: 'ID',
  'Short Description': 'Short Description',
  Type: 'Type',
  Assignee: 'Assignee',
  Department: 'Department'  
}
#ERR: undefined

Behaviour from point a) should work here.

bigabdoul commented 3 years ago

@Zireael sync your local repo and retry with the new changes. Let me know if the result with your file isn't as expected.