From 02707848ad3fa7713db4d7864cb80f4616d0b360 Mon Sep 17 00:00:00 2001 From: reviewher <24845478+reviewher@users.noreply.github.com> Date: Fri, 11 Mar 2022 16:24:23 -0800 Subject: [PATCH] `skipHidden` for `sheet_to_json` [ci skip] --- .npmignore | 1 + bits/90_utils.js | 4 ++++ bits/97_node.js | 5 ++++- test.js | 24 ++++++++++++++++++++++++ test.mjs | 24 ++++++++++++++++++++++++ test.ts | 24 ++++++++++++++++++++++++ tests/core.js | 38 ++++++++++++++++++++++++++++++++++++++ types/index.d.ts | 3 +++ 8 files changed, 122 insertions(+), 1 deletion(-) diff --git a/.npmignore b/.npmignore index c28e5c2..34a5245 100644 --- a/.npmignore +++ b/.npmignore @@ -42,6 +42,7 @@ tmp .spelling .eslintignore .eslintrc +.eslintmjs .jshintrc xlsx.mini.js CONTRIBUTING.md diff --git a/bits/90_utils.js b/bits/90_utils.js index 76d7968..cac0749 100644 --- a/bits/90_utils.js +++ b/bits/90_utils.js @@ -67,7 +67,10 @@ function sheet_to_json(sheet/*:Worksheet*/, opts/*:?Sheet2JSONOpts*/) { var R = r.s.r, C = 0; var header_cnt = {}; if(dense && !sheet[R]) sheet[R] = []; + var colinfo/*:Array*/ = o.skipHidden && sheet["!cols"] || []; + var rowinfo/*:Array*/ = o.skipHidden && sheet["!rows"] || []; for(C = r.s.c; C <= r.e.c; ++C) { + if(((colinfo[C]||{}).hidden)) continue; cols[C] = encode_col(C); val = dense ? sheet[R][C] : sheet[cols[C] + rr]; switch(header) { @@ -87,6 +90,7 @@ function sheet_to_json(sheet/*:Worksheet*/, opts/*:?Sheet2JSONOpts*/) { } } for (R = r.s.r + offset; R <= r.e.r; ++R) { + if ((rowinfo[R]||{}).hidden) continue; var row = make_json_row(sheet, r, R, cols, header, hdr, dense, o); if((row.isempty === false) || (header === 1 ? o.blankrows !== false : !!o.blankrows)) out[outi++] = row.row; } diff --git a/bits/97_node.js b/bits/97_node.js index 8fc2819..fed6867 100644 --- a/bits/97_node.js +++ b/bits/97_node.js @@ -82,7 +82,10 @@ function write_json_stream(sheet/*:Worksheet*/, opts/*:?Sheet2CSVOpts*/) { var R = r.s.r, C = 0; var header_cnt = {}; if(dense && !sheet[R]) sheet[R] = []; + var colinfo/*:Array*/ = o.skipHidden && sheet["!cols"] || []; + var rowinfo/*:Array*/ = o.skipHidden && sheet["!rows"] || []; for(C = r.s.c; C <= r.e.c; ++C) { + if(((colinfo[C]||{}).hidden)) continue; cols[C] = encode_col(C); val = dense ? sheet[R][C] : sheet[cols[C] + rr]; switch(header) { @@ -104,7 +107,7 @@ function write_json_stream(sheet/*:Worksheet*/, opts/*:?Sheet2CSVOpts*/) { R = r.s.r + offset; stream._read = function() { while(R <= r.e.r) { - //if ((rowinfo[R-1]||{}).hidden) continue; + if ((rowinfo[R-1]||{}).hidden) continue; var row = make_json_row(sheet, r, R, cols, header, hdr, dense, o); ++R; if((row.isempty === false) || (header === 1 ? o.blankrows !== false : !!o.blankrows)) { diff --git a/test.js b/test.js index 713307c..940e151 100644 --- a/test.js +++ b/test.js @@ -1823,6 +1823,30 @@ describe('json output', function() { X.utils.sheet_to_json(ws, {raw:true}); X.utils.sheet_to_json(ws, {raw:true, defval: 'jimjin'}); }); + it('should handle skipHidden for rows if requested', function() { + var ws2 = X.utils.aoa_to_sheet(data), json = X.utils.sheet_to_json(ws2); + assert.equal(json[0]["1"], true); + assert.equal(json[2]["3"], "qux"); + ws2["!rows"] = [null,{hidden:true},null,null]; json = X.utils.sheet_to_json(ws2, {skipHidden: 1}); + assert.equal(json[0]["1"], "foo"); + assert.equal(json[1]["3"], "qux"); + }); + it('should handle skipHidden for columns if requested', function() { + var ws2 = X.utils.aoa_to_sheet(data), json = X.utils.sheet_to_json(ws2); + assert.equal(json[1]["2"], "bar"); + assert.equal(json[2]["3"], "qux"); + ws2["!cols"] = [null,{hidden:true},null,null]; json = X.utils.sheet_to_json(ws2, {skipHidden: 1}); + assert.equal(json[1]["2"], void 0); + assert.equal(json[2]["3"], "qux"); + }); + it('should handle skipHidden when first row is hidden', function() { + var ws2 = X.utils.aoa_to_sheet(data), json = X.utils.sheet_to_json(ws2); + assert.equal(json[0]["1"], true); + assert.equal(json[2]["3"], "qux"); + ws2["!rows"] = [{hidden:true},null,null,null]; json = X.utils.sheet_to_json(ws2, {skipHidden: 1}); + assert.equal(json[1]["1"], "foo"); + assert.equal(json[2]["3"], "qux"); + }); it('should disambiguate headers', function() { var _data = [["S","h","e","e","t","J","S"],[1,2,3,4,5,6,7],[2,3,4,5,6,7,8]]; var _ws = X.utils.aoa_to_sheet(_data); diff --git a/test.mjs b/test.mjs index b4e50ac..dd1f0dd 100644 --- a/test.mjs +++ b/test.mjs @@ -1809,6 +1809,30 @@ describe('json output', function() { X.utils.sheet_to_json(ws, {raw:true}); X.utils.sheet_to_json(ws, {raw:true, defval: 'jimjin'}); }); + it('should handle skipHidden for rows if requested', function() { + var ws2 = X.utils.aoa_to_sheet(data), json = X.utils.sheet_to_json(ws2); + assert.equal(json[0]["1"], true); + assert.equal(json[2]["3"], "qux"); + ws2["!rows"] = []; ws2["!rows"][1] = {hidden:true}; json = X.utils.sheet_to_json(ws2, {skipHidden: true}); + assert.equal(json[0]["1"], "foo"); + assert.equal(json[1]["3"], "qux"); + }); + it('should handle skipHidden for columns if requested', function() { + var ws2 = X.utils.aoa_to_sheet(data), json = X.utils.sheet_to_json(ws2); + assert.equal(json[1]["2"], "bar"); + assert.equal(json[2]["3"], "qux"); + ws2["!cols"] = []; ws2["!cols"][1] = {hidden:true}; json = X.utils.sheet_to_json(ws2, {skipHidden: true}); + assert.equal(json[1]["2"], void 0); + assert.equal(json[2]["3"], "qux"); + }); + it('should handle skipHidden when first row is hidden', function() { + var ws2 = X.utils.aoa_to_sheet(data), json = X.utils.sheet_to_json(ws2); + assert.equal(json[0]["1"], true); + assert.equal(json[2]["3"], "qux"); + ws2["!rows"] = [{hidden:true}]; json = X.utils.sheet_to_json(ws2, {skipHidden: true}); + assert.equal(json[1]["1"], "foo"); + assert.equal(json[2]["3"], "qux"); + }); it('should disambiguate headers', function() { var _data = [["S","h","e","e","t","J","S"],[1,2,3,4,5,6,7],[2,3,4,5,6,7,8]]; var _ws = X.utils.aoa_to_sheet(_data); diff --git a/test.ts b/test.ts index a78463a..2ae4231 100644 --- a/test.ts +++ b/test.ts @@ -1764,6 +1764,30 @@ Deno.test('json output', async function(t) { X.utils.sheet_to_json(ws, {raw:true}); X.utils.sheet_to_json(ws, {raw:true, defval: 'jimjin'}); }); + await t.step('should handle skipHidden for rows if requested', async function(t) { + var ws2 = X.utils.aoa_to_sheet(data), json: Array = X.utils.sheet_to_json(ws2); + assert.equal(json[0]["1"], true); + assert.equal(json[2]["3"], "qux"); + ws2["!rows"] = []; ws2["!rows"][1] = {hidden:true}; json = X.utils.sheet_to_json(ws2, {skipHidden: true}); + assert.equal(json[0]["1"], "foo"); + assert.equal(json[1]["3"], "qux"); + }); + await t.step('should handle skipHidden for columns if requested', async function(t) { + var ws2 = X.utils.aoa_to_sheet(data), json: Array = X.utils.sheet_to_json(ws2); + assert.equal(json[1]["2"], "bar"); + assert.equal(json[2]["3"], "qux"); + ws2["!cols"] = []; ws2["!cols"][1] = {hidden:true}; json = X.utils.sheet_to_json(ws2, {skipHidden: true}); + assert.equal(json[1]["2"], void 0); + assert.equal(json[2]["3"], "qux"); + }); + await t.step('should handle skipHidden when first row is hidden', async function(t) { + var ws2 = X.utils.aoa_to_sheet(data), json: Array = X.utils.sheet_to_json(ws2); + assert.equal(json[0]["1"], true); + assert.equal(json[2]["3"], "qux"); + ws2["!rows"] = [{hidden:true}]; json = X.utils.sheet_to_json(ws2, {skipHidden: true}); + assert.equal(json[1]["1"], "foo"); + assert.equal(json[2]["3"], "qux"); + }); await t.step('should disambiguate headers', async function(t) { var _data = [["S","h","e","e","t","J","S"],[1,2,3,4,5,6,7],[2,3,4,5,6,7,8]]; var _ws = X.utils.aoa_to_sheet(_data); diff --git a/tests/core.js b/tests/core.js index c9f91c5..531b72f 100644 --- a/tests/core.js +++ b/tests/core.js @@ -1823,6 +1823,30 @@ describe('json output', function() { X.utils.sheet_to_json(ws, {raw:true}); X.utils.sheet_to_json(ws, {raw:true, defval: 'jimjin'}); }); + it('should handle skipHidden for rows if requested', function() { + var ws2 = X.utils.aoa_to_sheet(data), json = X.utils.sheet_to_json(ws2); + assert.equal(json[0]["1"], true); + assert.equal(json[2]["3"], "qux"); + ws2["!rows"] = [null,{hidden:true},null,null]; json = X.utils.sheet_to_json(ws2, {skipHidden: 1}); + assert.equal(json[0]["1"], "foo"); + assert.equal(json[1]["3"], "qux"); + }); + it('should handle skipHidden for columns if requested', function() { + var ws2 = X.utils.aoa_to_sheet(data), json = X.utils.sheet_to_json(ws2); + assert.equal(json[1]["2"], "bar"); + assert.equal(json[2]["3"], "qux"); + ws2["!cols"] = [null,{hidden:true},null,null]; json = X.utils.sheet_to_json(ws2, {skipHidden: 1}); + assert.equal(json[1]["2"], void 0); + assert.equal(json[2]["3"], "qux"); + }); + it('should handle skipHidden when first row is hidden', function() { + var ws2 = X.utils.aoa_to_sheet(data), json = X.utils.sheet_to_json(ws2); + assert.equal(json[0]["1"], true); + assert.equal(json[2]["3"], "qux"); + ws2["!rows"] = [{hidden:true},null,null,null]; json = X.utils.sheet_to_json(ws2, {skipHidden: 1}); + assert.equal(json[1]["1"], "foo"); + assert.equal(json[2]["3"], "qux"); + }); it('should disambiguate headers', function() { var _data = [["S","h","e","e","t","J","S"],[1,2,3,4,5,6,7],[2,3,4,5,6,7,8]]; var _ws = X.utils.aoa_to_sheet(_data); @@ -2286,6 +2310,20 @@ describe('HTML', function() { assert.equal(X.utils.sheet_to_csv(ws), "A,B\n1,2\n3,4\n4,6"); }); }); + describe('empty cell containing html element should increment cell index', function() { + var html = "
abc def
"; + var expectedCellCount = 3; + it('HTML string', function() { + var ws = X.read(html, {type:'string'}).Sheets.Sheet1; + var range = X.utils.decode_range(ws['!ref']); + assert.equal(range.e.c,expectedCellCount - 1); + }); + if(domtest) it('DOM', function() { + var ws = X.utils.table_to_sheet(get_dom_element(html)); + var range = X.utils.decode_range(ws['!ref']); + assert.equal(range.e.c, expectedCellCount - 1); + }); + }); }); describe('js -> file -> js', function() { diff --git a/types/index.d.ts b/types/index.d.ts index 187968d..f6dab36 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -714,6 +714,9 @@ export interface Sheet2JSONOpts extends DateNFOption { /** if true, return raw data; if false, return formatted text */ raw?: boolean; + /** if true, skip hidden rows and columns */ + skipHidden?: boolean; + /** if true, return raw numbers; if false, return formatted numbers */ rawNumbers?: boolean; }