-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Changes from all commits
f75811c
0ad3477
ecc0012
86b8ef1
8bf1011
2a4f343
4cd7a3a
38d4340
8726894
8b0ef45
11a7c64
2f21f40
a9ce800
116483a
e6b817f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -344,6 +344,49 @@ class Worksheet { | |
}); | ||
} | ||
|
||
duplicateRow(start, count) { | ||
// I want to add after the start row | ||
start++; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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). |
||
|
||
const nKeep = start; | ||
const nEnd = this._rows.length; | ||
let i; | ||
let rSrc; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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[ ... ] |
||
|
||
// insert new cells | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() 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); | ||
|
There was a problem hiding this comment.
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