Skip to content

Merge cells after row insert #1377

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 3 commits into from
Oct 5, 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
17 changes: 14 additions & 3 deletions lib/doc/worksheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,8 @@ class Worksheet {
spliceRows(start, count, ...inserts) {
// same problem as row.splice, except worse.
const nKeep = start + count;
const nExpand = inserts.length - count;
const nInserts = inserts.length;
const nExpand = nInserts - count;
const nEnd = this._rows.length;
let i;
let rSrc;
Expand Down Expand Up @@ -445,6 +446,16 @@ class Worksheet {
// eslint-disable-next-line no-loop-func
rSrc.eachCell({includeEmpty: true}, (cell, colNumber) => {
rDst.getCell(colNumber).style = cell.style;

// remerge cells accounting for insert offset
if (cell._value.constructor.name === 'MergeValue') {
const cellToBeMerged = this.getRow(cell._row._number + nInserts).getCell(colNumber);
const prevMaster = cell._value._master;
const newMaster = this
.getRow(prevMaster._row._number + nInserts)
.getCell(prevMaster._column._number)
cellToBeMerged.merge(newMaster);
}
});
} else {
this._rows[i + nExpand - 1] = undefined;
Expand All @@ -453,14 +464,14 @@ class Worksheet {
}

// now copy over the new values
for (i = 0; i < inserts.length; i++) {
for (i = 0; i < nInserts; i++) {
const rDst = this.getRow(start + i);
rDst.style = {};
rDst.values = inserts[i];
}

// account for defined names
this.workbook.definedNames.spliceRows(this.name, start, count, inserts.length);
this.workbook.definedNames.spliceRows(this.name, start, count, nInserts);
}

// iterate over every row in the worksheet, including maybe empty rows
Expand Down
32 changes: 32 additions & 0 deletions spec/unit/doc/worksheet.merge.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,5 +212,37 @@ describe('Worksheet', () => {
testUtils.styles.numFmts.numFmt1
);
});

it('preserves merges after row inserts', function() {
const wb = new Excel.Workbook();
const ws = wb.addWorksheet('testMergeAfterInsert');

ws.addRow([1,2]);
ws.addRow([3,4]);
ws.mergeCells('A1:B2');
ws.insertRow(1, ['Inserted Row Text']);

const r2 = ws.getRow(2);
const r3 = ws.getRow(3);

let cellVals = [];
for (const r of [r2, r3]) {
for (const cell of r._cells) {
cellVals.push(cell._value);
}
}

let nNumberVals = 0;
let nMergeVals = 0;
for (let cellVal of cellVals) {
const name = cellVal.constructor.name;
if (name === 'NumberValue') nNumberVals += 1;
if (name === 'MergeValue' && cellVal.model.master === "A2") {
nMergeVals += 1;
}
}
expect(nNumberVals).to.deep.equal(1);
expect(nMergeVals).to.deep.equal(3);
})
});
});
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy