Skip to content

Commit

Permalink
Merge pull request #13 from StefanNedelchev/feature/col-breaks
Browse files Browse the repository at this point in the history
feat: added col breaks feature
  • Loading branch information
zurmokeeper committed Jul 16, 2023
2 parents 3044178 + fd38dfc commit 80da0ad
Show file tree
Hide file tree
Showing 17 changed files with 149 additions and 9 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ Reverse engineered from Excel spreadsheet files as a project.
npm install @zurmokeeper/exceljs
```

# New Features!

Since V4.4.3, the detailed update of the new version can be accessed from [here](https://github.com/zurmokeeper/excelize/releases).

# V4.4.3 New Features!

Change Log:
Expand Down Expand Up @@ -782,6 +786,9 @@ worksheet.getColumn(5).outlineLevel = 1;
expect(worksheet.getColumn(4).collapsed).to.equal(false);
expect(worksheet.getColumn(5).collapsed).to.equal(true);

// Insert a page break below the column
dobCol.addPageBreak();

// iterate over all current cells in this column
dobCol.eachCell(function(cell, rowNumber) {
// ...
Expand Down
7 changes: 7 additions & 0 deletions README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
npm install @zurmokeeper/exceljs
```

# 新的功能!

自V4.4.3以后,新版本详细更新内容在[这里](https://github.com/zurmokeeper/excelize/releases).

# V4.4.3 新的功能!

变更日志:
Expand Down Expand Up @@ -730,6 +734,9 @@ worksheet.getColumn(5).outlineLevel = 1;
expect(worksheet.getColumn(4).collapsed).to.equal(false);
expect(worksheet.getColumn(5).collapsed).to.equal(true);

// 在该列右边插入一个分页符
dobCol.addPageBreak();

// 遍历此列中的所有当前单元格
dobCol.eachCell(function(cell, rowNumber) {
// ...
Expand Down
10 changes: 8 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,11 @@ export interface Column {
*/
eachCell(opt: { includeEmpty: boolean }, callback: (cell: Cell, rowNumber: number) => void): void;

/**
* Inserts a page break after the column
*/
addPageBreak(tp?: number, btm?: number): void;

defn: any; //todo
}
export interface PageSetup {
Expand Down Expand Up @@ -972,7 +977,7 @@ export interface Range extends Location {
}>): boolean;
}

export interface RowBreak {
export interface PageBreak {
id: number;
max: number;
min: number;
Expand All @@ -986,7 +991,8 @@ export interface WorksheetModel {
properties: WorksheetProperties;
pageSetup: Partial<PageSetup>;
headerFooter: Partial<HeaderFooter>;
rowBreaks: RowBreak[];
rowBreaks: PageBreak[];
colBreaks: PageBreak[];
views: WorksheetView[];
autoFilter: AutoFilter;
media: Media[];
Expand Down
21 changes: 17 additions & 4 deletions lib/doc/column.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,7 @@ class Column {
}

get collapsed() {
return !!(
this._outlineLevel && this._outlineLevel >= this._worksheet.properties.outlineLevelCol
);
return !!(this._outlineLevel && this._outlineLevel >= this._worksheet.properties.outlineLevelCol);
}

toString() {
Expand Down Expand Up @@ -201,6 +199,21 @@ class Column {
});
}

// Page Breaks
addPageBreak(tp, btn) {
const ws = this._worksheet;
const top = Math.max(0, tp - 1) || 0;
const bottom = Math.max(0, btn - 1) || 65535;
const pb = {
id: this._number,
max: bottom,
man: 1,
};
if (top) pb.min = top;

ws.colBreaks.push(pb);
}

// =========================================================================
// styles
_applyStyle(name, value) {
Expand Down Expand Up @@ -301,7 +314,7 @@ class Column {
* sort cols by min
* If it is not sorted, the subsequent column configuration will be overwritten
* */
cols = cols.sort(function(pre, next) {
cols = cols.sort(function(pre, next) {
return pre.min - next.min;
});
while (index < cols.length) {
Expand Down
2 changes: 2 additions & 0 deletions lib/doc/worksheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class Worksheet {

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

// for tabColor, default row height, outline levels, etc
this.properties = Object.assign(
Expand Down Expand Up @@ -850,6 +851,7 @@ class Worksheet {
pageSetup: this.pageSetup,
headerFooter: this.headerFooter,
rowBreaks: this.rowBreaks,
colBreaks: this.colBreaks,
views: this.views,
autoFilter: this.autoFilter,
media: this._media.map(medium => medium.model),
Expand Down
8 changes: 8 additions & 0 deletions lib/stream/xlsx/worksheet-writer.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const PictureXform = require('../../xlsx/xform/sheet/picture-xform');
const ConditionalFormattingsXform = require('../../xlsx/xform/sheet/cf/conditional-formattings-xform');
const HeaderFooterXform = require('../../xlsx/xform/sheet/header-footer-xform');
const RowBreaksXform = require('../../xlsx/xform/sheet/row-breaks-xform');
const ColBreaksXform = require('../../xlsx/xform/sheet/col-breaks-xform');

// since prepare and render are functional, we can use singletons
const xform = {
Expand All @@ -52,6 +53,7 @@ const xform = {
conditionalFormattings: new ConditionalFormattingsXform(),
headerFooter: new HeaderFooterXform(),
rowBreaks: new RowBreaksXform(),
colBreaks: new ColBreaksXform(),
};

// ============================================================================================
Expand Down Expand Up @@ -107,6 +109,7 @@ class WorksheetWriter {

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

// for default row height, outline levels, etc
this.properties = Object.assign(
Expand Down Expand Up @@ -246,6 +249,7 @@ class WorksheetWriter {
this._writeBackground();
this._writeHeaderFooter();
this._writeRowBreaks();
this._writeColBreaks();

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

_writeColBreaks() {
this.stream.write(xform.colBreaks.toXml(this.colBreaks));
}

_writeDataValidations() {
this.stream.write(xform.dataValidations.toXml(this.dataValidations.model));
}
Expand Down
39 changes: 39 additions & 0 deletions lib/xlsx/xform/sheet/col-breaks-xform.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
'use strict';

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

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

class ColBreaksXform extends ListXform {
constructor() {
const options = {
tag: 'colBreaks',
count: true,
childXform: new PageBreaksXform(),
};
super(options);
}

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

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);
}

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

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

module.exports = ColBreaksXform;
3 changes: 3 additions & 0 deletions lib/xlsx/xform/sheet/worksheet-xform.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const PictureXform = require('./picture-xform');
const DrawingXform = require('./drawing-xform');
const TablePartXform = require('./table-part-xform');
const RowBreaksXform = require('./row-breaks-xform');
const ColBreaksXform = require('./col-breaks-xform');
const HeaderFooterXform = require('./header-footer-xform');
const ConditionalFormattingsXform = require('./cf/conditional-formattings-xform');
const ExtListXform = require('./ext-lst-xform');
Expand Down Expand Up @@ -113,6 +114,7 @@ class WorkSheetXform extends BaseXform {
autoFilter: new AutoFilterXform(),
mergeCells: new ListXform({tag: 'mergeCells', count: true, childXform: new MergeCellXform()}),
rowBreaks: new RowBreaksXform(),
colBreaks: new ColBreaksXform(),
hyperlinks: new ListXform({
tag: 'hyperlinks',
count: false,
Expand Down Expand Up @@ -343,6 +345,7 @@ class WorkSheetXform extends BaseXform {
this.map.pageSetup.render(xmlStream, model.pageSetup);
this.map.headerFooter.render(xmlStream, model.headerFooter);
this.map.rowBreaks.render(xmlStream, model.rowBreaks);
this.map.colBreaks.render(xmlStream, model.colBreaks);
this.map.drawing.render(xmlStream, model.drawing); // Note: must be after rowBreaks
this.map.picture.render(xmlStream, model.background); // Note: must be after drawing
this.map.tableParts.render(xmlStream, model.tables);
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zurmokeeper/exceljs",
"version": "4.4.3",
"version": "4.4.4",
"description": "Excel Workbook Manager - Read and Write xlsx and csv Files.",
"private": false,
"license": "MIT",
Expand Down Expand Up @@ -111,7 +111,7 @@
"saxes": "^5.0.1",
"tmp": "^0.2.0",
"unzipper": "^0.10.11",
"uuid": "^8.3.0"
"uuid": "^9.0.0"
},
"devDependencies": {
"@babel/cli": "^7.10.5",
Expand Down
22 changes: 22 additions & 0 deletions spec/integration/worksheet-xlsx-writer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -534,5 +534,27 @@ describe('WorksheetWriter', () => {
row.addPageBreak();
expect(ws.rowBreaks.length).to.equal(2);
});

it('adds multiple col 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('C1').value = 'C1';
ws.getCell('D1').value = 'D1';
ws.getCell('A2').value = 'A2';
ws.getCell('B2').value = 'B2';
ws.getCell('C2').value = 'C2';
ws.getCell('D2').value = 'D2';

let col = ws.getColumn('A');
col.addPageBreak();
col = ws.getColumn('C');
col.addPageBreak();

expect(ws.colBreaks.length).to.equal(2);
});
});
});
22 changes: 22 additions & 0 deletions spec/unit/doc/worksheet.pagebreak.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,27 @@ describe('Worksheet', () => {

expect(ws.rowBreaks.length).to.equal(2);
});

it('adds multiple col breaks', () => {
const wb = new Excel.Workbook();
const ws = wb.addWorksheet('blort');

// initial values
ws.getCell('A1').value = 'A1';
ws.getCell('B1').value = 'B1';
ws.getCell('C1').value = 'C1';
ws.getCell('D1').value = 'D1';
ws.getCell('A2').value = 'A2';
ws.getCell('B2').value = 'B2';
ws.getCell('C2').value = 'C2';
ws.getCell('D2').value = 'D2';

let col = ws.getColumn('A');
col.addPageBreak();
col = ws.getColumn('C');
col.addPageBreak();

expect(ws.colBreaks.length).to.equal(2);
});
});
});
1 change: 1 addition & 0 deletions spec/unit/xlsx/xform/sheet/data/sheet.1.0.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
},
"media": [],
"rowBreaks": [],
"colBreaks": [],
"tables": [],
"conditionalFormattings": []
}
1 change: 1 addition & 0 deletions spec/unit/xlsx/xform/sheet/data/sheet.1.1.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
"comments": [],
"media": [],
"rowBreaks": [],
"colBreaks": [],
"tables": [],
"headerFooter": {
"oddHeader": "&C&KCCCCCC&\"Aril\"2 exceljs",
Expand Down
3 changes: 3 additions & 0 deletions spec/unit/xlsx/xform/sheet/data/sheet.7.0.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@
{"id": 2, "max": 2, "min": 0, "man": 1},
{"id": 5, "max": 2, "min": 0, "man": 1}
],
"colBreaks": [
{ "id": 3, "min": 0, "max": 1000, "man": 1 }
],
"media": [],
"tables": [],
"conditionalFormattings": []
Expand Down
3 changes: 3 additions & 0 deletions spec/unit/xlsx/xform/sheet/data/sheet.7.1.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@
{"id": 2, "max": 2, "min": 0, "man": 1},
{"id": 5, "max": 2, "min": 0, "man": 1}
],
"colBreaks": [
{ "id": 3, "min": 0, "max": 1000, "man": 1 }
],
"comments": [],
"media": [],
"tables": [],
Expand Down
3 changes: 3 additions & 0 deletions spec/unit/xlsx/xform/sheet/data/sheet.7.2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,8 @@
<brk id="2" max="2" min="0" man="1"/>
<brk id="5" max="2" min="0" man="1"/>
</rowBreaks>
<colBreaks count="1" manualBreakCount="1">
<brk id="3" max="1000" min="0" man="1"/>
</colBreaks>
<pageMargins left="0.7" right="0.7" top="0.75" bottom="0.75" header="0.3" footer="0.3"/>
</worksheet>
2 changes: 1 addition & 1 deletion spec/unit/xlsx/xform/sheet/worksheet-xform.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ const expectations = [
},
},
{
title: 'Sheet 7 - Row Breaks',
title: 'Sheet 7 - Page Breaks',
create: () => new WorksheetXform(),
initialModel: require('./data/sheet.7.0.json'),
preparedModel: require('./data/sheet.7.1.json'),
Expand Down

0 comments on commit 80da0ad

Please sign in to comment.