Skip to content

function duplicateRows added #1078

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 15 commits into from
Jan 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
43 changes: 43 additions & 0 deletions lib/doc/worksheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,49 @@ class Worksheet {
});
}

duplicateRow(start, count) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think it would be useful to add an optional argument "insert" = false that will control whether existing rows below the start row are shifted down or not

// I want to add after the start row
start++;
Copy link
Collaborator

Choose a reason for hiding this comment

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

For clarity - it's probably better to leave the start arg as 'immutable' and assign nKeep below to (start + 1).
Here also we could add a const rStart = this._rows[start - 1] to reference the start row as this will not change


const nKeep = start;
const nEnd = this._rows.length;
let i;
let rSrc;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Just a style issue - I tend to declare vars only where they are needed - e.g. for (let i = ... ), const rSrc = this._rows[ ... ]
Block scoping allows multiple uses of these names


// insert new cells
Copy link
Collaborator

Choose a reason for hiding this comment

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

Thinking out loud here - this for loop is a duplication of code already implemented in worksheet.spliceRows()
Perhaps a better strategy would be to prepare an array of insertion values and let spliceRows() do the donkey work...

const values = this._rows[start - 1].values;
const inserts = new Array(count - 1).fill(values);
const deleteCount = insert ? 0 : count - 1;
this.spliceRows(start + 1, deleteCount, ...inserts);

// copy styles from start row to cloned rows
...

for (i = nEnd; i >= nKeep; i--) {
rSrc = this._rows[i - 1];
if (rSrc) {
const rDst = this.getRow(i + count);
rDst.values = rSrc.values;
rDst.style = rSrc.style;
// eslint-disable-next-line no-loop-func
rSrc.eachCell({includeEmpty: true}, (cell, colNumber) => {
rDst.getCell(colNumber).style = cell.style;
});
}
else {
this._rows[i + count - 1] = undefined;
}
}

// Reference to the original row
rSrc = this._rows[start-2];

// now copy over the new values and styles
for (i = 0; i < count; i++) {
const rDst = this.getRow(start + i);
rDst.values = rSrc.values;
rDst.style = rSrc.style;
rSrc.eachCell({includeEmpty: true}, (cell, colNumber) => {
rDst.getCell(colNumber).style = cell.style;
});
}

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

spliceRows(start, count) {
// same problem as row.splice, except worse.
const inserts = Array.prototype.slice.call(arguments, 2);
Expand Down
23 changes: 23 additions & 0 deletions spec/integration/workbook/workbook.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,29 @@ describe('Workbook', () => {
});
});

describe('Duplicate Rows', () => {
it('Duplicate rows properly', () => {
const wb = new ExcelJS.Workbook();
const ws = wb.addWorksheet('duplicateTest');
ws.getCell('A1').value = 'OneInfo';
ws.duplicateRow(1,2);

return wb.xlsx
.writeFile(TEST_XLSX_FILE_NAME)
.then(() => {
const wb2 = new ExcelJS.Workbook();
return wb2.xlsx.readFile(TEST_XLSX_FILE_NAME);
})
.then(wb2 => {
const ws2 = wb2.getWorksheet('duplicateTest');

expect(ws2.getCell('A2').value).to.equal('OneInfo');
expect(ws2.getCell('A3').value).to.equal('OneInfo');
});
});
});


describe('Merge Cells', () => {
it('serialises and deserialises properly', () => {
const wb = new ExcelJS.Workbook();
Expand Down