diff --git a/lib/doc/worksheet.js b/lib/doc/worksheet.js index db600d7a6..c74c9222c 100644 --- a/lib/doc/worksheet.js +++ b/lib/doc/worksheet.js @@ -344,6 +344,49 @@ class Worksheet { }); } + duplicateRow(start, count) { + // I want to add after the start row + start++; + + const nKeep = start; + const nEnd = this._rows.length; + let i; + let rSrc; + + // insert new cells + 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); diff --git a/spec/integration/workbook/workbook.spec.js b/spec/integration/workbook/workbook.spec.js index 1dfd90792..a0136874a 100644 --- a/spec/integration/workbook/workbook.spec.js +++ b/spec/integration/workbook/workbook.spec.js @@ -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();