Skip to content

Commit

Permalink
!rows processing (fixes #188)
Browse files Browse the repository at this point in the history
based on comment from @SheetJSDev:
#81 (comment)

fixes #81 h/t @neversaid
  • Loading branch information
paulish authored and SheetJSDev committed Mar 31, 2017
1 parent 563efd8 commit 233eae2
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 12 deletions.
4 changes: 4 additions & 0 deletions bits/45_styutils.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ function process_col(coll/*:ColInfo*/) {
if(coll.customWidth) delete coll.customWidth;
}

var DEF_DPI = 96, DPI = DEF_DPI;
function px2pt(px) { return px * 72 / DPI; }
function pt2px(pt) { return pt * DPI / 72; }

/* [MS-EXSPXML3] 2.4.54 ST_enmPattern */
var XLMLPatternTypeMap = {
"None": "none",
Expand Down
17 changes: 14 additions & 3 deletions bits/67_wsxml.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ return function parse_ws_xml_data(sdata, s, opts, guess, themes, styles) {
}; })();

function write_ws_xml_data(ws/*:Worksheet*/, opts, idx/*:number*/, wb/*:Workbook*/, rels)/*:string*/ {
var o = [], r = [], range = safe_decode_range(ws['!ref']), cell, ref, rr = "", cols = [], R=0, C=0;
var o = [], r = [], range = safe_decode_range(ws['!ref']), cell, ref, rr = "", cols = [], R=0, C=0, rows = ws['!rows'];
for(C = range.s.c; C <= range.e.c; ++C) cols[C] = encode_col(C);
for(R = range.s.r; R <= range.e.r; ++R) {
r = [];
Expand All @@ -302,7 +302,18 @@ function write_ws_xml_data(ws/*:Worksheet*/, opts, idx/*:number*/, wb/*:Workbook
if(ws[ref] === undefined) continue;
if((cell = write_ws_xml_cell(ws[ref], ref, ws, opts, idx, wb)) != null) r.push(cell);
}
if(r.length > 0) o[o.length] = (writextag('row', r.join(""), {r:rr}));
if(r.length > 0) {
var params = {r:rr}
if(rows && rows[R]) {
var row = rows[R];
if(row.hidden) params.hidden = 1;
var height = -1;
if (row.hpx) height = px2pt(row.hpx);
else if (row.hpt) height = row.hpt;
if (height > -1) { params.ht = height; params.customHeight = 1; }
}
o[o.length] = (writextag('row', r.join(""), params));
}
}
return o.join("");
}
Expand All @@ -324,7 +335,7 @@ function write_ws_xml(idx/*:number*/, opts, wb/*:Workbook*/, rels)/*:string*/ {
o[o.length] = (writextag('dimension', null, {'ref': ref}));

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

if(ws['!cols'] !== undefined && ws['!cols'].length > 0) o[o.length] = (write_ws_xml_cols(ws, ws['!cols']));
o[sidx = o.length] = '<sheetData/>';
Expand Down
18 changes: 15 additions & 3 deletions tests/write.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ var data = [
[1,2,3],
[true, false, null, "sheetjs"],
["foo","bar",new Date("2014-02-19T14:30Z"), "0.3"],
["baz", null, "qux", 3.14159]
["baz", null, "qux", 3.14159],
["hidden"],
["visible"]
];

var ws_name = "SheetJS";
Expand All @@ -18,6 +20,13 @@ var wscols = [
{wpx:125}
];

var wsrows = [];
wsrows[0] = {hpt: 12}; // "points"
wsrows[1] = {hpx: 16}; // "pixels"
wsrows[2] = {hpt: 18};
wsrows[3] = {hpx: 24};
wsrows[4] = {hidden:true}; // hide row
wsrows[5] = {hidden:false};

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

/* TEST: column widths */
/* TEST: column props */
ws['!cols'] = wscols;

/* TEST: row props */
ws['!rows'] = wsrows;

/* TEST: hyperlink note: Excel does not automatically style hyperlinks */
ws['A3'].l = { Target: "http://sheetjs.com", Tooltip: "Visit us <SheetJS.com!>" };

Expand Down
21 changes: 18 additions & 3 deletions xlsx.flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -5458,6 +5458,10 @@ function process_col(coll/*:ColInfo*/) {
if(coll.customWidth) delete coll.customWidth;
}

var DEF_DPI = 96, DPI = DEF_DPI;
function px2pt(px) { return px * 72 / DPI; }
function pt2px(pt) { return pt * DPI / 72; }

/* [MS-EXSPXML3] 2.4.54 ST_enmPattern */
var XLMLPatternTypeMap = {
"None": "none",
Expand Down Expand Up @@ -9193,7 +9197,7 @@ return function parse_ws_xml_data(sdata, s, opts, guess, themes, styles) {
}; })();

function write_ws_xml_data(ws/*:Worksheet*/, opts, idx/*:number*/, wb/*:Workbook*/, rels)/*:string*/ {
var o = [], r = [], range = safe_decode_range(ws['!ref']), cell, ref, rr = "", cols = [], R=0, C=0;
var o = [], r = [], range = safe_decode_range(ws['!ref']), cell, ref, rr = "", cols = [], R=0, C=0, rows = ws['!rows'];
for(C = range.s.c; C <= range.e.c; ++C) cols[C] = encode_col(C);
for(R = range.s.r; R <= range.e.r; ++R) {
r = [];
Expand All @@ -9203,7 +9207,18 @@ function write_ws_xml_data(ws/*:Worksheet*/, opts, idx/*:number*/, wb/*:Workbook
if(ws[ref] === undefined) continue;
if((cell = write_ws_xml_cell(ws[ref], ref, ws, opts, idx, wb)) != null) r.push(cell);
}
if(r.length > 0) o[o.length] = (writextag('row', r.join(""), {r:rr}));
if(r.length > 0) {
var params = {r:rr}
if(rows && rows[R]) {
var row = rows[R];
if(row.hidden) params.hidden = 1;
var height = -1;
if (row.hpx) height = px2pt(row.hpx);
else if (row.hpt) height = row.hpt;
if (height > -1) { params.ht = height; params.customHeight = 1; }
}
o[o.length] = (writextag('row', r.join(""), params));
}
}
return o.join("");
}
Expand All @@ -9225,7 +9240,7 @@ function write_ws_xml(idx/*:number*/, opts, wb/*:Workbook*/, rels)/*:string*/ {
o[o.length] = (writextag('dimension', null, {'ref': ref}));

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

if(ws['!cols'] !== undefined && ws['!cols'].length > 0) o[o.length] = (write_ws_xml_cols(ws, ws['!cols']));
o[sidx = o.length] = '<sheetData/>';
Expand Down
21 changes: 18 additions & 3 deletions xlsx.js
Original file line number Diff line number Diff line change
Expand Up @@ -5404,6 +5404,10 @@ function process_col(coll) {
if(coll.customWidth) delete coll.customWidth;
}

var DEF_DPI = 96, DPI = DEF_DPI;
function px2pt(px) { return px * 72 / DPI; }
function pt2px(pt) { return pt * DPI / 72; }

/* [MS-EXSPXML3] 2.4.54 ST_enmPattern */
var XLMLPatternTypeMap = {
"None": "none",
Expand Down Expand Up @@ -9138,7 +9142,7 @@ return function parse_ws_xml_data(sdata, s, opts, guess, themes, styles) {
}; })();

function write_ws_xml_data(ws, opts, idx, wb, rels) {
var o = [], r = [], range = safe_decode_range(ws['!ref']), cell, ref, rr = "", cols = [], R=0, C=0;
var o = [], r = [], range = safe_decode_range(ws['!ref']), cell, ref, rr = "", cols = [], R=0, C=0, rows = ws['!rows'];
for(C = range.s.c; C <= range.e.c; ++C) cols[C] = encode_col(C);
for(R = range.s.r; R <= range.e.r; ++R) {
r = [];
Expand All @@ -9148,7 +9152,18 @@ function write_ws_xml_data(ws, opts, idx, wb, rels) {
if(ws[ref] === undefined) continue;
if((cell = write_ws_xml_cell(ws[ref], ref, ws, opts, idx, wb)) != null) r.push(cell);
}
if(r.length > 0) o[o.length] = (writextag('row', r.join(""), {r:rr}));
if(r.length > 0) {
var params = {r:rr}
if(rows && rows[R]) {
var row = rows[R];
if(row.hidden) params.hidden = 1;
var height = -1;
if (row.hpx) height = px2pt(row.hpx);
else if (row.hpt) height = row.hpt;
if (height > -1) { params.ht = height; params.customHeight = 1; }
}
o[o.length] = (writextag('row', r.join(""), params));
}
}
return o.join("");
}
Expand All @@ -9170,7 +9185,7 @@ function write_ws_xml(idx, opts, wb, rels) {
o[o.length] = (writextag('dimension', null, {'ref': ref}));

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

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

0 comments on commit 233eae2

Please sign in to comment.