diff options
author | PJ Fanning <fanningpj@apache.org> | 2020-05-15 19:46:17 +0000 |
---|---|---|
committer | PJ Fanning <fanningpj@apache.org> | 2020-05-15 19:46:17 +0000 |
commit | 9bddb8729eb517f18b47d4e9eb6ed5495631462d (patch) | |
tree | f12c6471d6e88b22417b91f7bf618dad12a5e05d /src/testcases/org | |
parent | 237d55b3e39f78161d55f411bad380b1acff38d6 (diff) | |
download | poi-9bddb8729eb517f18b47d4e9eb6ed5495631462d.tar.gz poi-9bddb8729eb517f18b47d4e9eb6ed5495631462d.zip |
[github-179] add an option for RangeCopier.copyRange() to clone styles. Thanks to xjlin0. This closes #179
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1877792 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/testcases/org')
-rw-r--r-- | src/testcases/org/apache/poi/ss/usermodel/TestRangeCopier.java | 54 |
1 files changed, 49 insertions, 5 deletions
diff --git a/src/testcases/org/apache/poi/ss/usermodel/TestRangeCopier.java b/src/testcases/org/apache/poi/ss/usermodel/TestRangeCopier.java index d00a820e3c..929abc35d4 100644 --- a/src/testcases/org/apache/poi/ss/usermodel/TestRangeCopier.java +++ b/src/testcases/org/apache/poi/ss/usermodel/TestRangeCopier.java @@ -19,14 +19,14 @@ package org.apache.poi.ss.usermodel; -import static org.junit.Assert.assertEquals; - import org.junit.Ignore; import org.junit.Test; import org.apache.poi.ss.ITestDataProvider; import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.ss.util.CellReference; +import static org.junit.Assert.*; + @Ignore public abstract class TestRangeCopier { protected Sheet sheet1; @@ -116,13 +116,57 @@ public abstract class TestRangeCopier { assertEquals("1.2", getCellContent(sheet1, "G24")); } - protected static String getCellContent(Sheet sheet, String coordinates) { + @Test + public void testCopyStyles() { + String cellContent = "D6 aligned to the right"; + HorizontalAlignment toTheRight = HorizontalAlignment.RIGHT; + // create cell with content aligned to the right + CellStyle style = workbook.createCellStyle(); + style.setAlignment(toTheRight); + Cell cell = sheet1.createRow(5).createCell(3); + cell.setCellValue(cellContent); + cell.setCellStyle(style); + + Sheet destSheet = sheet2; + CellRangeAddress tileRange = CellRangeAddress.valueOf("D6:D6"); // on sheet1 + CellRangeAddress destRange = CellRangeAddress.valueOf("J6:J6"); // on sheet2 + transSheetRangeCopier.copyRange(tileRange, destRange, true, false); + assertEquals(cellContent, getCellContent(destSheet, "J6")); + assertEquals(toTheRight, getCell(destSheet, "J6").getCellStyle().getAlignment()); + } + + @Test + public void testMergedRanges() { + String cellContent = "D6 merged to E7"; + + // create cell merged from D6 to E7 + CellRangeAddress mergedRangeAddress = new CellRangeAddress(5,6,3,4); + Cell cell = sheet1.createRow(5).createCell(3); + cell.setCellValue(cellContent); + sheet1.addMergedRegion(mergedRangeAddress); + + Sheet destSheet = sheet2; + CellRangeAddress tileRange = CellRangeAddress.valueOf("D6:E7"); // on sheet1 + transSheetRangeCopier.copyRange(tileRange, tileRange, false, true); + assertEquals(cellContent, getCellContent(destSheet, "D6")); + assertFalse(destSheet.getMergedRegions().isEmpty()); + destSheet.getMergedRegions().forEach((mergedRegion) -> { + assertTrue(mergedRegion.equals(mergedRangeAddress)); + }); + } + + protected static String getCellContent(Sheet sheet, String coordinates) { + Cell cell = getCell(sheet, coordinates); + return cell == null ? "" : cell.toString(); + } + + protected static Cell getCell(Sheet sheet, String coordinates) { try { CellReference p = new CellReference(coordinates); - return sheet.getRow(p.getRow()).getCell(p.getCol()).toString(); + return sheet.getRow(p.getRow()).getCell(p.getCol()); } catch (NullPointerException e) { // row or cell does not exist - return ""; + return null; } } } |