Skip to content

Parse page breaks #2602

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/doc/worksheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -927,6 +927,7 @@ Please leave feedback at https://github.com/exceljs/exceljs/discussions/2575`
this._parseRows(value);

this._parseMergeCells(value);
this.rowBreaks = value.rowBreaks;
this.dataValidations = new DataValidations(value.dataValidations);
this.properties = value.properties;
this.pageSetup = value.pageSetup;
Expand Down
30 changes: 30 additions & 0 deletions lib/xlsx/xform/sheet/brk-xform.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const BaseXform = require('../base-xform');

class BrkXform extends BaseXform {
get tag() {
return 'brk';
}

render(xmlStream, model) {
xmlStream.leafNode('brk', model);
}

parseOpen(node) {
if (node.name === this.tag) {
this.model = {
id: node.attributes.id ? parseInt(node.attributes.id, 10) : undefined,
max: node.attributes.max ? parseInt(node.attributes.max, 10) : undefined,
min: node.attributes.min ? parseInt(node.attributes.min, 10) : undefined,
man: node.attributes.man ? parseInt(node.attributes.man, 10) : undefined,
};
return true;
}
return false;
}

parseClose() {
return false;
}
}

module.exports = BrkXform;
27 changes: 0 additions & 27 deletions lib/xlsx/xform/sheet/page-breaks-xform.js

This file was deleted.

64 changes: 47 additions & 17 deletions lib/xlsx/xform/sheet/row-breaks-xform.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,69 @@
'use strict';

const PageBreaksXform = require('./page-breaks-xform');
const BrkXform = require('./brk-xform');

const ListXform = require('../list-xform');
const BaseXform = require('../base-xform');

