Skip to content

Commit

Permalink
Merge pull request #17 from 5kahoisaac/master
Browse files Browse the repository at this point in the history
Corretly serialize and deserialize multiple print areas on one worksheet
  • Loading branch information
zurmokeeper authored Jul 30, 2023
2 parents 80da0ad + 24b435b commit 8393273
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 14 deletions.
28 changes: 14 additions & 14 deletions lib/xlsx/xform/book/workbook-xform.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,19 @@ class WorkbookXform extends BaseXform {
let index = 0; // sheets is sparse array - calc index manually
model.sheets.forEach(sheet => {
if (sheet.pageSetup && sheet.pageSetup.printArea) {
const definedName = {
name: '_xlnm.Print_Area',
localSheetId: index,
ranges: [],
};
sheet.pageSetup.printArea.split('&&').forEach(printArea => {
const printAreaComponents = printArea.split(':');
const definedName = {
name: '_xlnm.Print_Area',
ranges: [`'${sheet.name}'!$${printAreaComponents[0]}:$${printAreaComponents[1]}`],
localSheetId: index,
};
printAreas.push(definedName);
definedName.ranges.push(`'${sheet.name}'!$${printAreaComponents[0]}:$${printAreaComponents[1]}`);
});
printAreas.push(definedName);
}

if (
sheet.pageSetup &&
(sheet.pageSetup.printTitlesRow || sheet.pageSetup.printTitlesColumn)
) {
if (sheet.pageSetup && (sheet.pageSetup.printTitlesRow || sheet.pageSetup.printTitlesColumn)) {
const ranges = [];

if (sheet.pageSetup.printTitlesColumn) {
Expand Down Expand Up @@ -193,10 +191,12 @@ class WorkbookXform extends BaseXform {
if (!worksheet.pageSetup) {
worksheet.pageSetup = {};
}
const range = colCache.decodeEx(definedName.ranges[0]);
worksheet.pageSetup.printArea = worksheet.pageSetup.printArea
? `${worksheet.pageSetup.printArea}&&${range.dimensions}`
: range.dimensions;
for (let i = 0; i < definedName.ranges.length; ++i) {
const range = colCache.decodeEx(definedName.ranges[i]);
worksheet.pageSetup.printArea = worksheet.pageSetup.printArea
? `${worksheet.pageSetup.printArea}&&${range.dimensions}`
: range.dimensions;
}
}
} else if (definedName.name === '_xlnm.Print_Titles') {
worksheet = worksheets[definedName.localSheetId];
Expand Down
20 changes: 20 additions & 0 deletions spec/integration/pr/new/test-new-pr-17.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const ExcelJS = verquire('exceljs');

// const NEW_TEST_17_XLSX_FILE_NAME = './spec/integration/data/test-new-pr-17.xlsx';

describe('new pr related issues', () => {
describe('new pr 17 serialize and deserialize multiple print areas on one worksheet', () => {
it('Multiple print areas can be correctly read to settings', async () => {
const wb = new ExcelJS.Workbook();
const ws = wb.addWorksheet('sheet');
const writePrintArea = 'A2:B5&&A7:B10&&A13:B17';
ws.pageSetup.printArea = writePrintArea;
// await wb.xlsx.writeFile(NEW_TEST_17_XLSX_FILE_NAME);
const buffer = await wb.xlsx.writeBuffer();
await wb.xlsx.load(buffer);
const worksheet = wb.getWorksheet('sheet');
const readPintArea = worksheet.pageSetup.printArea;
expect(writePrintArea).to.equal(readPintArea);
});
});
});

0 comments on commit 8393273

Please sign in to comment.