question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

sheet_to_json doesn't output anything for a blank cell

See original GitHub issue

It seems like using sheet_to_json doesn’t handle blank cells very well. I want to keep them in my script (I’m parsing an xlsx file and outputting a csv), but a blank cell outputs an array like:

[ 'foo',
  'bar',
  ,      // the blank value
  'another value' ]

It’d be nice if you got a blank string, since this library seems to recognize the blank value. Would this be possible?

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:18 (1 by maintainers)

github_iconTop GitHub Comments

59reactions
yang5664commented, Jul 7, 2018

you don’t need to change anythings, just in function pass opt defval:‘’ that’s it.

XLSX.utils.sheet_to_json(workbook.Sheets[sheet],{defval:""})
2reactions
SheetJSDevcommented, Jan 11, 2015

The code as it stands now won’t generate an empty string. The simplest way is to just add a post-processing loop to your code:

for(var i = 0; i != json.length; ++i) for(var j = 0; j != json[i].length; ++j) if(typeof json[i][j] === 'undefined') json[i][j] = "";

As for what it “should” be, why not the number 0? an empty string would be weird in a case like:

[
  [1, 2, 3],
  [4, 5, 6],
  [7, ? ,9]
]

I’d be amenable to adding an option (maybe something called “default” in the options object?), and the changes are relatively simple: https://github.com/SheetJS/js-xlsx/blob/master/xlsx.js#L5293

            val = sheet[cols[C] + rr];
            if(val === undefined || val.t === undefined) continue;

In the bottom half of sheet_to_json, we walk in row-major order across the worksheet. The first line attempts to find if the sheet has a cell at the prescribed address. If the cell doesn’t exist or if its type is not set, then we skip the cell. Instead of just continuing, that second line should set the cell to the empty string:

            if(val === undefined || val.t === undefined) { row[hdr[C]] = ""; continue; }
Read more comments on GitHub >

github_iconTop Results From Across the Web

javascript - sheetJs - no output for empty cell - Stack Overflow
data is just a const which holds conversion from sheet to json ... using for conversion and manipulation; defval presents empty cell values....
Read more >
Utility Functions - SheetJS Community Edition
aoa_to_sheet takes an array of arrays of JS values and returns a worksheet resembling the input data. Values are interpreted as follows: Numbers,...
Read more >
How to remove blank cells in Excel - Ablebits
Empty cells are removed. Tips: If something has gone awry, don't panic and immediately press Ctrl + Z to get your data ...
Read more >
xlsx.utils.sheet_to_json | The AI Search Engine You Control
I am trying to convert an Excel worksheet into a JSON object using the built in node module XLSX' function XLSX.utils.sheet_to_json() However, ...
Read more >
How to Determine IF a Cell is Blank or Not Blank in Excel
Want to learn more about the IF function? This post will give you an overview of how to use an IF function when...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found