Padma
Padma

Reputation: 41

sheetJs - no output for empty cell

I am using sheetjs. The empty cell is not getting populated in the json object including the header. I need that information also if a column is not entered. Here is my code:

workbook.SheetNames.forEach((sheetName) => {
    let XL_row_object = XLSX.utils.sheet_to_json(workbook.Sheets[sheetName], {defval:""});
    console.log( JSON.stringify(XL_row_object ));
}

Upvotes: 4

Views: 6441

Answers (3)

vinays84
vinays84

Reputation: 422

To extend the sheetStubs answer, XLSX.utils.sheet_to_json will ignore stubs so you should first convert them with a simple function:

function removeStubs(wb) {
  Object.values(wb.Sheets).forEach(ws => {
    Object.values(ws).filter(v => v.t === 'z').forEach(v => Object.assign(v,{t:'s',v:''}));
  });
  return wb;
}

then call removeStubs( XLSX.readFile('file', {sheetStubs:true}) )

Upvotes: 0

Marko Sikman
Marko Sikman

Reputation: 405

You can solved it with this part, at the begining, when you are including your SheetJS:

**const data = xlsx.utils.sheet_to_json((ws),{defval:""});**

  • data is just a const which holds conversion from sheet to json
  • ws is the name of our sheets which we are using for conversion and manipulation
  • defval presents empty cell values. In our case it is an empty string ("")

Upvotes: 7

burakkp
burakkp

Reputation: 53

I was having the same issue and I solved this problem with this:

XLSX.readFile('file', {sheetStubs:true})

Upvotes: 1

Related Questions