skipHidden for sheet_to_json [ci skip]

This commit is contained in:
reviewher 2022-03-11 16:24:23 -08:00
parent 0044f3be82
commit 02707848ad
8 changed files with 122 additions and 1 deletions

@ -42,6 +42,7 @@ tmp
.spelling
.eslintignore
.eslintrc
.eslintmjs
.jshintrc
xlsx.mini.js
CONTRIBUTING.md

@ -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<ColInfo>*/ = o.skipHidden && sheet["!cols"] || [];
var rowinfo/*:Array<ColInfo>*/ = 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;
}

@ -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<ColInfo>*/ = o.skipHidden && sheet["!cols"] || [];
var rowinfo/*:Array<RowInfo>*/ = 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)) {

24
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);

24
test.mjs generated

@ -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);

24
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<any> = 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<any> = 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<any> = 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);

38
tests/core.js generated

@ -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 = "<table><tr><td>abc</td><td><b> </b></td><td>def</td></tr></table>";
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() {

3
types/index.d.ts vendored

@ -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;
}