Skip to content

Commit 2339e13

Browse files
author
Anton Alexandrenok
committed
skipHidden option for sheet_to_csv (fixes #755)
Note: Excel CSV conversion includes hidden rows and columns by default
1 parent 7d15f35 commit 2339e13

7 files changed

Lines changed: 62 additions & 14 deletions

File tree

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1688,6 +1688,7 @@ produces CSV output. The function takes an options argument:
16881688
| dateNF | fmt 14 | Use specified date format in string output |
16891689
| strip | false | Remove trailing field separators in each record ** |
16901690
| blankrows | true | Include blank lines in the CSV output |
1691+
| skipHidden | false | Skips hidden rows/columns in the CSV output |
16911692

16921693
- `strip` will remove trailing commas from each line under default `FS/RS`
16931694
- blankrows must be set to `false` to skip blank lines.
@@ -2313,5 +2314,3 @@ granted by the Apache 2.0 License are reserved by the Original Author.
23132314
- ISO/IEC 29500:2012(E) "Information technology — Document description and processing languages — Office Open XML File Formats"
23142315
- Open Document Format for Office Applications Version 1.2 (29 September 2011)
23152316
- Worksheet File Format (From Lotus) December 1984
2316-
2317-

bits/90_utils.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,9 @@ function sheet_to_json(sheet/*:Worksheet*/, opts/*:?Sheet2JSONOpts*/){
8181
var qreg = /"/g;
8282
function make_csv_row(sheet/*:Worksheet*/, r/*:Range*/, R/*:number*/, cols/*:Array<string>*/, fs/*:number*/, rs/*:number*/, FS/*:string*/, o/*:Sheet2CSVOpts*/)/*:?string*/ {
8383
var isempty = true;
84-
var row = "", txt = "", rr = encode_row(R);
84+
var row = [], txt = "", rr = encode_row(R);
8585
for(var C = r.s.c; C <= r.e.c; ++C) {
86+
if (!cols[C]) continue;
8687
var val = o.dense ? (sheet[R]||[])[C]: sheet[cols[C] + rr];
8788
if(val == null) txt = "";
8889
else if(val.v != null) {
@@ -95,10 +96,10 @@ function make_csv_row(sheet/*:Worksheet*/, r/*:Range*/, R/*:number*/, cols/*:Arr
9596
txt = '=' + val.f; if(txt.indexOf(",") >= 0) txt = '"' + txt.replace(qreg, '""') + '"';
9697
} else txt = "";
9798
/* NOTE: Excel CSV does not support array formulae */
98-
row += (C === r.s.c ? "" : FS) + txt;
99+
row.push(txt);
99100
}
100101
if(o.blankrows === false && isempty) return null;
101-
return row;
102+
return row.join(FS);
102103
}
103104

104105
function sheet_to_csv(sheet/*:Worksheet*/, opts/*:?Sheet2CSVOpts*/)/*:string*/ {
@@ -111,8 +112,11 @@ function sheet_to_csv(sheet/*:Worksheet*/, opts/*:?Sheet2CSVOpts*/)/*:string*/ {
111112
var endregex = new RegExp((FS=="|" ? "\\|" : FS)+"+$");
112113
var row = "", cols = [];
113114
o.dense = Array.isArray(sheet);
114-
for(var C = r.s.c; C <= r.e.c; ++C) cols[C] = encode_col(C);
115+
var colInfos = o.skipHidden ? sheet["!cols"] : undefined;
116+
var rowInfos = o.skipHidden ? sheet["!rows"] : undefined;
117+
for(var C = r.s.c; C <= r.e.c; ++C) if (!colInfos || !colInfos[C] || !colInfos[C].hidden) cols[C] = encode_col(C);
115118
for(var R = r.s.r; R <= r.e.r; ++R) {
119+
if (rowInfos && rowInfos[R] && rowInfos[R].hidden) continue;
116120
row = make_csv_row(sheet, r, R, cols, fs, rs, FS, o);
117121
if(row == null) { continue; }
118122
if(o.strip) row = row.replace(endregex,"");

docbits/82_util.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ produces CSV output. The function takes an options argument:
123123
| dateNF | fmt 14 | Use specified date format in string output |
124124
| strip | false | Remove trailing field separators in each record ** |
125125
| blankrows | true | Include blank lines in the CSV output |
126+
| skipHidden | false | Skips hidden rows/columns in the CSV output |
126127

127128
- `strip` will remove trailing commas from each line under default `FS/RS`
128129
- blankrows must be set to `false` to skip blank lines.

test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1780,6 +1780,24 @@ describe('csv', function() {
17801780
assert.equal(wb.Sheets.Sheet1['!ref'], "A1:B3");
17811781
});
17821782
});
1783+
it('should handle skipHidden for rows if requested', function() {
1784+
var baseline = "1,2,3,\nTRUE,FALSE,,sheetjs\nfoo,bar,2/19/14,0.3\n,,,\nbaz,,qux,\n";
1785+
assert.equal(X.utils.sheet_to_csv(ws), baseline);
1786+
ws["!rows"] = [null,{hidden:true},null,null];
1787+
assert.equal(X.utils.sheet_to_csv(ws), baseline);
1788+
assert.equal(X.utils.sheet_to_csv(ws, {skipHidden:true}), "1,2,3,\nfoo,bar,2/19/14,0.3\n,,,\nbaz,,qux,\n");
1789+
delete ws["!rows"];
1790+
});
1791+
it('should handle skipHidden for columns if requested', function() {
1792+
var baseline = "1,2,3,\nTRUE,FALSE,,sheetjs\nfoo,bar,2/19/14,0.3\n,,,\nbaz,,qux,\n";
1793+
assert.equal(X.utils.sheet_to_csv(ws), baseline);
1794+
ws["!cols"] = [null,{hidden:true},null,null];
1795+
assert.equal(X.utils.sheet_to_csv(ws), baseline);
1796+
assert.equal(X.utils.sheet_to_csv(ws, {skipHidden:true}), "1,3,\nTRUE,,sheetjs\nfoo,2/19/14,0.3\n,,\nbaz,qux,\n");
1797+
ws["!cols"] = [{hidden:true},null,null,null];
1798+
assert.equal(X.utils.sheet_to_csv(ws, {skipHidden:true}), "2,3,\nFALSE,,sheetjs\nbar,2/19/14,0.3\n,,\n,qux,\n");
1799+
delete ws["!cols"];
1800+
});
17831801
});
17841802
});
17851803

tests/core.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1791,6 +1791,24 @@ describe('csv', function() {
17911791
assert.equal(wb.Sheets.Sheet1['!ref'], "A1:B3");
17921792
});
17931793
});
1794+
it('should handle skipHidden for rows if requested', function() {
1795+
var baseline = "1,2,3,\nTRUE,FALSE,,sheetjs\nfoo,bar,2/19/14,0.3\n,,,\nbaz,,qux,\n";
1796+
assert.equal(X.utils.sheet_to_csv(ws), baseline);
1797+
ws["!rows"] = [null,{hidden:true},null,null];
1798+
assert.equal(X.utils.sheet_to_csv(ws), baseline);
1799+
assert.equal(X.utils.sheet_to_csv(ws, {skipHidden:true}), "1,2,3,\nfoo,bar,2/19/14,0.3\n,,,\nbaz,,qux,\n");
1800+
delete ws["!rows"];
1801+
});
1802+
it('should handle skipHidden for columns if requested', function() {
1803+
var baseline = "1,2,3,\nTRUE,FALSE,,sheetjs\nfoo,bar,2/19/14,0.3\n,,,\nbaz,,qux,\n";
1804+
assert.equal(X.utils.sheet_to_csv(ws), baseline);
1805+
ws["!cols"] = [null,{hidden:true},null,null];
1806+
assert.equal(X.utils.sheet_to_csv(ws), baseline);
1807+
assert.equal(X.utils.sheet_to_csv(ws, {skipHidden:true}), "1,3,\nTRUE,,sheetjs\nfoo,2/19/14,0.3\n,,\nbaz,qux,\n");
1808+
ws["!cols"] = [{hidden:true},null,null,null];
1809+
assert.equal(X.utils.sheet_to_csv(ws, {skipHidden:true}), "2,3,\nFALSE,,sheetjs\nbar,2/19/14,0.3\n,,\n,qux,\n");
1810+
delete ws["!cols"];
1811+
});
17941812
});
17951813
});
17961814

