Skip to content

Commit

Permalink
Bugfix in copyAndInsert. copyCellsFrom(srcRow) supports now merged re…
Browse files Browse the repository at this point in the history
…gions (inside the current row).
  • Loading branch information
kreinhard committed Feb 6, 2020
1 parent 58f2238 commit f06e90f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
Binary file modified examples/Excel/Workbook-Test.xlsx
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -142,11 +142,21 @@ class ExcelRow(val sheet: ExcelSheet, val row: Row) {
}

fun copyCellsFrom(srcRow: ExcelRow) {
val numMergedRegions = mutableListOf<CellRangeAddress>()
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))
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ internal class ExcelRowActionsTest {
sheet1.getRow(3)!!.copyAndInsert(actionsSheet, 1)
Assertions.assertEquals("[email protected]", actionsSheet.getRow(0)!!.getCell(1)!!.stringCellValue)
Assertions.assertEquals("[email protected]", 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)
Expand All @@ -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())
Expand Down

0 comments on commit f06e90f

Please sign in to comment.