From 94f623706efbc315fb6950e31a1abb2a98804f30 Mon Sep 17 00:00:00 2001 From: Paolo Mottadelli Date: Wed, 9 Apr 2008 20:50:55 +0000 Subject: [PATCH] XSSFSheet removeMergedRegion + tests git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@646527 13f79535-47bb-0310-9956-ffa450edef68 --- build.xml | 9 ++++++-- .../apache/poi/xssf/usermodel/XSSFSheet.java | 13 +++++++++--- .../poi/xssf/usermodel/TestXSSFSheet.java | 21 +++++++++++++++++++ 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/build.xml b/build.xml index 5dd4a52fbc..6d39cff448 100644 --- a/build.xml +++ b/build.xml @@ -202,6 +202,7 @@ under the License. + @@ -234,6 +235,7 @@ under the License. + @@ -553,7 +555,7 @@ under the License. - @@ -585,6 +587,8 @@ under the License. + + @@ -792,6 +796,7 @@ under the License. + @@ -1315,7 +1320,7 @@ FORREST_HOME environment variable! - + diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java index 08d42db6cd..0311599046 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java @@ -65,7 +65,6 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheetView; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheetViews; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet; import org.openxmlformats.schemas.spreadsheetml.x2006.main.STPane; -import org.openxmlformats.schemas.spreadsheetml.x2006.main.STPaneState; public class XSSFSheet implements Sheet { @@ -645,8 +644,16 @@ public class XSSFSheet implements Sheet { } public void removeMergedRegion(int index) { - // TODO Auto-generated method stub - + CTMergeCell[] mergeCellsArray = new CTMergeCell[getMergedCells().sizeOfMergeCellArray() - 1]; + for (int i = 0 ; i < getMergedCells().sizeOfMergeCellArray() ; i++) { + if (i < index) { + mergeCellsArray[i] = getMergedCells().getMergeCellArray(i); + } + else if (i > index) { + mergeCellsArray[i - 1] = getMergedCells().getMergeCellArray(i); + } + } + getMergedCells().setMergeCellArray(mergeCellsArray); } public void removeRow(Row row) { diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java index a89b14cc6b..da73fa8d51 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java @@ -570,6 +570,27 @@ public class TestXSSFSheet extends TestCase { assertEquals(1, sheet.getNumMergedRegions()); } + public void testRemoveMergedRegion() { + Workbook workbook = new XSSFWorkbook(); + CTSheet ctSheet = CTSheet.Factory.newInstance(); + CTWorksheet ctWorksheet = CTWorksheet.Factory.newInstance(); + XSSFSheet sheet = new XSSFSheet(ctSheet, ctWorksheet, (XSSFWorkbook) workbook); + Region region_1 = new Region("A1:B2"); + Region region_2 = new Region("C3:D4"); + Region region_3 = new Region("E5:F6"); + sheet.addMergedRegion(region_1); + sheet.addMergedRegion(region_2); + sheet.addMergedRegion(region_3); + assertEquals("C3:D4", ctWorksheet.getMergeCells().getMergeCellArray(1).getRef()); + assertEquals(3, sheet.getNumMergedRegions()); + sheet.removeMergedRegion(1); + assertEquals("E5:F6", ctWorksheet.getMergeCells().getMergeCellArray(1).getRef()); + assertEquals(2, sheet.getNumMergedRegions()); + sheet.removeMergedRegion(1); + sheet.removeMergedRegion(0); + assertEquals(0, sheet.getNumMergedRegions()); + } + private XSSFSheet createSheet(XSSFWorkbook workbook, String name) { XSSFSheet sheet = (XSSFSheet) workbook.createSheet(name); -- 2.39.5