From 233eae2f4ecdbf6332a3627b161503f20021e7c0 Mon Sep 17 00:00:00 2001 From: Paul Ishenin Date: Fri, 31 Mar 2017 10:57:11 +0700 Subject: [PATCH] !rows processing (fixes #188) based on comment from @sheetjsdev: https://github.com/SheetJS/js-xlsx/issues/81#issuecomment-48360276 fixes #81 h/t @neversaid --- bits/45_styutils.js | 4 ++++ bits/67_wsxml.js | 17 ++++++++++++++--- tests/write.js | 18 +++++++++++++++--- xlsx.flow.js | 21 ++++++++++++++++++--- xlsx.js | 21 ++++++++++++++++++--- 5 files changed, 69 insertions(+), 12 deletions(-) diff --git a/bits/45_styutils.js b/bits/45_styutils.js index 331be37..a854375 100644 --- a/bits/45_styutils.js +++ b/bits/45_styutils.js @@ -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", diff --git a/bits/67_wsxml.js b/bits/67_wsxml.js index dd8ee8b..5ab4e7c 100644 --- a/bits/67_wsxml.js +++ b/bits/67_wsxml.js @@ -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 = []; @@ -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(""); } @@ -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] = ''; diff --git a/tests/write.js b/tests/write.js index 2b626c1..46752e1 100644 --- a/tests/write.js +++ b/tests/write.js @@ -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"; @@ -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]); @@ -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 " }; diff --git a/xlsx.flow.js b/xlsx.flow.js index 4ef78ee..3beca0b 100644 --- a/xlsx.flow.js +++ b/xlsx.flow.js @@ -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", @@ -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 = []; @@ -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(""); } @@ -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] = ''; diff --git a/xlsx.js b/xlsx.js index 72896aa..9dfa83a 100644 --- a/xlsx.js +++ b/xlsx.js @@ -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", @@ -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 = []; @@ -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(""); } @@ -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] = '';