]> source.dussan.org Git - poi.git/commitdiff
XSSFSheet getNumMergedRegions, getMergedRegionAt methods + tests
authorPaolo Mottadelli <paolo@apache.org>
Mon, 7 Apr 2008 20:31:43 +0000 (20:31 +0000)
committerPaolo Mottadelli <paolo@apache.org>
Mon, 7 Apr 2008 20:31:43 +0000 (20:31 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@645666 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/ss/util/Region.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java
src/testcases/org/apache/poi/ss/util/TestRegion.java

index e44026cde25c0c9dbc69e6998675ac6e865f57fd..71dd31403b120b3da53b96482ceb1a0f955c3771 100644 (file)
@@ -62,7 +62,16 @@ public class Region
         this(region.row_from, region.col_from, region.row_to, region.col_to);
     }
 
-    /**
+    public Region(String ref) {
+       CellReference cellReferenceFrom = new CellReference(ref.substring(0, ref.indexOf(":")));
+       CellReference cellReferenceTo = new CellReference(ref.substring(ref.indexOf(":") + 1));
+       this.rowFrom = cellReferenceFrom.getRow();
+       this.colFrom = (short) cellReferenceFrom.getCol();
+       this.rowTo = cellReferenceTo.getRow();
+       this.colTo = (short) cellReferenceTo.getCol();
+       }
+
+       /**
      * get the upper left hand corner column number
      *
      * @return column number for the upper left hand corner
index 96ffe3c03a428a3db67aaa1a25b9c73bd49f9274..08d42db6cd9c785940aa0a2b435c651cb0be20d3 100644 (file)
@@ -458,13 +458,12 @@ public class XSSFSheet implements Sheet {
        }
 
     public Region getMergedRegionAt(int index) {
-        // TODO Auto-generated method stub
-        return null;
+       CTMergeCell ctMergeCell = getMergedCells().getMergeCellArray(index);
+       return new Region(ctMergeCell.getRef());
     }
 
     public int getNumMergedRegions() {
-        // TODO Auto-generated method stub
-        return 0;
+       return getMergedCells().sizeOfMergeCellArray();
     }
     
     public int getNumHyperlinks() {
@@ -990,13 +989,18 @@ public class XSSFSheet implements Sheet {
        }
 
        private void addNewMergeCell(Region region) {
-       if (ctMergeCells == null) {
-               ctMergeCells = worksheet.addNewMergeCells();
-       }
+       ctMergeCells = getMergedCells();
        CTMergeCell ctMergeCell = ctMergeCells.addNewMergeCell();
        ctMergeCell.setRef(region.getRegionRef());
        }
 
+       private CTMergeCells getMergedCells() {
+               if (ctMergeCells == null) {
+                       ctMergeCells = worksheet.addNewMergeCells();
+       }
+               return ctMergeCells;
+       }
+
        private CTPageSetUpPr getSheetTypePageSetUpPr() {
        if (getSheetTypeSheetPr().getPageSetUpPr() == null) {
                getSheetTypeSheetPr().setPageSetUpPr(CTPageSetUpPr.Factory.newInstance());
index e92d5d06574e27117d6d65886422cbfed6a54e8c..a89b14cc6b4e814448800fc43c5e5b958b3ee25d 100644 (file)
@@ -23,6 +23,7 @@ import org.apache.poi.ss.usermodel.Cell;
 import org.apache.poi.ss.usermodel.Row;
 import org.apache.poi.ss.usermodel.Sheet;
 import org.apache.poi.ss.usermodel.Workbook;
+import org.apache.poi.ss.util.Region;
 import org.apache.poi.xssf.model.CommentsTable;
 import org.apache.poi.xssf.usermodel.helpers.ColumnHelper;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCol;
@@ -548,6 +549,27 @@ public class TestXSSFSheet extends TestCase {
        assertEquals(STPane.BOTTOM_RIGHT, ctWorksheet.getSheetViews().getSheetViewArray(0).getPane().getActivePane());
     }
     
+    public void testNewMergedRegionAt() {
+       Workbook workbook = new XSSFWorkbook();
+       CTSheet ctSheet = CTSheet.Factory.newInstance();
+       CTWorksheet ctWorksheet = CTWorksheet.Factory.newInstance();
+       XSSFSheet sheet = new XSSFSheet(ctSheet, ctWorksheet, (XSSFWorkbook) workbook);
+       Region region = new Region("B2:D4");
+       sheet.addMergedRegion(region);
+       assertEquals("B2:D4", sheet.getMergedRegionAt(0).getRegionRef());
+    }
+    
+    public void testGetNumMergedRegions() {
+       Workbook workbook = new XSSFWorkbook();
+       CTSheet ctSheet = CTSheet.Factory.newInstance();
+       CTWorksheet ctWorksheet = CTWorksheet.Factory.newInstance();
+       XSSFSheet sheet = new XSSFSheet(ctSheet, ctWorksheet, (XSSFWorkbook) workbook);
+       assertEquals(0, sheet.getNumMergedRegions());
+       Region region = new Region("B2:D4");
+       sheet.addMergedRegion(region);
+       assertEquals(1, sheet.getNumMergedRegions());
+    }
+    
 
        private XSSFSheet createSheet(XSSFWorkbook workbook, String name) {
         XSSFSheet sheet = (XSSFSheet) workbook.createSheet(name);
index 8cd620d38cba5f63361aa06669c5f5bc4a78fec5..e05b072c3488574616c11b9849e05325a4c0032e 100644 (file)
@@ -46,4 +46,10 @@ public class TestRegion extends TestCase {
                assertFalse(region.contains(9, (short) 10));\r
        }\r
        \r
+       public void testConstructors() {\r
+               Region region_1 = new Region("A1:E7");\r
+               assertEquals(0, region_1.getColumnFrom());\r
+               assertEquals((short)4, region_1.getColumnTo());\r
+       }\r
+       \r
 }\r