From c356e91fdc9721e50623c629a8c5446b9df2a3f6 Mon Sep 17 00:00:00 2001 From: Siguza Date: Mon, 17 Apr 2017 03:28:38 +0100 Subject: [PATCH] XLSX write page margins changes distilled from Siguza/js-xlsx see https://github.com/protobi/js-xlsx/pull/19 for discussion --- bits/67_wsxml.js | 6 +++++- test.js | 31 +++++++++++++++++++++++-------- xlsx.flow.js | 6 +++++- xlsx.js | 6 +++++- 4 files changed, 38 insertions(+), 11 deletions(-) diff --git a/bits/67_wsxml.js b/bits/67_wsxml.js index 633792f..b4b5453 100644 --- a/bits/67_wsxml.js +++ b/bits/67_wsxml.js @@ -143,6 +143,10 @@ function parse_ws_xml_margins(margin) { }); return o; } +function write_ws_xml_margins(margin) { + default_margins(margin); + return writextag('pageMargins', null, margin); +} function parse_ws_xml_cols(columns, cols) { var seencol = false; @@ -452,7 +456,7 @@ function write_ws_xml(idx/*:number*/, opts, wb/*:Workbook*/, rels)/*:string*/ { delete ws['!links']; /* printOptions */ - /* pageMargins */ + if (ws['!margins'] != null) o[o.length] = write_ws_xml_margins(ws['!margins']) /* pageSetup */ var hfidx = o.length; diff --git a/test.js b/test.js index eafe902..81162e5 100644 --- a/test.js +++ b/test.js @@ -658,6 +658,14 @@ function hlink(wb) { assert.equal(get_cell(ws, "A7").l.Tooltip, "foo bar baz"); } +function check_margin(margins, exp) { + assert.equal(margins.left, exp[0]); + assert.equal(margins.right, exp[1]); + assert.equal(margins.top, exp[2]); + assert.equal(margins.bottom, exp[3]); + assert.equal(margins.header, exp[4]); + assert.equal(margins.footer, exp[5]); +} describe('parse features', function() { describe('sheet visibility', function() { @@ -977,14 +985,6 @@ describe('parse features', function() { }); describe('page margins', function() { - function check_margin(margins, exp) { - assert.equal(margins.left, exp[0]); - assert.equal(margins.right, exp[1]); - assert.equal(margins.top, exp[2]); - assert.equal(margins.bottom, exp[3]); - assert.equal(margins.header, exp[4]); - assert.equal(margins.footer, exp[5]); - } var wb1, wb2, wb3, wb4, wb5, wbs; var bef = (function() { wb1 = X.readFile(paths.pmxls); @@ -1251,6 +1251,21 @@ describe('roundtrip features', function() { }); }); + describe('should preserve page margins', function() {[ + //['xlml', paths.pmxml], + ['xlsx', paths.pmxlsx], + ['xlsb', paths.pmxlsb] + ].forEach(function(w) { it(w[0], function() { + var wb1 = X.readFile(w[1]); + var wb2 = X.read(X.write(wb1, {bookType:w[0], type:"binary"}), {type:"binary"}); + check_margin(wb2.Sheets["Normal"]["!margins"], [0.7, 0.7, 0.75, 0.75, 0.3, 0.3]); + check_margin(wb2.Sheets["Wide"]["!margins"], [1, 1, 1, 1, 0.5, 0.5]); + check_margin(wb2.Sheets["Wide"]["!margins"], [1, 1, 1, 1, 0.5, 0.5]); + check_margin(wb2.Sheets["Narrow"]["!margins"], [0.25, 0.25, 0.75, 0.75, 0.3, 0.3]); + check_margin(wb2.Sheets["Custom 1 Inch Centered"]["!margins"], [1, 1, 1, 1, 0.3, 0.3]); + check_margin(wb2.Sheets["1 Inch HF"]["!margins"], [0.7, 0.7, 0.75, 0.75, 1, 1]); + }); }); }); + describe('should preserve sheet visibility', function() { [ ['xlml', paths.svxml], ['xlsx', paths.svxlsx], diff --git a/xlsx.flow.js b/xlsx.flow.js index 21201b9..6b24fd1 100644 --- a/xlsx.flow.js +++ b/xlsx.flow.js @@ -10100,6 +10100,10 @@ function parse_ws_xml_margins(margin) { }); return o; } +function write_ws_xml_margins(margin) { + default_margins(margin); + return writextag('pageMargins', null, margin); +} function parse_ws_xml_cols(columns, cols) { var seencol = false; @@ -10409,7 +10413,7 @@ function write_ws_xml(idx/*:number*/, opts, wb/*:Workbook*/, rels)/*:string*/ { delete ws['!links']; /* printOptions */ - /* pageMargins */ + if (ws['!margins'] != null) o[o.length] = write_ws_xml_margins(ws['!margins']) /* pageSetup */ var hfidx = o.length; diff --git a/xlsx.js b/xlsx.js index 7a60fba..a5ffee5 100644 --- a/xlsx.js +++ b/xlsx.js @@ -10038,6 +10038,10 @@ function parse_ws_xml_margins(margin) { }); return o; } +function write_ws_xml_margins(margin) { + default_margins(margin); + return writextag('pageMargins', null, margin); +} function parse_ws_xml_cols(columns, cols) { var seencol = false; @@ -10347,7 +10351,7 @@ function write_ws_xml(idx, opts, wb, rels) { delete ws['!links']; /* printOptions */ - /* pageMargins */ + if (ws['!margins'] != null) o[o.length] = write_ws_xml_margins(ws['!margins']) /* pageSetup */ var hfidx = o.length;