diff --git a/examples/Excel/Workbook-Test.xlsx b/examples/Excel/Workbook-Test.xlsx index f6a5b6bc..eca655e5 100644 Binary files a/examples/Excel/Workbook-Test.xlsx and b/examples/Excel/Workbook-Test.xlsx differ diff --git a/merlin-core/src/main/kotlin/de/micromata/merlin/excel/ExcelRow.kt b/merlin-core/src/main/kotlin/de/micromata/merlin/excel/ExcelRow.kt index 81176d54..f4c62ba4 100644 --- a/merlin-core/src/main/kotlin/de/micromata/merlin/excel/ExcelRow.kt +++ b/merlin-core/src/main/kotlin/de/micromata/merlin/excel/ExcelRow.kt @@ -131,7 +131,7 @@ class ExcelRow(val sheet: ExcelSheet, val row: Row) { @JvmOverloads fun copyAndInsert(targetSheet: ExcelSheet? = null, insertPosition: Int = rowNum + 1): ExcelRow { val target = targetSheet ?: sheet - if (insertPosition <= sheet.poiSheet.lastRowNum) { + if (insertPosition <= target.poiSheet.lastRowNum) { target.shiftRows(insertPosition, n = 1) } val newPoiRow = target.poiSheet.createRow(insertPosition) @@ -142,11 +142,21 @@ class ExcelRow(val sheet: ExcelSheet, val row: Row) { } fun copyCellsFrom(srcRow: ExcelRow) { + val numMergedRegions = mutableListOf() + srcRow.sheet.poiSheet.mergedRegions?.forEach { address -> + // Only merge region in this single row are supported. + if (address.firstRow == srcRow.rowNum && address.lastRow == srcRow.rowNum) { + numMergedRegions.add(address) + } + } for (colNum in 0..srcRow.lastCellNum) { val srcCell = srcRow.row.getCell(colNum) if (srcCell != null) { val destCell = row.getCell(colNum) ?: row.createCell(colNum) ExcelCell.copyCell(srcCell, destCell) + numMergedRegions.filter { it.firstColumn == srcCell.columnIndex }.forEach { + sheet.addMergeRegion(CellRangeAddress(this.rowNum, this.rowNum, it.firstColumn, it.lastColumn)) + } } } } diff --git a/merlin-core/src/test/kotlin/de/micromata/merlin/excel/ExcelRowActionsTest.kt b/merlin-core/src/test/kotlin/de/micromata/merlin/excel/ExcelRowActionsTest.kt index e9dd3385..795a7caa 100644 --- a/merlin-core/src/test/kotlin/de/micromata/merlin/excel/ExcelRowActionsTest.kt +++ b/merlin-core/src/test/kotlin/de/micromata/merlin/excel/ExcelRowActionsTest.kt @@ -19,6 +19,8 @@ internal class ExcelRowActionsTest { sheet1.getRow(3)!!.copyAndInsert(actionsSheet, 1) Assertions.assertEquals("k.reinhard@acme.com", actionsSheet.getRow(0)!!.getCell(1)!!.stringCellValue) Assertions.assertEquals("b.muster@acme.com", actionsSheet.getRow(1)!!.getCell(1)!!.stringCellValue) + val copiedRow = actionsSheet.getRow(1)!! + Assertions.assertTrue(actionsSheet.poiSheet.mergedRegions?.any { it.firstRow == copiedRow.rowNum && it.lastRow == copiedRow.rowNum && it.firstColumn == 5 && it.lastColumn == 7 } == true) Assertions.assertEquals(0.0, actionsSheet.getRow(2)!!.getCell(0)!!.numericCellValue) Assertions.assertEquals(1.0, actionsSheet.getRow(3)!!.getCell(0)!!.numericCellValue) @@ -34,6 +36,9 @@ internal class ExcelRowActionsTest { Assertions.assertEquals(1.0, actionsSheet.getRow(5)!!.getCell(0)!!.numericCellValue) Assertions.assertEquals(1.5, actionsSheet.getRow(6)!!.getCell(0)!!.numericCellValue) Assertions.assertEquals(2.0, actionsSheet.getRow(7)!!.getCell(0)!!.numericCellValue) + Assertions.assertTrue(actionsSheet.poiSheet.mergedRegions?.any { it.firstRow == newRow.rowNum && it.lastRow == newRow.rowNum && it.firstColumn == 7 && it.lastColumn == 8 } == true) + + sheet1.getRow(3)!!.copyAndInsert(actionsSheet, 14) val file = File(Definitions.OUTPUT_DIR, "RowActionsTest.xlsx") log.info("Writing checksum Excel file: " + file.getAbsolutePath())