class RowBreaksXform extends ListXform {
class RowBreaksXform extends BaseXform {
constructor() {
const options = {
tag: 'rowBreaks',
count: true,
childXform: new PageBreaksXform(),
super();

this.map = {
brk: new BrkXform(),
};
super(options);
}

// get tag() { return 'rowBreaks'; }
get tag() {
return 'rowBreaks';
}

render(xmlStream, model) {
if (model && model.length) {
xmlStream.openNode(this.tag, this.$);
if (this.count) {
xmlStream.addAttribute(this.$count, model.length);
xmlStream.addAttribute('manualBreakCount', model.length);
}
xmlStream.openNode(this.tag);
xmlStream.addAttribute('count', model.length);
xmlStream.addAttribute('manualBreakCount', model.length);

const {childXform} = this;
model.forEach(childModel => {
childXform.render(xmlStream, childModel);
model.forEach(brk => {
this.map.brk.render(xmlStream, brk);
});

xmlStream.closeNode();
} else if (this.empty) {
xmlStream.leafNode(this.tag);
}
}

parseOpen(node) {
if (this.parser) {
this.parser.parseOpen(node);
return true;
}
switch (node.name) {
case this.tag:
this.model = [];
return true;
default:
this.parser = this.map[node.name];
if (this.parser) {
this.parser.parseOpen(node);
}
return true;
}
}

parseClose(name) {
if (this.parser) {
if (!this.parser.parseClose(name)) {
if (name === 'brk') {
this.model.push(this.parser.model);
}
this.parser = undefined;
}
return true;
}

return false;
}
}

module.exports = RowBreaksXform;
2 changes: 1 addition & 1 deletion lib/xlsx/xform/sheet/worksheet-xform.js
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,6 @@ class WorkSheetXform extends BaseXform {
});
return true;
}

if (this.map[node.name] && !this.ignoreNodes.includes(node.name)) {
this.parser = this.map[node.name];
this.parser.parseOpen(node);
Expand Down Expand Up @@ -425,6 +424,7 @@ class WorkSheetXform extends BaseXform {
cols: this.map.cols.model,
rows: this.map.sheetData.model,
mergeCells: this.map.mergeCells.model,
rowBreaks: this.map.rowBreaks.model,
hyperlinks: this.map.hyperlinks.model,
dataValidations: this.map.dataValidations.model,
properties,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const testXformHelper = require('../test-xform-helper');

const PageBreaksXform = verquire('xlsx/xform/sheet/page-breaks-xform');
const BrkXform = verquire('xlsx/xform/sheet/brk-xform');

const expectations = [
{
title: 'one page break',
create() {
return new PageBreaksXform();
return new BrkXform();
},
initialModel: {id: 2, max: 3, min: 1, man: 1},
preparedModel: {id: 2, max: 3, min: 1, man: 1},
Expand All @@ -16,6 +16,6 @@ const expectations = [
},
];

describe('PageBreaksXform', () => {
describe('BrkXform', () => {
testXformHelper(expectations);
});
1 change: 1 addition & 0 deletions spec/unit/xlsx/xform/sheet/data/sheet.1.3.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
"evenHeader": "&C&KCCCCCC&\"Aril\"3 exceljs evenHeader",
"evenFooter": "&Lexceljs&C&F&RPage &P evenHeader"
},
"rowBreaks": null,
"tables": null,
"conditionalFormattings": []
}
1 change: 1 addition & 0 deletions spec/unit/xlsx/xform/sheet/data/sheet.5.3.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"cols": null,
"hyperlinks": null,
"views": null,
"rowBreaks": null,
"tables": null,
"conditionalFormattings": []
}
1 change: 1 addition & 0 deletions spec/unit/xlsx/xform/sheet/data/sheet.6.3.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"cols": null,
"hyperlinks": null,
"views": null,
"rowBreaks": null,
"tables": null,
"conditionalFormattings": []
}
70 changes: 70 additions & 0 deletions spec/unit/xlsx/xform/sheet/data/sheet.7.3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
{
"dimensions": "A1:C7",
"properties": {
"defaultRowHeight": 14.4,
"dyDescent": 0.55,
"outlineLevelRow": 2,
"outlineLevelCol": 0
},
"pageSetup": {
"margins": {"left": 0.7, "right": 0.7, "top": 0.75, "bottom": 0.75, "header": 0.3, "footer": 0.3 },
"fitToPage": false
},
"rows": [
{
"number": 1, "min": 1, "max": 3,
"cells": [
{"address": "A1", "type": 3, "value": 0},
{"address": "C1", "type": 3, "value": 1}
]
},
{
"number": 2, "min": 1, "max": 3, "outlineLevel": 1,
"cells": [
{"address": "C2", "type": 3, "value": 2}
]
},
{
"number": 3, "min": 1, "max": 3, "outlineLevel": 2, "collapsed": true,
"cells": [
{"address": "C3", "type": 3, "value": 3}
]
},
{
"number": 5, "min": 1, "max": 3,
"cells": [
{"address": "A5", "type": 3, "value": 4}
]
},
{
"number": 7, "min": 1, "max": 3,
"cells": [
{"address": "A7", "type": 3, "value": 5},
{"address": "B7", "type": 2, "value": 5}
]
}
],
"cols": null,
"background": null,
"mergeCells": null,
"dataValidations": null,
"drawing": null,
"headerFooter": null,
"hyperlinks": null,
"views": [{
"rightToLeft": false,
"showGridLines": true,
"showRowColHeaders": true,
"showRuler": true,
"state": "normal",
"workbookViewId": 0,
"zoomScale": 100,
"zoomScaleNormal": 100
}],
"rowBreaks": [
{"id": 2, "max": 2, "min": 0, "man": 1},
{"id": 5, "max": 2, "min": 0, "man": 1}
],
"tables": null,
"conditionalFormattings": []
}
70 changes: 70 additions & 0 deletions spec/unit/xlsx/xform/sheet/data/sheet.7.4.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
{
"dimensions": "A1:C7",
"properties": {
"defaultRowHeight": 14.4,
"dyDescent": 0.55,
"outlineLevelRow": 2,
"outlineLevelCol": 0
},
"pageSetup": {
"margins": {"left": 0.7, "right": 0.7, "top": 0.75, "bottom": 0.75, "header": 0.3, "footer": 0.3 },
"fitToPage": false
},
"rows": [
{
"number": 1, "min": 1, "max": 3, "style": {},
"cells": [
{"address": "A1", "type": 3, "value": "Name"},
{"address": "C1", "type": 3, "value": "Tom"}
]
},
{
"number": 2, "min": 1, "max": 3, "outlineLevel": 1, "style": {},
"cells": [
{"address": "C2", "type": 3, "value": "Dick"}
]
},
{
"number": 3, "min": 1, "max": 3, "outlineLevel": 2, "collapsed": true, "style": {},
"cells": [
{"address": "C3", "type": 3, "value": "Harry"}
]
},
{
"number": 5, "min": 1, "max": 3, "style": {},
"cells": [
{"address": "A5", "type": 3, "value": "Inline"}
]
},
{
"number": 7, "min": 1, "max": 3, "style": {},
"cells": [
{"address": "A7", "type": 3, "value": "Between"},
{"address": "B7", "type": 2, "value": 5}
]
}
],
"cols": null,
"background": null,
"mergeCells": null,
"dataValidations": null,
"drawing": null,
"headerFooter": null,
"media": [],
"views": [{
"rightToLeft": false,
"showGridLines": true,
"showRowColHeaders": true,
"showRuler": true,
"state": "normal",
"workbookViewId": 0,
"zoomScale": 100,
"zoomScaleNormal": 100
}],
"rowBreaks": [
{"id": 2, "max": 2, "min": 0, "man": 1},
{"id": 5, "max": 2, "min": 0, "man": 1}
],
"tables": [],
"conditionalFormattings": []
}
4 changes: 3 additions & 1 deletion spec/unit/xlsx/xform/sheet/worksheet-xform.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,9 @@ const expectations = [
initialModel: require('./data/sheet.7.0.json'),
preparedModel: require('./data/sheet.7.1.json'),
xml: fs.readFileSync(`${__dirname}/data/sheet.7.2.xml`).toString(),
tests: ['prepare', 'render'],
parsedModel: require('./data/sheet.7.3.json'),
reconciledModel: require('./data/sheet.7.4.json'),
tests: ['prepare', 'render', 'parse', 'reconcile'],
options: {
sharedStrings: new SharedStringsXform(),
hyperlinks: [],
Expand Down