4

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 ));
}
Padma
  • 41
  • 1
  • 2

3 Answers3

6

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 ("")
Marko Sikman
  • 345
  • 3
  • 8
1

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

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

António Ribeiro
  • 4,129
  • 5
  • 32
  • 49
burakkp
  • 53
  • 9
0

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}) )

vinays84
  • 394
  • 4
  • 14