Skip to content

Commit 8393273

Browse files
authored
Merge pull request #17 from 5kahoisaac/master
Corretly serialize and deserialize multiple print areas on one worksheet
2 parents 80da0ad + 24b435b commit 8393273

File tree

2 files changed

+34
-14
lines changed

2 files changed

+34
-14
lines changed

lib/xlsx/xform/book/workbook-xform.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,21 +42,19 @@ class WorkbookXform extends BaseXform {
4242
let index = 0; // sheets is sparse array - calc index manually
4343
model.sheets.forEach(sheet => {
4444
if (sheet.pageSetup && sheet.pageSetup.printArea) {
45+
const definedName = {
46+
name: '_xlnm.Print_Area',
47+
localSheetId: index,
48+
ranges: [],
49+
};
4550
sheet.pageSetup.printArea.split('&&').forEach(printArea => {
4651
const printAreaComponents = printArea.split(':');
47-
const definedName = {
48-
name: '_xlnm.Print_Area',
49-
ranges: [`'${sheet.name}'!$${printAreaComponents[0]}:$${printAreaComponents[1]}`],
50-
localSheetId: index,
51-
};
52-
printAreas.push(definedName);
52+
definedName.ranges.push(`'${sheet.name}'!$${printAreaComponents[0]}:$${printAreaComponents[1]}`);
5353
});
54+
printAreas.push(definedName);
5455
}
5556

56-
if (
57-
sheet.pageSetup &&
58-
(sheet.pageSetup.printTitlesRow || sheet.pageSetup.printTitlesColumn)
59-
) {
57+
if (sheet.pageSetup && (sheet.pageSetup.printTitlesRow || sheet.pageSetup.printTitlesColumn)) {
6058
const ranges = [];
6159

6260
if (sheet.pageSetup.printTitlesColumn) {
@@ -193,10 +191,12 @@ class WorkbookXform extends BaseXform {
193191
if (!worksheet.pageSetup) {
194192
worksheet.pageSetup = {};
195193
}
196-
const range = colCache.decodeEx(definedName.ranges[0]);
197-
worksheet.pageSetup.printArea = worksheet.pageSetup.printArea
198-
? `${worksheet.pageSetup.printArea}&&${range.dimensions}`
199-
: range.dimensions;
194+
for (let i = 0; i < definedName.ranges.length; ++i) {
195+
const range = colCache.decodeEx(definedName.ranges[i]);
196+
worksheet.pageSetup.printArea = worksheet.pageSetup.printArea
197+
? `${worksheet.pageSetup.printArea}&&${range.dimensions}`
198+
: range.dimensions;
199+
}
200200
}
201201
} else if (definedName.name === '_xlnm.Print_Titles') {
202202
worksheet = worksheets[definedName.localSheetId];
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const ExcelJS = verquire('exceljs');
2+
3+
// const NEW_TEST_17_XLSX_FILE_NAME = './spec/integration/data/test-new-pr-17.xlsx';
4+
5+
describe('new pr related issues', () => {
6+
describe('new pr 17 serialize and deserialize multiple print areas on one worksheet', () => {
7+
it('Multiple print areas can be correctly read to settings', async () => {
8+
const wb = new ExcelJS.Workbook();
9+
const ws = wb.addWorksheet('sheet');
10+
const writePrintArea = 'A2:B5&&A7:B10&&A13:B17';
11+
ws.pageSetup.printArea = writePrintArea;
12+
// await wb.xlsx.writeFile(NEW_TEST_17_XLSX_FILE_NAME);
13+
const buffer = await wb.xlsx.writeBuffer();
14+
await wb.xlsx.load(buffer);
15+
const worksheet = wb.getWorksheet('sheet');
16+
const readPintArea = worksheet.pageSetup.printArea;
17+
expect(writePrintArea).to.equal(readPintArea);
18+
});
19+
});
20+
});

0 commit comments

Comments
 (0)