From 3c1c5bac8329677a4587d912d30b3ffd43e9aeeb 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 5dd4a52fb..6d39cff44 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 08d42db6c..031159904 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 a89b14cc6..da73fa8d5 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);