sheet_to_json doesn't output anything for a blank cell #159
Labels
No Label
DBF
Dates
Defined Names
Features
Formula
HTML
Images
Infrastructure
Integration
International
ODS
Operations
Performance
PivotTables
Pro
Protection
Read Bug
SSF
SYLK
Style
Write Bug
good first issue
No Milestone
No Assignees
1 Participants
Notifications
Due Date
No due date set.
Dependencies
No dependencies set.
Reference: sheetjs/sheetjs#159
Loading…
Reference in New Issue
No description provided.
Delete Branch "%!s(<nil>)"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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:
It'd be nice if you got a blank string, since this library seems to recognize the blank value. Would this be possible?
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:
As for what it "should" be, why not the number 0? an empty string would be weird in a case like:
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
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:
Wow, really appreciate the lightning quick response! Your for loop snippet worked perfectly - I'm probably not experienced enough to comment on the intricacies of what it "should" be, but thank you!
Changing line 11549 from
if(val === undefined || val.t === undefined) continue;
to
if(val === undefined || val.t === undefined) { row[hdr[C]] = ""; continue; }
fixed the issue for me.
@SheetJSDev Thank you for the suggestion. It fixed the issue for me too.
It would be really nice to turn this into an option.
Thanks a lot.
Great work! You saved my day. Thanks alot :)
Me too having same problem.
How to rectify that to read the empty cell, like
[{custName : "Ben", custCity:"city1", phno:123}, {custName : "Ken", custCity:"city2", phno:}]
Solution 1 .Condition "if(h===undefined)continue;" in "xlsx.core.min.js" comment it out.
or do it properly...
Solution 2 . By passing Condition extra param while running this XLSX.utils.sheet_to_json(wb.Sheets[name] , {blankCell : false}). add a condition on line no. 19150 "if(defval === undefined && blankCell) continue;" in file xlsx.js etc..
@hmnshmshr I did this but no change?? can you please ellaborate?/
you don't need to change anythings, just in function pass opt defval:'' that's it.
please try
JSON.parse(JSON.stringify(XLSX.utils.sheet_to_json(workbook.Sheets[sheet]))
@yang5664 That worked like a charm. Thank you
@yang5664. Thank you so much. This worked for me!
thank you @yang5664 . It helped me to solve the problem in flick !
we apply condition but it doesnot work.
please if you have another solution guide it
thanks.
XLSX.utils.sheet_to_json(workbook.Sheets[sheet],{defval:""})
we can use this but cannot work please if you have another solution then guide me
thanks
we can change the line but doenot work
2021, this still works flawlessly. Thank you so very much.
Works if you are using the latest version of js from https://cdn.sheetjs.com/ (Read "how to use")