Skip to content

Fix existing row styles when using spliceRows #737

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
Apr 11, 2019
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
8 changes: 8 additions & 0 deletions lib/doc/worksheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,10 @@ Worksheet.prototype = {
rSrc = this._rows[i - 1];
if (rSrc) {
this.getRow(i + nExpand).values = rSrc.values;
// eslint-disable-next-line no-loop-func
rSrc.eachCell({ includeEmpty: true }, function(cell, colNumber) {
this.getRow(i + nExpand).getCell(colNumber).style = cell.style;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm.. have we 100% that cell.style object hasn't multiple references from some different cells?
may should we use deep copy here?

@cxam @alubbe what are You thinking about it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Siemienik do you think that would be necessary because the row that we get the style for each cell gets set to undefined anyway?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I worry about case like this one #669

}.bind(this));
this._rows[i - 1] = undefined;
} else {
this._rows[(i + nExpand) - 1] = undefined;
Expand All @@ -341,6 +345,10 @@ Worksheet.prototype = {
rSrc = this._rows[i - 1];
if (rSrc) {
this.getRow(i + nExpand).values = rSrc.values;
// eslint-disable-next-line no-loop-func
rSrc.eachCell({ includeEmpty: true }, function(cell, colNumber) {
this.getRow(i + nExpand).getCell(colNumber).style = cell.style;
}.bind(this));
} else {
this._rows[(i + nExpand) - 1] = undefined;
}
Expand Down
1 change: 1 addition & 0 deletions spec/integration/workbook/workbook.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,7 @@ describe('Workbook', function() {
'splice.rows.insertFewer',
'splice.rows.insertSame',
'splice.rows.insertMore',
'splice.rows.insertStyle',
'splice.columns.removeOnly',
'splice.columns.insertFewer',
'splice.columns.insertSame',
Expand Down
5 changes: 5 additions & 0 deletions spec/unit/doc/worksheet.values.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,11 @@ describe('Worksheet', function() {
testUtils.createTestBook(wb, 'xlsx', ['splice.rows.insertMore'], options);
testUtils.checkTestBook(wb, 'xlsx', ['splice.rows.insertMore'], options);
});
it('Insert style', function() {
var wb = new Excel.Workbook();
testUtils.createTestBook(wb, 'xlsx', ['splice.rows.insertStyle'], options);
testUtils.checkTestBook(wb, 'xlsx', ['splice.rows.insertStyle'], options);
});
});
describe('Columns', function() {
it('splices columns', function() {
Expand Down
47 changes: 46 additions & 1 deletion spec/utils/test-spliced-sheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,52 @@ module.exports = {
expect(ws.getRow(5).values).to.deep.equal([, 4.1,, 4.3]);
expect(ws.getRow(6).values).to.deep.equal([, '5,1', '5,2', '5,3']);
}
}
},
insertStyle: {
addSheet: function(wb) {
var ws = wb.addWorksheet('splice-row-insert-style');

ws.addRow(['1,1', '1,2', '1,3']);
ws.addRow(['2,1', '2,2', '2,3']);
ws.getCell('A2').fill = {
type: 'pattern',
pattern:'darkVertical',
fgColor: { argb:'FFFF0000' }
};

ws.spliceRows(2, 0, ['one', 'two', 'three']);
ws.getCell('A2').border = {
top: { style:'thin' },
left: { style:'thin' },
bottom: { style:'thin' },
right: { style:'thin' }
};
},

checkSheet: function(wb) {
var ws = wb.getWorksheet('splice-row-insert-style');
expect(ws).to.not.be.undefined();

expect(ws.getRow(1).values).to.deep.equal([, '1,1', '1,2', '1,3']);
expect(ws.getRow(2).values).to.deep.equal([, 'one', 'two', 'three']);
expect(ws.getRow(3).values).to.deep.equal([, '2,1', '2,2', '2,3']);
expect(ws.getCell('A2').style).to.deep.equal({
border: {
top: { style:'thin' },
left: { style:'thin' },
bottom: { style:'thin' },
right: { style:'thin' }
}
});
expect(ws.getCell('A3').style).to.deep.equal({
fill: {
type: 'pattern',
pattern:'darkVertical',
fgColor: { argb:'FFFF0000' }
}
});
}
},
},
columns: {
removeOnly: {
Expand Down