Skip to content

fix issue #1057 Fix addConditionalFormatting is not a function error when using Streaming XLSX Writer #1143

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 1 commit into from
Mar 29, 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
31 changes: 31 additions & 0 deletions lib/stream/xlsx/worksheet-writer.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const PageMarginsXform = require('../../xlsx/xform/sheet/page-margins-xform');
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');

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

// ============================================================================================
Expand Down Expand Up @@ -95,6 +97,9 @@ class WorksheetWriter {
this._formulae = {};
this._siFormulae = 0;

// keep a record of conditionalFormattings
this.conditionalFormatting = [];

// for default row height, outline levels, etc
this.properties = Object.assign(
{},
Expand Down Expand Up @@ -203,6 +208,7 @@ class WorksheetWriter {
// this._writeDimensions();

this._writeHyperlinks();
this._writeConditionalFormatting();
this._writeDataValidations();
this._writeSheetProtection();
this._writePageMargins();
Expand Down Expand Up @@ -415,6 +421,23 @@ class WorksheetWriter {
this._merges.push(dimensions);
}

// ===========================================================================
// Conditional Formatting
addConditionalFormatting(cf) {
this.conditionalFormatting.push(cf);
}

removeConditionalFormatting(filter) {
console.log('conditionalFormatting', this.conditionalFormatting);
if (typeof filter === 'number') {
this.conditionalFormatting.splice(filter, 1);
} else if (filter instanceof Function) {
this.conditionalFormatting = this.conditionalFormatting.filter(filter);
} else {
this.conditionalFormatting = [];
}
}

// =========================================================================

addBackgroundImage(imageId) {
Expand Down Expand Up @@ -549,6 +572,14 @@ class WorksheetWriter {
this.stream.write(xform.hyperlinks.toXml(this._sheetRelsWriter._hyperlinks));
}

_writeConditionalFormatting() {
const options = {
styles: this._workbook.styles,
};
xform.conditionalFormattings.prepare(this.conditionalFormatting, options);
this.stream.write(xform.conditionalFormattings.toXml(this.conditionalFormatting));
}

_writeDataValidations() {
this.stream.write(xform.dataValidations.toXml(this.dataValidations.model));
}
Expand Down
23 changes: 23 additions & 0 deletions spec/integration/workbook-xlsx-writer/workbook-xlsx-writer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -506,5 +506,28 @@ describe('WorkbookWriter', () => {
const imageData = await fsReadFileAsync(IMAGE_FILENAME);
expect(Buffer.compare(imageData, image.buffer)).to.equal(0);
});

it('with conditional formatting', async () => {
const options = {
filename: TEST_XLSX_FILE_NAME,
useStyles: true,
useSharedStrings: true,
};
const wb = testUtils.createTestBook(
new ExcelJS.stream.xlsx.WorkbookWriter(options),
'xlsx',
['conditionalFormatting']
);

return wb
.commit()
.then(() => {
const wb2 = new ExcelJS.Workbook();
return wb2.xlsx.readFile(TEST_XLSX_FILE_NAME);
})
.then(wb2 => {
testUtils.checkTestBook(wb2, 'xlsx', ['conditionalFormatting']);
});
});
});
});
74 changes: 74 additions & 0 deletions spec/utils/data/conditional-formatting.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
{
"expression": {
"ref": "A1:E7",
"rules": [
{
"type": "expression",
"formulae": [
"MOD(ROW()+COLUMN(),2)=0"
],
"style": {
"fill": {
"type": "pattern",
"pattern": "solid",
"bgColor": {
"argb": "FF00FF00"
}
}
}
},
{
"type": "expression",
"priority": 2,
"formulae": [
"TRUE"
],
"style": {
"fill": {
"type": "pattern",
"pattern": "solid",
"bgColor": {
"argb": "FFFF0000"
}
}
}
}
]
},
"top10": {
"ref": "A8:E15",
"rules": [
{
"type": "top10",
"percent": true,
"rank": 10,
"style": {
"font": {
"bold": true
}
}
},
{
"type": "top10",
"percent": true,
"bottom": true,
"rank": 10,
"style": {
"font": {
"italic": true
}
}
}
]
},
"types": [
"expression",
"cellIs",
"top10",
"aboveAverage",
"colorScale",
"iconSet",
"containsText",
"timePeriod"
]
}
4 changes: 4 additions & 0 deletions spec/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const Column = verquire('doc/column');

const testSheets = {
dataValidations: require('./test-data-validation-sheet'),
conditionalFormatting: require('./test-conditional-formatting-sheet'),
values: require('./test-values-sheet'),
splice: require('./test-spliced-sheet'),
};
Expand Down Expand Up @@ -53,6 +54,9 @@ module.exports = {
styles: tools.fix(require('./data/styles.json')),
properties: tools.fix(require('./data/sheet-properties.json')),
pageSetup: tools.fix(require('./data/page-setup.json')),
conditionalFormatting: tools.fix(
require('./data/conditional-formatting.json')
),

createTestBook(workbook, docType, sheets) {
const options = getOptions(docType);
Expand Down
36 changes: 36 additions & 0 deletions spec/utils/test-conditional-formatting-sheet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const tools = require('./tools');

const self = {
conditionalFormattings: tools.fix(require('./data/conditional-formatting')),
getConditionalFormatting(type) {
return self.conditionalFormattings[type] || null;
},
addSheet(wb) {
const ws = wb.addWorksheet('conditional-formatting');
const {types} = self.conditionalFormattings;
types.forEach(type => {
const conditionalFormatting = self.getConditionalFormatting(type);
if (conditionalFormatting) {
ws.addConditionalFormatting(conditionalFormatting);
}
});
},

checkSheet(wb) {
const ws = wb.getWorksheet('conditional-formatting');
expect(ws).to.not.be.undefined();
expect(ws.conditionalFormattings).to.not.be.undefined();
(ws.conditionalFormattings && ws.conditionalFormattings).forEach(item => {
const type = item.rules && item.rules[0].type;
const conditionalFormatting = self.getConditionalFormatting(type);
expect(item).to.have.property('ref');
expect(item).to.have.property('rules');
expect(self.conditionalFormattings[type]).to.have.property('ref');
expect(self.conditionalFormattings[type]).to.have.property('rules');
expect(item.ref).to.deep.equal(conditionalFormatting.ref);
expect(item.rules.length).to.equal(conditionalFormatting.rules.length);
});
},
};

module.exports = self;