Skip to content

WorkbookWriter support rowBreaks #1257

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

Merged
merged 2 commits into from
May 21, 2020
Merged
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
12 changes: 11 additions & 1 deletion lib/stream/xlsx/worksheet-writer.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const PageSetupXform = require('../../xlsx/xform/sheet/page-setup-xform');
const AutoFilterXform = require('../../xlsx/xform/sheet/auto-filter-xform');
const PictureXform = require('../../xlsx/xform/sheet/picture-xform');
const ConditionalFormattingsXform = require('../../xlsx/xform/sheet/cf/conditional-formattings-xform');
const RowBreaksXform = require('../../xlsx/xform/sheet/row-breaks-xform');

// since prepare and render are functional, we can use singletons
const xform = {
Expand All @@ -47,6 +48,7 @@ const xform = {
autoFilter: new AutoFilterXform(),
picture: new PictureXform(),
conditionalFormattings: new ConditionalFormattingsXform(),
rowBreaks: new RowBreaksXform(),
};

// ============================================================================================
Expand All @@ -72,7 +74,7 @@ class WorksheetWriter {
// column keys (addRow convenience): key ==> this._columns index
this._keys = {};

// keep record of all merges
// keep a record of all row and column pageBreaks
this._merges = [];
this._merges.add = function() {}; // ignore cell instruction

Expand Down Expand Up @@ -100,6 +102,9 @@ class WorksheetWriter {
// keep a record of conditionalFormattings
this.conditionalFormatting = [];

// keep a record of all row and column pageBreaks
this.rowBreaks = [];

// for default row height, outline levels, etc
this.properties = Object.assign(
{},
Expand Down Expand Up @@ -214,6 +219,7 @@ class WorksheetWriter {
this._writePageMargins();
this._writePageSetup();
this._writeBackground();
this._writeRowBreaks();

// Legacy Data tag for comments
this._writeLegacyData();
Expand Down Expand Up @@ -580,6 +586,10 @@ class WorksheetWriter {
this.stream.write(xform.conditionalFormattings.toXml(this.conditionalFormatting));
}

_writeRowBreaks() {
this.stream.write(xform.rowBreaks.toXml(this.rowBreaks));
}

_writeDataValidations() {
this.stream.write(xform.dataValidations.toXml(this.dataValidations.model));
}
Expand Down
23 changes: 22 additions & 1 deletion spec/integration/worksheet-xlsx-writer.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const testutils = require('./../utils/index');
const testutils = require('../utils/index');

const ExcelJS = verquire('exceljs');

Expand Down Expand Up @@ -514,4 +514,25 @@ describe('WorksheetWriter', () => {
}).to.throw(Error);
});
});

describe('Page Breaks', () => {
it('adds multiple row breaks', () => {
const wb = new ExcelJS.stream.xlsx.WorkbookWriter();
const ws = wb.addWorksheet('blort');

// initial values
ws.getCell('A1').value = 'A1';
ws.getCell('B1').value = 'B1';
ws.getCell('A2').value = 'A2';
ws.getCell('B2').value = 'B2';
ws.getCell('A3').value = 'A3';
ws.getCell('B3').value = 'B3';

let row = ws.getRow(1);
row.addPageBreak();
row = ws.getRow(2);
row.addPageBreak();
expect(ws.rowBreaks.length).to.equal(2);
});
});
});