Sheetjs: How to read second row as header?

Created on 18 Oct 2016  路  2Comments  路  Source: SheetJS/sheetjs

I need to skip first row of .xlsx file and have to consider second row as a header. Is there any utlity in js-xlsx for this?

Most helpful comment

The sheet_to_json option takes a range argument. If you set the range to 1 it will automatically skip the first row. Here's a sample XLSX file where the second row is the real "header"

header_row.xlsx

title
a,b,c
1,2,3
4,5,6
7,8,9

Here's a session in node, but the same thing works in browser too:

> var XLSX = require('xlsx')
undefined
> var wb = XLSX.readFile('header_row.xlsx')
undefined
> XLSX.utils.sheet_to_json(wb.Sheets.Sheet1) // this uses the first row as the header
[ { title: 'a', undefined: 'c' },
  { title: '1', undefined: '3' },
  { title: '4', undefined: '6' },
  { title: '7', undefined: '9' } ]
> XLSX.utils.sheet_to_json(wb.Sheets.Sheet1, {range:1}) // this uses the second row
[ { a: '1', b: '2', c: '3' },
  { a: '4', b: '5', c: '6' },
  { a: '7', b: '8', c: '9' } ]

All 2 comments

The sheet_to_json option takes a range argument. If you set the range to 1 it will automatically skip the first row. Here's a sample XLSX file where the second row is the real "header"

header_row.xlsx

title
a,b,c
1,2,3
4,5,6
7,8,9

Here's a session in node, but the same thing works in browser too:

> var XLSX = require('xlsx')
undefined
> var wb = XLSX.readFile('header_row.xlsx')
undefined
> XLSX.utils.sheet_to_json(wb.Sheets.Sheet1) // this uses the first row as the header
[ { title: 'a', undefined: 'c' },
  { title: '1', undefined: '3' },
  { title: '4', undefined: '6' },
  { title: '7', undefined: '9' } ]
> XLSX.utils.sheet_to_json(wb.Sheets.Sheet1, {range:1}) // this uses the second row
[ { a: '1', b: '2', c: '3' },
  { a: '4', b: '5', c: '6' },
  { a: '7', b: '8', c: '9' } ]

slove my problem

Was this page helpful?
0 / 5 - 0 ratings

Related issues

happy0088 picture happy0088  路  3Comments

magtuan picture magtuan  路  3Comments

sangpuion picture sangpuion  路  3Comments

eyalcohen4 picture eyalcohen4  路  3Comments

sudhakar-sekar picture sudhakar-sekar  路  3Comments