Skip to content

Commit

Permalink
XLSX write page margins
Browse files Browse the repository at this point in the history
changes distilled from Siguza/js-xlsx
see protobi/js-xlsx#19 for discussion
  • Loading branch information
KingTiger001 committed Apr 17, 2017
1 parent 960f0da commit 9f41e6b
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 11 deletions.
6 changes: 5 additions & 1 deletion bits/67_wsxml.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
31 changes: 23 additions & 8 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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],
Expand Down
6 changes: 5 additions & 1 deletion xlsx.flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 5 additions & 1 deletion xlsx.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 9f41e6b

Please sign in to comment.