Reputation: 553
I'm currently working with an excel document, but I need to get only the table headers name. So far my code looks like this
if(file){
let fileReader = new FileReader();
fileReader.readAsBinaryString(file);
fileReader.onload = (event) =>{
let data = event.target?.result;
let workbook = XLSX.read(data,{type:"binary"});
console.log('WORKBOOK',workbook)
workbook.SheetNames.forEach(sheet => {
let rowObject = XLSX.utils.sheet_to_json(workbook.Sheets[sheet])
console.log('RowObject',rowObject)
setFile(rowObject)
})
}
}
Inside the Sheets object, I can see the cells, including the header name, how can I get only the header row?
Upvotes: 1
Views: 3715
Reputation: 61
You can convert ths sheet to sheet_to_json helper and you can skip the firt row (the headers) adding header:1, additionally if you set defval:"" then you will raplacing default values with blanck text, you can replace it with undefined or anyother value that you want.
When you apply this method with this parameters you will obtain an array of arrays, the first array in this array of arrays is the header.
const rowObject = XLSX.utils.sheet_to_json(workbook.Sheets[sheet],
{header: 1,
defval: ""})
const headers = rowObject[0];
Upvotes: 0
Reputation: 11
You can get headers in the following way.
let rowObject = XLSX.utils.sheet_to_json(workbook.Sheets[sheet],
{header: 1,
defval: ""})
Upvotes: 1