Skip to content

Commit 56b35c1

Browse files
paulishsaarCiklum
authored andcommitted
!rows processing (fixes protobi#188)
based on comment from @SheetJSDev: SheetJS#81 (comment) fixes protobi#81 h/t @neversaid
1 parent a80f0ab commit 56b35c1

File tree

5 files changed

+69
-12
lines changed

5 files changed

+69
-12
lines changed

bits/45_styutils.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ function process_col(coll/*:ColInfo*/) {
9090
if(coll.customWidth) delete coll.customWidth;
9191
}
9292

93+
var DEF_DPI = 96, DPI = DEF_DPI;
94+
function px2pt(px) { return px * 72 / DPI; }
95+
function pt2px(pt) { return pt * DPI / 72; }
96+
9397
/* [MS-EXSPXML3] 2.4.54 ST_enmPattern */
9498
var XLMLPatternTypeMap = {
9599
"None": "none",

bits/67_wsxml.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ return function parse_ws_xml_data(sdata, s, opts, guess, themes, styles) {
292292
}; })();
293293

294294
function write_ws_xml_data(ws/*:Worksheet*/, opts, idx/*:number*/, wb/*:Workbook*/, rels)/*:string*/ {
295-
var o = [], r = [], range = safe_decode_range(ws['!ref']), cell, ref, rr = "", cols = [], R=0, C=0;
295+
var o = [], r = [], range = safe_decode_range(ws['!ref']), cell, ref, rr = "", cols = [], R=0, C=0, rows = ws['!rows'];
296296
for(C = range.s.c; C <= range.e.c; ++C) cols[C] = encode_col(C);
297297
for(R = range.s.r; R <= range.e.r; ++R) {
298298
r = [];
@@ -302,7 +302,18 @@ function write_ws_xml_data(ws/*:Worksheet*/, opts, idx/*:number*/, wb/*:Workbook
302302
if(ws[ref] === undefined) continue;
303303
if((cell = write_ws_xml_cell(ws[ref], ref, ws, opts, idx, wb)) != null) r.push(cell);
304304
}
305-
if(r.length > 0) o[o.length] = (writextag('row', r.join(""), {r:rr}));
305+
if(r.length > 0) {
306+
var params = {r:rr}
307+
if(rows && rows[R]) {
308+
var row = rows[R];
309+
if(row.hidden) params.hidden = 1;
310+
var height = -1;
311+
if (row.hpx) height = px2pt(row.hpx);
312+
else if (row.hpt) height = row.hpt;
313+
if (height > -1) { params.ht = height; params.customHeight = 1; }
314+
}
315+
o[o.length] = (writextag('row', r.join(""), params));
316+
}
306317
}
307318
return o.join("");
308319
}
@@ -324,7 +335,7 @@ function write_ws_xml(idx/*:number*/, opts, wb/*:Workbook*/, rels)/*:string*/ {
324335
o[o.length] = (writextag('dimension', null, {'ref': ref}));
325336

326337
/* TODO: store in WB, process styles */
327-
if(opts.sheetFormat) o[o.length] = (writextag('sheetFormatPr', null, {defaultRowHeight:opts.sheetFormat.defaultRowHeight||'16', baseColWidth:opts.sheetFormat.baseColWidth||'10' }))
338+
if(opts.sheetFormat) o[o.length] = (writextag('sheetFormatPr', null, {defaultRowHeight:opts.sheetFormat.defaultRowHeight||'16', baseColWidth:opts.sheetFormat.baseColWidth||'10' }));
328339

329340
if(ws['!cols'] !== undefined && ws['!cols'].length > 0) o[o.length] = (write_ws_xml_cols(ws, ws['!cols']));
330341
o[sidx = o.length] = '<sheetData/>';

tests/write.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ var data = [
66
[1,2,3],
77
[true, false, null, "sheetjs"],
88
["foo","bar",new Date("2014-02-19T14:30Z"), "0.3"],
9-
["baz", null, "qux", 3.14159]
9+
["baz", null, "qux", 3.14159],
10+
["hidden"],
11+
["visible"]
1012
];
1113

1214
var ws_name = "SheetJS";
@@ -18,6 +20,13 @@ var wscols = [
1820
{wpx:125}
1921
];
2022

23+
var wsrows = [];
24+
wsrows[0] = {hpt: 12}; // "points"
25+
wsrows[1] = {hpx: 16}; // "pixels"
26+
wsrows[2] = {hpt: 18};
27+
wsrows[3] = {hpx: 24};
28+
wsrows[4] = {hidden:true}; // hide row
29+
wsrows[5] = {hidden:false};
2130

2231
console.log("Sheet Name: " + ws_name);
2332
console.log("Data: "); for(var i=0; i!=data.length; ++i) console.log(data[i]);
@@ -50,11 +59,14 @@ ws['E1'] = {t:'n', f:"TRANSPOSE(A1:D1)", F:"E1:E4"};
5059
ws['E2'] = {t:'n', F:"E1:E4"};
5160
ws['E3'] = {t:'n', F:"E1:E4"};
5261
ws['E4'] = {t:'n', F:"E1:E4"};
53-
ws["!ref"] = "A1:E4";
62+
ws["!ref"] = "A1:E6";
5463

55-
/* TEST: column widths */
64+
/* TEST: column props */
5665
ws['!cols'] = wscols;
5766

67+
/* TEST: row props */
68+
ws['!rows'] = wsrows;
69+
5870
/* TEST: hyperlink note: Excel does not automatically style hyperlinks */
5971
ws['A3'].l = { Target: "http://sheetjs.com", Tooltip: "Visit us <SheetJS.com!>" };
6072

xlsx.flow.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5458,6 +5458,10 @@ function process_col(coll/*:ColInfo*/) {
54585458
if(coll.customWidth) delete coll.customWidth;
54595459
}
54605460

5461+
var DEF_DPI = 96, DPI = DEF_DPI;
5462+
function px2pt(px) { return px * 72 / DPI; }
5463+
function pt2px(pt) { return pt * DPI / 72; }
5464+
54615465
/* [MS-EXSPXML3] 2.4.54 ST_enmPattern */
54625466
var XLMLPatternTypeMap = {
54635467
"None": "none",
@@ -9193,7 +9197,7 @@ return function parse_ws_xml_data(sdata, s, opts, guess, themes, styles) {
91939197
}; })();
91949198

91959199
function write_ws_xml_data(ws/*:Worksheet*/, opts, idx/*:number*/, wb/*:Workbook*/, rels)/*:string*/ {
9196-
var o = [], r = [], range = safe_decode_range(ws['!ref']), cell, ref, rr = "", cols = [], R=0, C=0;
9200+
var o = [], r = [], range = safe_decode_range(ws['!ref']), cell, ref, rr = "", cols = [], R=0, C=0, rows = ws['!rows'];
91979201
for(C = range.s.c; C <= range.e.c; ++C) cols[C] = encode_col(C);
91989202
for(R = range.s.r; R <= range.e.r; ++R) {
91999203
r = [];
@@ -9203,7 +9207,18 @@ function write_ws_xml_data(ws/*:Worksheet*/, opts, idx/*:number*/, wb/*:Workbook
92039207
if(ws[ref] === undefined) continue;
92049208
if((cell = write_ws_xml_cell(ws[ref], ref, ws, opts, idx, wb)) != null) r.push(cell);
92059209
}
9206-
if(r.length > 0) o[o.length] = (writextag('row', r.join(""), {r:rr}));
9210+
if(r.length > 0) {
9211+
var params = {r:rr}
9212+
if(rows && rows[R]) {
9213+
var row = rows[R];
9214+
if(row.hidden) params.hidden = 1;
9215+
var height = -1;
9216+
if (row.hpx) height = px2pt(row.hpx);
9217+
else if (row.hpt) height = row.hpt;
9218+
if (height > -1) { params.ht = height; params.customHeight = 1; }
9219+
}
9220+
o[o.length] = (writextag('row', r.join(""), params));
9221+
}
92079222
}
92089223
return o.join("");
92099224
}
@@ -9225,7 +9240,7 @@ function write_ws_xml(idx/*:number*/, opts, wb/*:Workbook*/, rels)/*:string*/ {
92259240
o[o.length] = (writextag('dimension', null, {'ref': ref}));
92269241

92279242
/* TODO: store in WB, process styles */
9228-
if(opts.sheetFormat) o[o.length] = (writextag('sheetFormatPr', null, {defaultRowHeight:opts.sheetFormat.defaultRowHeight||'16', baseColWidth:opts.sheetFormat.baseColWidth||'10' }))
9243+
if(opts.sheetFormat) o[o.length] = (writextag('sheetFormatPr', null, {defaultRowHeight:opts.sheetFormat.defaultRowHeight||'16', baseColWidth:opts.sheetFormat.baseColWidth||'10' }));
92299244

92309245
if(ws['!cols'] !== undefined && ws['!cols'].length > 0) o[o.length] = (write_ws_xml_cols(ws, ws['!cols']));
92319246
o[sidx = o.length] = '<sheetData/>';

xlsx.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5412,6 +5412,10 @@ function process_col(coll) {
54125412
if(coll.customWidth) delete coll.customWidth;
54135413
}
54145414

5415+
var DEF_DPI = 96, DPI = DEF_DPI;
5416+
function px2pt(px) { return px * 72 / DPI; }
5417+
function pt2px(pt) { return pt * DPI / 72; }
5418+
54155419
/* [MS-EXSPXML3] 2.4.54 ST_enmPattern */
54165420
var XLMLPatternTypeMap = {
54175421
"None": "none",
@@ -9351,7 +9355,7 @@ return function parse_ws_xml_data(sdata, s, opts, guess, themes, styles) {
93519355
}; })();
93529356

93539357
function write_ws_xml_data(ws, opts, idx, wb, rels) {
9354-
var o = [], r = [], range = safe_decode_range(ws['!ref']), cell, ref, rr = "", cols = [], R=0, C=0;
9358+
var o = [], r = [], range = safe_decode_range(ws['!ref']), cell, ref, rr = "", cols = [], R=0, C=0, rows = ws['!rows'];
93559359
for(C = range.s.c; C <= range.e.c; ++C) cols[C] = encode_col(C);
93569360
for(R = range.s.r; R <= range.e.r; ++R) {
93579361
r = [];
@@ -9361,7 +9365,18 @@ function write_ws_xml_data(ws, opts, idx, wb, rels) {
93619365
if(ws[ref] === undefined) continue;
93629366
if((cell = write_ws_xml_cell(ws[ref], ref, ws, opts, idx, wb)) != null) r.push(cell);
93639367
}
9364-
if(r.length > 0) o[o.length] = (writextag('row', r.join(""), {r:rr}));
9368+
if(r.length > 0) {
9369+
var params = {r:rr}
9370+
if(rows && rows[R]) {
9371+
var row = rows[R];
9372+
if(row.hidden) params.hidden = 1;
9373+
var height = -1;
9374+
if (row.hpx) height = px2pt(row.hpx);
9375+
else if (row.hpt) height = row.hpt;
9376+
if (height > -1) { params.ht = height; params.customHeight = 1; }
9377+
}
9378+
o[o.length] = (writextag('row', r.join(""), params));
9379+
}
93659380
}
93669381
return o.join("");
93679382
}
@@ -9383,7 +9398,7 @@ function write_ws_xml(idx, opts, wb, rels) {
93839398
o[o.length] = (writextag('dimension', null, {'ref': ref}));
93849399

93859400
/* TODO: store in WB, process styles */
9386-
if(opts.sheetFormat) o[o.length] = (writextag('sheetFormatPr', null, {defaultRowHeight:opts.sheetFormat.defaultRowHeight||'16', baseColWidth:opts.sheetFormat.baseColWidth||'10' }))
9401+
if(opts.sheetFormat) o[o.length] = (writextag('sheetFormatPr', null, {defaultRowHeight:opts.sheetFormat.defaultRowHeight||'16', baseColWidth:opts.sheetFormat.baseColWidth||'10' }));
93879402

93889403
if(ws['!cols'] !== undefined && ws['!cols'].length > 0) o[o.length] = (write_ws_xml_cols(ws, ws['!cols']));
93899404
o[sidx = o.length] = '<sheetData/>';

0 commit comments

Comments
 (0)