Skip to content

Commit c356e91

Browse files
SiguzaSheetJSDev
authored andcommitted
XLSX write page margins
changes distilled from Siguza/js-xlsx see protobi#19 for discussion
1 parent 09fba37 commit c356e91

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
@@ -658,6 +658,14 @@ function hlink(wb) {
658658
assert.equal(get_cell(ws, "A7").l.Tooltip, "foo bar baz");
659659
}
660660

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

662670
describe('parse features', function() {
663671
describe('sheet visibility', function() {
@@ -977,14 +985,6 @@ describe('parse features', function() {
977985
});
978986

979987
describe('page margins', function() {
980-
function check_margin(margins, exp) {
981-
assert.equal(margins.left, exp[0]);
982-
assert.equal(margins.right, exp[1]);
983-
assert.equal(margins.top, exp[2]);
984-
assert.equal(margins.bottom, exp[3]);
985-
assert.equal(margins.header, exp[4]);
986-
assert.equal(margins.footer, exp[5]);
987-
}
988988
var wb1, wb2, wb3, wb4, wb5, wbs;
989989
var bef = (function() {
990990
wb1 = X.readFile(paths.pmxls);
@@ -1251,6 +1251,21 @@ describe('roundtrip features', function() {
12511251
});
12521252
});
12531253

1254+
describe('should preserve page margins', function() {[
1255+
//['xlml', paths.pmxml],
1256+
['xlsx', paths.pmxlsx],
1257+
['xlsb', paths.pmxlsb]
1258+
].forEach(function(w) { it(w[0], function() {
1259+
var wb1 = X.readFile(w[1]);
1260+
var wb2 = X.read(X.write(wb1, {bookType:w[0], type:"binary"}), {type:"binary"});
1261+
check_margin(wb2.Sheets["Normal"]["!margins"], [0.7, 0.7, 0.75, 0.75, 0.3, 0.3]);
1262+
check_margin(wb2.Sheets["Wide"]["!margins"], [1, 1, 1, 1, 0.5, 0.5]);
1263+
check_margin(wb2.Sheets["Wide"]["!margins"], [1, 1, 1, 1, 0.5, 0.5]);
1264+
check_margin(wb2.Sheets["Narrow"]["!margins"], [0.25, 0.25, 0.75, 0.75, 0.3, 0.3]);
1265+
check_margin(wb2.Sheets["Custom 1 Inch Centered"]["!margins"], [1, 1, 1, 1, 0.3, 0.3]);
1266+
check_margin(wb2.Sheets["1 Inch HF"]["!margins"], [0.7, 0.7, 0.75, 0.75, 1, 1]);
1267+
}); }); });
1268+
12541269
describe('should preserve sheet visibility', function() { [
12551270
['xlml', paths.svxml],
12561271
['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
@@ -10038,6 +10038,10 @@ function parse_ws_xml_margins(margin) {
1003810038
});
1003910039
return o;
1004010040
}
10041+
function write_ws_xml_margins(margin) {
10042+
default_margins(margin);
10043+
return writextag('pageMargins', null, margin);
10044+
}
1004110045

1004210046
function parse_ws_xml_cols(columns, cols) {
1004310047
var seencol = false;
@@ -10347,7 +10351,7 @@ function write_ws_xml(idx, opts, wb, rels) {
1034710351
delete ws['!links'];
1034810352

1034910353
/* printOptions */
10350-
/* pageMargins */
10354+
if (ws['!margins'] != null) o[o.length] = write_ws_xml_margins(ws['!margins'])
1035110355
/* pageSetup */
1035210356

1035310357
var hfidx = o.length;

0 commit comments

Comments
 (0)