xlsx.flow.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17817,8 +17817,9 @@ function sheet_to_json(sheet/*:Worksheet*/, opts/*:?Sheet2JSONOpts*/){
1781717817
var qreg = /"/g;
1781817818
function make_csv_row(sheet/*:Worksheet*/, r/*:Range*/, R/*:number*/, cols/*:Array<string>*/, fs/*:number*/, rs/*:number*/, FS/*:string*/, o/*:Sheet2CSVOpts*/)/*:?string*/ {
1781917819
var isempty = true;
17820-
var row = "", txt = "", rr = encode_row(R);
17820+
var row = [], txt = "", rr = encode_row(R);
1782117821
for(var C = r.s.c; C <= r.e.c; ++C) {
17822+
if (!cols[C]) continue;
1782217823
var val = o.dense ? (sheet[R]||[])[C]: sheet[cols[C] + rr];
1782317824
if(val == null) txt = "";
1782417825
else if(val.v != null) {
@@ -17831,10 +17832,10 @@ function make_csv_row(sheet/*:Worksheet*/, r/*:Range*/, R/*:number*/, cols/*:Arr
1783117832
txt = '=' + val.f; if(txt.indexOf(",") >= 0) txt = '"' + txt.replace(qreg, '""') + '"';
1783217833
} else txt = "";
1783317834
/* NOTE: Excel CSV does not support array formulae */
17834-
row += (C === r.s.c ? "" : FS) + txt;
17835+
row.push(txt);
1783517836
}
1783617837
if(o.blankrows === false && isempty) return null;
17837-
return row;
17838+
return row.join(FS);
1783817839
}
1783917840

1784017841
function sheet_to_csv(sheet/*:Worksheet*/, opts/*:?Sheet2CSVOpts*/)/*:string*/ {
@@ -17847,8 +17848,11 @@ function sheet_to_csv(sheet/*:Worksheet*/, opts/*:?Sheet2CSVOpts*/)/*:string*/ {
1784717848
var endregex = new RegExp((FS=="|" ? "\\|" : FS)+"+$");
1784817849
var row = "", cols = [];
1784917850
o.dense = Array.isArray(sheet);
17850-
for(var C = r.s.c; C <= r.e.c; ++C) cols[C] = encode_col(C);
17851+
var colInfos = o.skipHidden ? sheet["!cols"] : undefined;
17852+
var rowInfos = o.skipHidden ? sheet["!rows"] : undefined;
17853+
for(var C = r.s.c; C <= r.e.c; ++C) if (!colInfos || !colInfos[C] || !colInfos[C].hidden) cols[C] = encode_col(C);
1785117854
for(var R = r.s.r; R <= r.e.r; ++R) {
17855+
if (rowInfos && rowInfos[R] && rowInfos[R].hidden) continue;
1785217856
row = make_csv_row(sheet, r, R, cols, fs, rs, FS, o);
1785317857
if(row == null) { continue; }
1785417858
if(o.strip) row = row.replace(endregex,"");

xlsx.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17727,8 +17727,9 @@ function sheet_to_json(sheet, opts){
1772717727
var qreg = /"/g;
1772817728
function make_csv_row(sheet, r, R, cols, fs, rs, FS, o) {
1772917729
var isempty = true;
17730-
var row = "", txt = "", rr = encode_row(R);
17730+
var row = [], txt = "", rr = encode_row(R);
1773117731
for(var C = r.s.c; C <= r.e.c; ++C) {
17732+
if (!cols[C]) continue;
1773217733
var val = o.dense ? (sheet[R]||[])[C]: sheet[cols[C] + rr];
1773317734
if(val == null) txt = "";
1773417735
else if(val.v != null) {
@@ -17741,10 +17742,10 @@ function make_csv_row(sheet, r, R, cols, fs, rs, FS, o) {
1774117742
txt = '=' + val.f; if(txt.indexOf(",") >= 0) txt = '"' + txt.replace(qreg, '""') + '"';
1774217743
} else txt = "";
1774317744
/* NOTE: Excel CSV does not support array formulae */
17744-
row += (C === r.s.c ? "" : FS) + txt;
17745+
row.push(txt);
1774517746
}
1774617747
if(o.blankrows === false && isempty) return null;
17747-
return row;
17748+
return row.join(FS);
1774817749
}
1774917750

1775017751
function sheet_to_csv(sheet, opts) {
@@ -17757,8 +17758,11 @@ function sheet_to_csv(sheet, opts) {
1775717758
var endregex = new RegExp((FS=="|" ? "\\|" : FS)+"+$");
1775817759
var row = "", cols = [];
1775917760
o.dense = Array.isArray(sheet);
17760-
for(var C = r.s.c; C <= r.e.c; ++C) cols[C] = encode_col(C);
17761+
var colInfos = o.skipHidden ? sheet["!cols"] : undefined;
17762+
var rowInfos = o.skipHidden ? sheet["!rows"] : undefined;
17763+
for(var C = r.s.c; C <= r.e.c; ++C) if (!colInfos || !colInfos[C] || !colInfos[C].hidden) cols[C] = encode_col(C);
1776117764
for(var R = r.s.r; R <= r.e.r; ++R) {
17765+
if (rowInfos && rowInfos[R] && rowInfos[R].hidden) continue;
1776217766
row = make_csv_row(sheet, r, R, cols, fs, rs, FS, o);
1776317767
if(row == null) { continue; }
1776417768
if(o.strip) row = row.replace(endregex,"");

0 commit comments

Comments
 (0)