Skip to content

Commit 39f705b

Browse files
SiguzaRimaCiklum
authored andcommitted
XLSX write page margins
changes distilled from Siguza/js-xlsx see protobi#19 for discussion
1 parent 5020f07 commit 39f705b

File tree

4 files changed

+38
-11
lines changed

4 files changed

+38
-11
lines changed

bits/67_wsxml.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,10 @@ function parse_ws_xml_margins(margin) {
143143
});
144144
return o;
145145
}
146+
function write_ws_xml_margins(margin) {
147+
default_margins(margin);
148+
return writextag('pageMargins', null, margin);
149+
}
146150

147151
function parse_ws_xml_cols(columns, cols) {
148152
var seencol = false;
@@ -452,7 +456,7 @@ function write_ws_xml(idx/*:number*/, opts, wb/*:Workbook*/, rels)/*:string*/ {
452456
delete ws['!links'];
453457

454458
/* printOptions */
455-
/* pageMargins */
459+
if (ws['!margins'] != null) o[o.length] = write_ws_xml_margins(ws['!margins'])
456460
/* pageSetup */
457461

458462
var hfidx = o.length;

test.js

+23-8
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,14 @@ function hlink(wb) {
660660
assert.equal(get_cell(ws, "A7").l.Tooltip, "foo bar baz");
661661
}
662662

663+
function check_margin(margins, exp) {
664+
assert.equal(margins.left, exp[0]);
665+
assert.equal(margins.right, exp[1]);
666+
assert.equal(margins.top, exp[2]);
667+
assert.equal(margins.bottom, exp[3]);
668+
assert.equal(margins.header, exp[4]);
669+
assert.equal(margins.footer, exp[5]);
670+
}
663671

664672
describe('parse features', function() {
665673
describe('sheet visibility', function() {
@@ -979,14 +987,6 @@ describe('parse features', function() {
979987
});
980988

981989
describe('page margins', function() {
982-
function check_margin(margins, exp) {
983-
assert.equal(margins.left, exp[0]);
984-
assert.equal(margins.right, exp[1]);
985-
assert.equal(margins.top, exp[2]);
986-
assert.equal(margins.bottom, exp[3]);
987-
assert.equal(margins.header, exp[4]);
988-
assert.equal(margins.footer, exp[5]);
989-
}
990990
var wb1, wb2, wb3, wb4, wb5, wbs;
991991
var bef = (function() {
992992
wb1 = X.readFile(paths.pmxls);
@@ -1253,6 +1253,21 @@ describe('roundtrip features', function() {
12531253
});
12541254
});
12551255

1256+
describe('should preserve page margins', function() {[
1257+
//['xlml', paths.pmxml],
1258+
['xlsx', paths.pmxlsx],
1259+
['xlsb', paths.pmxlsb]
1260+
].forEach(function(w) { it(w[0], function() {
1261+
var wb1 = X.readFile(w[1]);
1262+
var wb2 = X.read(X.write(wb1, {bookType:w[0], type:"binary"}), {type:"binary"});
1263+
check_margin(wb2.Sheets["Normal"]["!margins"], [0.7, 0.7, 0.75, 0.75, 0.3, 0.3]);
1264+
check_margin(wb2.Sheets["Wide"]["!margins"], [1, 1, 1, 1, 0.5, 0.5]);
1265+
check_margin(wb2.Sheets["Wide"]["!margins"], [1, 1, 1, 1, 0.5, 0.5]);
1266+
check_margin(wb2.Sheets["Narrow"]["!margins"], [0.25, 0.25, 0.75, 0.75, 0.3, 0.3]);
1267+
check_margin(wb2.Sheets["Custom 1 Inch Centered"]["!margins"], [1, 1, 1, 1, 0.3, 0.3]);
1268+
check_margin(wb2.Sheets["1 Inch HF"]["!margins"], [0.7, 0.7, 0.75, 0.75, 1, 1]);
1269+
}); }); });
1270+
12561271
describe('should preserve sheet visibility', function() { [
12571272
['xlml', paths.svxml],
12581273
['xlsx', paths.svxlsx],

xlsx.flow.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -10100,6 +10100,10 @@ function parse_ws_xml_margins(margin) {
1010010100
});
1010110101
return o;
1010210102
}
10103+
function write_ws_xml_margins(margin) {
10104+
default_margins(margin);
10105+
return writextag('pageMargins', null, margin);
10106+
}
1010310107

1010410108
function parse_ws_xml_cols(columns, cols) {
1010510109
var seencol = false;
@@ -10409,7 +10413,7 @@ function write_ws_xml(idx/*:number*/, opts, wb/*:Workbook*/, rels)/*:string*/ {
1040910413
delete ws['!links'];
1041010414

1041110415
/* printOptions */
10412-
/* pageMargins */
10416+
if (ws['!margins'] != null) o[o.length] = write_ws_xml_margins(ws['!margins'])
1041310417
/* pageSetup */
1041410418

1041510419
var hfidx = o.length;

xlsx.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -10238,6 +10238,10 @@ function parse_ws_xml_margins(margin) {
1023810238
});
1023910239
return o;
1024010240
}
10241+
function write_ws_xml_margins(margin) {
10242+
default_margins(margin);
10243+
return writextag('pageMargins', null, margin);
10244+
}
1024110245

1024210246
function parse_ws_xml_cols(columns, cols) {
1024310247
var seencol = false;
@@ -10547,7 +10551,7 @@ function write_ws_xml(idx, opts, wb, rels) {
1054710551
delete ws['!links'];
1054810552

1054910553
/* printOptions */
10550-
/* pageMargins */
10554+
if (ws['!margins'] != null) o[o.length] = write_ws_xml_margins(ws['!margins'])
1055110555
/* pageSetup */
1055210556

1055310557
var hfidx = o.length;

0 commit comments

Comments
 (0)