New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
sheet_to_json skips empty rows #139
Comments
@tamlyn excellent catch! Fortunately this is an easy fix: in the loop, the empty check should also consider the header. https://github.com/SheetJS/js-xlsx/blob/master/bits/90_utils.js#L119 can be rewritten as:
This is important enough to merit a new push later today with the fix. |
- allow for naked streams (pre-BIFF5, CFB optional) - support pre-BIFF5 strings and widths - removed nested functions in parse_xlscfb, added more record types - README improvements (paralleling js-xlsx) - fake "Sheet1" name if sheet name is missing (fixes #57, h/t @egon12) - demo only processes one file (addresses #52) - updated CFB (bits/18_cfb.js pulled from js-cfb) - cellStyles option (only supports patternType) - sheet_to_json __rowNum__ now non-enumerable (h/t @SystemParadox) - strip sourceMappingURL references from minified scripts (h/t @vinin3) - sheet_to_json header === 1 now includes empty rows (h/t @tamlyn) Related: SheetJS/sheetjs#139 SheetJS/sheetjs#142 SheetJS/sheetjs#51 SheetJS/j#8
sheet_to_json still not include empty cells |
Hi, first of all thx for this awesome tool. Now then, in the version bump 0.7.2 only includes the fix for xls empty cells, but im using an xlsx and still have problems with the empty cells. |
Also experiencing the same issue as Kevzz. Any updates? |
Can also confirm I'm having this issue. Empty cells in |
Same issue here |
eagerly awaiting for an update here :) |
+1 require support for empty cell. |
Hmmm, too bad, thought this would save my day :( |
Yeah. For me, I go thru the each row and each header to check for undefined values and put '' for empty field. workbook.SheetNames.forEach(function (sheetName) {
// Get headers.
var headers = [];
var sheet = workbook.Sheets[sheetName];
var range = XLSX.utils.decode_range(sheet['!ref']);
var C, R = range.s.r;
/* start in the first row */
/* walk every column in the range */
for (C = range.s.c; C <= range.e.c; ++C) {
var cell = sheet[XLSX.utils.encode_cell({c: C, r: R})];
/* find the cell in the first row */
var hdr = "UNKNOWN " + C; // <-- replace with your desired default
if (cell && cell.t) {
hdr = XLSX.utils.format_cell(cell);
}
headers.push(hdr);
}
// For each sheets, convert to json.
var roa = X.utils.sheet_to_json(workbook.Sheets[sheetName]);
if (roa.length > 0) {
roa.forEach(function (row) {
// Set empty cell to ''.
headers.forEach(function (hd) {
if (row[hd] == undefined) {
row[hd] = '';
}
});
});
}
}); |
+1 the key should be included in the json objects with an empty string or (Awesome tool nontheless) |
@SheetJSDev Hi, any update when this will be fixed? I am still seeing this issue. BTW great library! |
@abartolo Go to xlsx.js file, find
In summary this part has to be changed:
|
@mjza that has no change for me. Still the empty fields are inexistant and imposible to detect. |
+1 |
@mjza Thanks , It is work |
Any updates on this matter? |
I think this was resolved a long time ago, in version 0.8.0 the fix from #139 (comment) was applied. I just tested a file and see blank arrays generated when the option As far as I can tell the crux of the matter (including empty rows) has been resolved. If anyone can share a sample file with expected output I'll reopen the issue. |
sheet_to_json() with header option true yields empty column headers perfectly, But there is a bug and it won't echo null for last empty column data. In attached screenshot, there are 11 header items. 2nd column is empty and it's coming null as expected. But it's not echoing null for 11th column which is last one. |
@vins13pattar do you have a file you can share? It should not be generating null in the first place -- those should be array holes (undefined) I made a sample file to check what's going on: t139.xlsx And this is what I see in node: > XLSX.version
'0.9.5'
> XLSX.utils.sheet_to_json(XLSX.readFile('t139.xlsx').Sheets.Sheet1, {header:1})
[ [ 'id',
'id-job',
'type',
'name',
'address',
'latitude',
'longitude',
'service-duration',
'start-time',
'end-time',
'quantity',
'required-skills' ],
[ 'Stop1', , , , , , , , , , '206' ] ] |
@SheetJSDev Thank you for the reply.
Array holes are ok. But how to represent array hole after the last column?
I faced this issue because I was parsing content without any header columns. |
I had a similar issue but was able to solve it like this by removing empty object in from an array , hope this helps someone. Not an ideal solution but gets the job done
|
Are you by any chance roundtripping through JSON by stringifying in one place and parsing elsewhere? That would plug > var arr = [1,,,,,2]
undefined
> arr
[ 1, <4 empty items>, 2 ]
> JSON.stringify(arr)
'[1,null,null,null,null,2]'
> JSON.parse(JSON.stringify(arr))
[ 1, null, null, null, null, 2 ] |
When I implement this code in my component.ts the worksheet contains null values plus the sheet_to_json ignores the null values and strips the entire column being able to set null and continue. Here's the snippet of my code, I'm using the most recent version of xlsx 0.12.13, it also follows a sample xlsx file I'm importing. I'm using angular 5
} file = https://drive.google.com/file/d/1b9ql8LCHiDwRYNLyt_BjkCh9w908TbUT/view?usp=sharing |
@eduardombraga unable to download that file (you have to enable sharing). Can you test with the |
@SheetJSDev See if you can see now https://drive.google.com/file/d/1b9ql8LCHiDwRYNLyt_BjkCh9w908TbUT/view?usp=sharing |
To test in the web browser, go to https://oss.sheetjs.com/js-xlsx/ and drag-drop the The original worksheet had 13 columns, consistent with the actual data reported in chrome |
Correct but I can not return these null values? they are stuck and I lose the data I need to return the column with the values even though they are null? or is it not possible? because I need these null values so I save in the database as nulls or in the future make check if the valroes are null I send as 0 or any other whatever this being done at the moment of reading and writing the object ... |
I am using angular 6 and it will work on angular 5... This will work on xlsx, Only one I used it on. defval does the magic here, 'null' is what I want in place defval can be any value you set. |
Uall, worked as I expected. Thank you very much for the help. 👍 |
@ryankall Hello! When I have null fields plus the same ones are of type string I can write as null and those that are of type binary send another value if it is null? |
It was also not working for me until I added the 'defval' param: |
Hi there, try this once. const xlsx = require('xlsx'); const workbook = xlsx.read(file.buffer, { type: 'buffer' }); |
In my case I needed the header of a xlsx which could have empty rows above it. The range: 0 is the essential attribute here. |
thanks, Nahuelgrc :) 👍 |
Setting {sheetStubs: true} allowed me to access all the json objects including the ones with empty values, without adding it my array would just end at the first empty element but at least now it just skips the empty cells and continues with the rest of the array. I need to update an 'empty' element now so I need to be able to fetch the empty cell 'values'. Another important part so far was using the !null in my filter function as referenced here github.com//issues/139#issuecomment-390754486. My array is still returning with no values for the elements after the empty value but I hope that helps someone. |
Fantastic, it helped a lot. |
its not work |
Issue is still open ... |
Issue still not resolved. Very frustrating |
initial[name] = XLSX.utils.sheet_to_json(sheet,{defval:""}) {defval:""}This will work it automatically marks the empty cells as empty strings. |
This works! Thanks. |
When calling
xlsx.utils.sheet_to_json(sheet, {header: 1})
I would expect to receive an array of arrays including empty rows. However the function explicitly skips out empty rows which makes it impossible to select a particular row in the results. It would be good to make this behaviour configurable.As a workaround, I have used
sheet_to_csv
, which doesn't skip rows, followed by csv-parse.The text was updated successfully, but these errors were encountered: