From: Paolo Mottadelli Date: Sun, 6 Apr 2008 20:34:00 +0000 (+0000) Subject: XSSFSheet addMergedRegion + Region class moved to org.apache.poi.ss.util.Region ... X-Git-Tag: REL_3_5_BETA2~124 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=ba8c1c39a730e7002fd3f983c475be3b8c5015d1;p=poi.git XSSFSheet addMergedRegion + Region class moved to org.apache.poi.ss.util.Region + Region.getRegionRef method + tests git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@645303 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/contrib/src/org/apache/poi/hssf/usermodel/contrib/HSSFRegionUtil.java b/src/contrib/src/org/apache/poi/hssf/usermodel/contrib/HSSFRegionUtil.java index f587f022fe..ea525149c7 100644 --- a/src/contrib/src/org/apache/poi/hssf/usermodel/contrib/HSSFRegionUtil.java +++ b/src/contrib/src/org/apache/poi/hssf/usermodel/contrib/HSSFRegionUtil.java @@ -24,7 +24,7 @@ import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; -import org.apache.poi.hssf.util.Region; +import org.apache.poi.ss.util.Region; import org.apache.commons.lang.exception.NestableException; diff --git a/src/examples/src/org/apache/poi/hssf/usermodel/examples/MergedCells.java b/src/examples/src/org/apache/poi/hssf/usermodel/examples/MergedCells.java index c44d81617f..fa66d76f0a 100644 --- a/src/examples/src/org/apache/poi/hssf/usermodel/examples/MergedCells.java +++ b/src/examples/src/org/apache/poi/hssf/usermodel/examples/MergedCells.java @@ -20,7 +20,7 @@ package org.apache.poi.hssf.usermodel.examples; import org.apache.poi.hssf.usermodel.*; -import org.apache.poi.hssf.util.Region; +import org.apache.poi.ss.util.Region; import java.io.IOException; import java.io.FileOutputStream; diff --git a/src/java/org/apache/poi/hssf/dev/HSSF.java b/src/java/org/apache/poi/hssf/dev/HSSF.java index 7dddca966a..4302357c47 100644 --- a/src/java/org/apache/poi/hssf/dev/HSSF.java +++ b/src/java/org/apache/poi/hssf/dev/HSSF.java @@ -23,6 +23,7 @@ import java.io.FileInputStream; import java.io.FileOutputStream; import org.apache.poi.poifs.filesystem.POIFSFileSystem; +import org.apache.poi.ss.util.Region; import org.apache.poi.hssf.usermodel.*; import org.apache.poi.hssf.util.*; diff --git a/src/java/org/apache/poi/hssf/record/CFHeaderRecord.java b/src/java/org/apache/poi/hssf/record/CFHeaderRecord.java index d74d54ab17..224f5e5c19 100644 --- a/src/java/org/apache/poi/hssf/record/CFHeaderRecord.java +++ b/src/java/org/apache/poi/hssf/record/CFHeaderRecord.java @@ -18,7 +18,7 @@ package org.apache.poi.hssf.record; import org.apache.poi.hssf.record.cf.CellRange; -import org.apache.poi.hssf.util.Region; +import org.apache.poi.ss.util.Region; import org.apache.poi.util.LittleEndian; /** diff --git a/src/java/org/apache/poi/hssf/record/aggregates/CFRecordsAggregate.java b/src/java/org/apache/poi/hssf/record/aggregates/CFRecordsAggregate.java index 81154ebb22..d91269ed34 100644 --- a/src/java/org/apache/poi/hssf/record/aggregates/CFRecordsAggregate.java +++ b/src/java/org/apache/poi/hssf/record/aggregates/CFRecordsAggregate.java @@ -24,7 +24,7 @@ import org.apache.poi.hssf.record.CFHeaderRecord; import org.apache.poi.hssf.record.CFRuleRecord; import org.apache.poi.hssf.record.Record; import org.apache.poi.hssf.record.RecordInputStream; -import org.apache.poi.hssf.util.Region; +import org.apache.poi.ss.util.Region; import org.apache.poi.util.POILogFactory; import org.apache.poi.util.POILogger; diff --git a/src/java/org/apache/poi/hssf/record/cf/CellRange.java b/src/java/org/apache/poi/hssf/record/cf/CellRange.java index f45b79b003..41cb4a2a29 100644 --- a/src/java/org/apache/poi/hssf/record/cf/CellRange.java +++ b/src/java/org/apache/poi/hssf/record/cf/CellRange.java @@ -21,7 +21,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import org.apache.poi.hssf.util.Region; +import org.apache.poi.ss.util.Region; /** * diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFConditionalFormatting.java b/src/java/org/apache/poi/hssf/usermodel/HSSFConditionalFormatting.java index d9e470a68c..6a08c4b304 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFConditionalFormatting.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFConditionalFormatting.java @@ -21,7 +21,7 @@ import org.apache.poi.hssf.record.CFHeaderRecord; import org.apache.poi.hssf.record.CFRuleRecord; import org.apache.poi.hssf.record.aggregates.CFRecordsAggregate; import org.apache.poi.hssf.record.cf.CellRange; -import org.apache.poi.hssf.util.Region; +import org.apache.poi.ss.util.Region; /** * HSSFConditionalFormatting class encapsulates all settings of Conditional Formatting. diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java b/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java index 4aad1e1b18..ca0c126f42 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java @@ -55,9 +55,9 @@ import org.apache.poi.hssf.record.formula.ReferencePtg; import org.apache.poi.hssf.util.HSSFCellRangeAddress; import org.apache.poi.hssf.util.HSSFDataValidation; import org.apache.poi.hssf.util.PaneInformation; -import org.apache.poi.hssf.util.Region; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.util.Region; import org.apache.poi.util.POILogFactory; import org.apache.poi.util.POILogger; diff --git a/src/java/org/apache/poi/hssf/util/Region.java b/src/java/org/apache/poi/hssf/util/Region.java deleted file mode 100644 index 67d52eb137..0000000000 --- a/src/java/org/apache/poi/hssf/util/Region.java +++ /dev/null @@ -1,221 +0,0 @@ -/* ==================================================================== - Licensed to the Apache Software Foundation (ASF) under one or more - contributor license agreements. See the NOTICE file distributed with - this work for additional information regarding copyright ownership. - The ASF licenses this file to You under the Apache License, Version 2.0 - (the "License"); you may not use this file except in compliance with - the License. You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -==================================================================== */ - - -package org.apache.poi.hssf.util; - -import org.apache.poi.hssf.record.MergeCellsRecord.MergedRegion; - -/** - * Represents a from/to row/col square. This is a object primitive - * that can be used to represent row,col - row,col just as one would use String - * to represent a string of characters. Its really only useful for HSSF though. - * - * @author Andrew C. Oliver acoliver at apache dot org - */ - -public class Region - implements Comparable -{ - private int rowFrom; - private short colFrom; - private int rowTo; - private short colTo; - - /** - * Creates a new instance of Region (0,0 - 0,0) - */ - - public Region() - { - } - - public Region(int rowFrom, short colFrom, int rowTo, short colTo) - { - this.rowFrom = rowFrom; - this.rowTo = rowTo; - this.colFrom = colFrom; - this.colTo = colTo; - } - - /** - * special constructor (I know this is bad but it is so wrong that its right - * okay) that makes a region from a mergedcells's region subrecord. - */ - - public Region(MergedRegion region) - { - this(region.row_from, region.col_from, region.row_to, region.col_to); - } - - /** - * get the upper left hand corner column number - * - * @return column number for the upper left hand corner - */ - - public short getColumnFrom() - { - return colFrom; - } - - /** - * get the upper left hand corner row number - * - * @return row number for the upper left hand corner - */ - - public int getRowFrom() - { - return rowFrom; - } - - /** - * get the lower right hand corner column number - * - * @return column number for the lower right hand corner - */ - - public short getColumnTo() - { - return colTo; - } - - /** - * get the lower right hand corner row number - * - * @return row number for the lower right hand corner - */ - - public int getRowTo() - { - return rowTo; - } - - /** - * set the upper left hand corner column number - * - * @param colFrom column number for the upper left hand corner - */ - - public void setColumnFrom(short colFrom) - { - this.colFrom = colFrom; - } - - /** - * set the upper left hand corner row number - * - * @param rowFrom row number for the upper left hand corner - */ - - public void setRowFrom(int rowFrom) - { - this.rowFrom = rowFrom; - } - - /** - * set the lower right hand corner column number - * - * @param colTo column number for the lower right hand corner - */ - - public void setColumnTo(short colTo) - { - this.colTo = colTo; - } - - /** - * get the lower right hand corner row number - * - * @param rowTo row number for the lower right hand corner - */ - - public void setRowTo(int rowTo) - { - this.rowTo = rowTo; - } - - /** - * Answers: "is the row/column inside this range?" - * - * @return true if the cell is in the range and - * false if it is not - */ - - public boolean contains(int row, short col) - { - if ((this.rowFrom <= row) && (this.rowTo >= row) - && (this.colFrom <= col) && (this.colTo >= col)) - { - -// System.out.println("Region ("+rowFrom+","+colFrom+","+rowTo+","+ -// colTo+") does contain "+row+","+col); - return true; - } - return false; - } - - public boolean equals(Region r) - { - return (compareTo(r) == 0); - } - - /** - * Compares that the given region is the same less than or greater than this - * region. If any regional coordiant passed in is less than this regions - * coordinants then a positive integer is returned. Otherwise a negative - * integer is returned. - * - * @param r region - * @see #compareTo(Object) - */ - - public int compareTo(Region r) - { - if ((this.getRowFrom() == r.getRowFrom()) - && (this.getColumnFrom() == r.getColumnFrom()) - && (this.getRowTo() == r.getRowTo()) - && (this.getColumnTo() == r.getColumnTo())) - { - return 0; - } - if ((this.getRowFrom() < r.getRowFrom()) - || (this.getColumnFrom() < r.getColumnFrom()) - || (this.getRowTo() < r.getRowTo()) - || (this.getColumnTo() < r.getColumnTo())) - { - return 1; - } - return -1; - } - - public int compareTo(Object o) - { - return compareTo(( Region ) o); - } - - /** - * @return the area contained by this region (number of cells) - */ - - public int getArea() - { - return ((1 + (getRowTo() - getRowFrom())) - * (1 + (getColumnTo() - getColumnFrom()))); - } -} diff --git a/src/java/org/apache/poi/ss/util/Region.java b/src/java/org/apache/poi/ss/util/Region.java new file mode 100644 index 0000000000..e44026cde2 --- /dev/null +++ b/src/java/org/apache/poi/ss/util/Region.java @@ -0,0 +1,231 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ + + +package org.apache.poi.ss.util; + +import org.apache.poi.hssf.record.MergeCellsRecord.MergedRegion; + +/** + * Represents a from/to row/col square. This is a object primitive + * that can be used to represent row,col - row,col just as one would use String + * to represent a string of characters. Its really only useful for HSSF though. + * + * @author Andrew C. Oliver acoliver at apache dot org + */ + +public class Region + implements Comparable +{ + private int rowFrom; + private short colFrom; + private int rowTo; + private short colTo; + + /** + * Creates a new instance of Region (0,0 - 0,0) + */ + + public Region() + { + } + + public Region(int rowFrom, short colFrom, int rowTo, short colTo) + { + this.rowFrom = rowFrom; + this.rowTo = rowTo; + this.colFrom = colFrom; + this.colTo = colTo; + } + + /** + * special constructor (I know this is bad but it is so wrong that its right + * okay) that makes a region from a mergedcells's region subrecord. + */ + + public Region(MergedRegion region) + { + this(region.row_from, region.col_from, region.row_to, region.col_to); + } + + /** + * get the upper left hand corner column number + * + * @return column number for the upper left hand corner + */ + + public short getColumnFrom() + { + return colFrom; + } + + /** + * get the upper left hand corner row number + * + * @return row number for the upper left hand corner + */ + + public int getRowFrom() + { + return rowFrom; + } + + /** + * get the lower right hand corner column number + * + * @return column number for the lower right hand corner + */ + + public short getColumnTo() + { + return colTo; + } + + /** + * get the lower right hand corner row number + * + * @return row number for the lower right hand corner + */ + + public int getRowTo() + { + return rowTo; + } + + /** + * set the upper left hand corner column number + * + * @param colFrom column number for the upper left hand corner + */ + + public void setColumnFrom(short colFrom) + { + this.colFrom = colFrom; + } + + /** + * set the upper left hand corner row number + * + * @param rowFrom row number for the upper left hand corner + */ + + public void setRowFrom(int rowFrom) + { + this.rowFrom = rowFrom; + } + + /** + * set the lower right hand corner column number + * + * @param colTo column number for the lower right hand corner + */ + + public void setColumnTo(short colTo) + { + this.colTo = colTo; + } + + /** + * get the lower right hand corner row number + * + * @param rowTo row number for the lower right hand corner + */ + + public void setRowTo(int rowTo) + { + this.rowTo = rowTo; + } + + /** + * Answers: "is the row/column inside this range?" + * + * @return true if the cell is in the range and + * false if it is not + */ + + public boolean contains(int row, short col) + { + if ((this.rowFrom <= row) && (this.rowTo >= row) + && (this.colFrom <= col) && (this.colTo >= col)) + { + +// System.out.println("Region ("+rowFrom+","+colFrom+","+rowTo+","+ +// colTo+") does contain "+row+","+col); + return true; + } + return false; + } + + public boolean equals(Region r) + { + return (compareTo(r) == 0); + } + + /** + * Compares that the given region is the same less than or greater than this + * region. If any regional coordiant passed in is less than this regions + * coordinants then a positive integer is returned. Otherwise a negative + * integer is returned. + * + * @param r region + * @see #compareTo(Object) + */ + + public int compareTo(Region r) + { + if ((this.getRowFrom() == r.getRowFrom()) + && (this.getColumnFrom() == r.getColumnFrom()) + && (this.getRowTo() == r.getRowTo()) + && (this.getColumnTo() == r.getColumnTo())) + { + return 0; + } + if ((this.getRowFrom() < r.getRowFrom()) + || (this.getColumnFrom() < r.getColumnFrom()) + || (this.getRowTo() < r.getRowTo()) + || (this.getColumnTo() < r.getColumnTo())) + { + return 1; + } + return -1; + } + + public int compareTo(Object o) + { + return compareTo(( Region ) o); + } + + /** + * @return the area contained by this region (number of cells) + */ + + public int getArea() + { + return ((1 + (getRowTo() - getRowFrom())) + * (1 + (getColumnTo() - getColumnFrom()))); + } + + /** + * @return the string reference for this region + */ + public String getRegionRef() { + CellReference cellRefFrom = new CellReference(rowFrom, colFrom); + CellReference cellRefTo = new CellReference(rowTo, colTo); + String ref = cellRefFrom.formatAsString() + ":" + cellRefTo.formatAsString(); + return ref; + } +} diff --git a/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Sheet.java b/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Sheet.java index fe533b2841..e9fd943ba8 100644 --- a/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Sheet.java +++ b/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Sheet.java @@ -20,7 +20,7 @@ package org.apache.poi.ss.usermodel; import java.util.Iterator; import org.apache.poi.hssf.util.PaneInformation; -import org.apache.poi.hssf.util.Region; +import org.apache.poi.ss.util.Region; public interface Sheet extends Iterable { 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 d6106ff20e..50ae19b896 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java @@ -25,7 +25,6 @@ import java.util.LinkedList; import java.util.List; import org.apache.poi.hssf.util.PaneInformation; -import org.apache.poi.hssf.util.Region; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.Comment; import org.apache.poi.ss.usermodel.CommentsSource; @@ -37,6 +36,7 @@ import org.apache.poi.ss.usermodel.PrintSetup; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.util.CellReference; +import org.apache.poi.ss.util.Region; import org.apache.poi.xssf.model.CommentsTable; import org.apache.poi.xssf.usermodel.helpers.ColumnHelper; import org.apache.xmlbeans.XmlOptions; @@ -48,6 +48,8 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCols; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDialogsheet; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTHeaderFooter; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTHyperlink; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTMergeCell; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTMergeCells; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPageBreak; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPageMargins; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPageSetUpPr; @@ -72,6 +74,7 @@ public class XSSFSheet implements Sheet { protected ColumnHelper columnHelper; protected XSSFWorkbook workbook; protected CommentsSource sheetComments; + protected CTMergeCells ctMergeCells; public static final short LeftMargin = 0; public static final short RightMargin = 1; @@ -189,10 +192,18 @@ public class XSSFSheet implements Sheet { } public int addMergedRegion(Region region) { - // TODO Auto-generated method stub - return 0; + addNewMergeCell(region); + return ctMergeCells.sizeOfMergeCellArray(); } + private void addNewMergeCell(Region region) { + if (ctMergeCells == null) { + ctMergeCells = worksheet.addNewMergeCells(); + } + CTMergeCell ctMergeCell = ctMergeCells.addNewMergeCell(); + ctMergeCell.setRef(region.getRegionRef()); + } + public void autoSizeColumn(short column) { columnHelper.setColBestFit(column, true); } diff --git a/src/ooxml/testcases/org/apache/poi/xssf/util/TestCellReference.java b/src/ooxml/testcases/org/apache/poi/xssf/util/TestCellReference.java deleted file mode 100644 index 79a1c3717b..0000000000 --- a/src/ooxml/testcases/org/apache/poi/xssf/util/TestCellReference.java +++ /dev/null @@ -1,171 +0,0 @@ -/* ==================================================================== - Licensed to the Apache Software Foundation (ASF) under one or more - contributor license agreements. See the NOTICE file distributed with - this work for additional information regarding copyright ownership. - The ASF licenses this file to You under the Apache License, Version 2.0 - (the "License"); you may not use this file except in compliance with - the License. You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -==================================================================== */ - -package org.apache.poi.xssf.util; - -import org.apache.poi.ss.util.CellReference; - -import junit.framework.TestCase; - - -/** - * Tests that the common CellReference works as we need it to - */ -public class TestCellReference extends TestCase { - - public void testGetCellRefParts() { - CellReference cellReference; - String[] parts; - - String cellRef = "A1"; - cellReference = new CellReference(cellRef); - assertEquals(0, cellReference.getCol()); - parts = cellReference.getCellRefParts(); - assertNotNull(parts); - assertEquals(null, parts[0]); - assertEquals("1", parts[1]); - assertEquals("A", parts[2]); - - cellRef = "AA1"; - cellReference = new CellReference(cellRef); - assertEquals(26, cellReference.getCol()); - parts = cellReference.getCellRefParts(); - assertNotNull(parts); - assertEquals(null, parts[0]); - assertEquals("1", parts[1]); - assertEquals("AA", parts[2]); - - cellRef = "AA100"; - cellReference = new CellReference(cellRef); - assertEquals(26, cellReference.getCol()); - parts = cellReference.getCellRefParts(); - assertNotNull(parts); - assertEquals(null, parts[0]); - assertEquals("100", parts[1]); - assertEquals("AA", parts[2]); - - cellRef = "AAA300"; - cellReference = new CellReference(cellRef); - assertEquals(702, cellReference.getCol()); - parts = cellReference.getCellRefParts(); - assertNotNull(parts); - assertEquals(null, parts[0]); - assertEquals("300", parts[1]); - assertEquals("AAA", parts[2]); - - cellRef = "ZZ100521"; - cellReference = new CellReference(cellRef); - assertEquals(26*26+25, cellReference.getCol()); - parts = cellReference.getCellRefParts(); - assertNotNull(parts); - assertEquals(null, parts[0]); - assertEquals("100521", parts[1]); - assertEquals("ZZ", parts[2]); - - cellRef = "ZYX987"; - cellReference = new CellReference(cellRef); - assertEquals(26*26*26 + 25*26 + 24 - 1, cellReference.getCol()); - parts = cellReference.getCellRefParts(); - assertNotNull(parts); - assertEquals(null, parts[0]); - assertEquals("987", parts[1]); - assertEquals("ZYX", parts[2]); - - cellRef = "AABC10065"; - cellReference = new CellReference(cellRef); - parts = cellReference.getCellRefParts(); - assertNotNull(parts); - assertEquals(null, parts[0]); - assertEquals("10065", parts[1]); - assertEquals("AABC", parts[2]); - } - - public void testGetColNumFromRef() { - String cellRef = "A1"; - CellReference cellReference = new CellReference(cellRef); - assertEquals(0, cellReference.getCol()); - - cellRef = "AA1"; - cellReference = new CellReference(cellRef); - assertEquals(26, cellReference.getCol()); - - cellRef = "AB1"; - cellReference = new CellReference(cellRef); - assertEquals(27, cellReference.getCol()); - - cellRef = "BA1"; - cellReference = new CellReference(cellRef); - assertEquals(26+26, cellReference.getCol()); - - cellRef = "CA1"; - cellReference = new CellReference(cellRef); - assertEquals(26+26+26, cellReference.getCol()); - - cellRef = "ZA1"; - cellReference = new CellReference(cellRef); - assertEquals(26*26, cellReference.getCol()); - - cellRef = "ZZ1"; - cellReference = new CellReference(cellRef); - assertEquals(26*26+25, cellReference.getCol()); - - cellRef = "AAA1"; - cellReference = new CellReference(cellRef); - assertEquals(26*26+26, cellReference.getCol()); - - - cellRef = "A1100"; - cellReference = new CellReference(cellRef); - assertEquals(0, cellReference.getCol()); - - cellRef = "BC15"; - cellReference = new CellReference(cellRef); - assertEquals(54, cellReference.getCol()); - } - - public void testGetRowNumFromRef() { - String cellRef = "A1"; - CellReference cellReference = new CellReference(cellRef); - assertEquals(0, cellReference.getRow()); - - cellRef = "A12"; - cellReference = new CellReference(cellRef); - assertEquals(11, cellReference.getRow()); - - cellRef = "AS121"; - cellReference = new CellReference(cellRef); - assertEquals(120, cellReference.getRow()); - } - - public void testConvertNumToColString() { - short col = 702; - String collRef = new CellReference(0, col).formatAsString(); - assertEquals("AAA1", collRef); - - short col2 = 0; - String collRef2 = new CellReference(0, col2).formatAsString(); - assertEquals("A1", collRef2); - - short col3 = 27; - String collRef3 = new CellReference(0, col3).formatAsString(); - assertEquals("AB1", collRef3); - - short col4 = 2080; - String collRef4 = new CellReference(0, col4).formatAsString(); - assertEquals("CBA1", collRef4); - } -} diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java b/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java index f05c1d1157..119556c225 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java @@ -20,7 +20,8 @@ package org.apache.poi.hssf.usermodel; import junit.framework.TestCase; -import org.apache.poi.hssf.util.Region; + +import org.apache.poi.ss.util.Region; import org.apache.poi.util.TempFile; import java.io.*; diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestCloneSheet.java b/src/testcases/org/apache/poi/hssf/usermodel/TestCloneSheet.java index a663602ed3..6a0b151581 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestCloneSheet.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestCloneSheet.java @@ -20,7 +20,8 @@ package org.apache.poi.hssf.usermodel; import junit.framework.TestCase; -import org.apache.poi.hssf.util.Region; + +import org.apache.poi.ss.util.Region; /** * Test the ability to clone a sheet. diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestDataValidation.java b/src/testcases/org/apache/poi/hssf/usermodel/TestDataValidation.java index 34885e7a21..2c28edf3ac 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestDataValidation.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestDataValidation.java @@ -19,6 +19,7 @@ package org.apache.poi.hssf.usermodel; import junit.framework.TestCase; import org.apache.poi.hssf.util.*; +import org.apache.poi.ss.util.Region; import java.io.*; import java.util.*; diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFConditionalFormatting.java b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFConditionalFormatting.java index 5dcc61aa41..d03a3a4071 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFConditionalFormatting.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFConditionalFormatting.java @@ -21,7 +21,7 @@ import junit.framework.TestCase; import org.apache.poi.hssf.record.CFRuleRecord.ComparisonOperator; import org.apache.poi.hssf.util.HSSFColor; -import org.apache.poi.hssf.util.Region; +import org.apache.poi.ss.util.Region; /** * * @author Dmitriy Kumshayev diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFSheet.java b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFSheet.java index 13eafa4fd8..f3283677b9 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFSheet.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFSheet.java @@ -35,8 +35,8 @@ import org.apache.poi.hssf.record.SCLRecord; import org.apache.poi.hssf.record.VCenterRecord; import org.apache.poi.hssf.record.WSBoolRecord; import org.apache.poi.hssf.record.WindowTwoRecord; -import org.apache.poi.hssf.util.Region; import org.apache.poi.poifs.filesystem.POIFSFileSystem; +import org.apache.poi.ss.util.Region; import org.apache.poi.util.TempFile; /** diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestWorkbook.java b/src/testcases/org/apache/poi/hssf/usermodel/TestWorkbook.java index efe726a6a5..bf6953e785 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestWorkbook.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestWorkbook.java @@ -25,8 +25,8 @@ import org.apache.poi.hssf.record.BackupRecord; import org.apache.poi.hssf.record.LabelSSTRecord; import org.apache.poi.hssf.record.Record; import org.apache.poi.hssf.record.aggregates.ValueRecordsAggregate; -import org.apache.poi.hssf.util.Region; import org.apache.poi.poifs.filesystem.POIFSFileSystem; +import org.apache.poi.ss.util.Region; import org.apache.poi.util.TempFile; import java.io.File; diff --git a/src/testcases/org/apache/poi/ss/util/TestCellReference.java b/src/testcases/org/apache/poi/ss/util/TestCellReference.java new file mode 100644 index 0000000000..f13b221134 --- /dev/null +++ b/src/testcases/org/apache/poi/ss/util/TestCellReference.java @@ -0,0 +1,171 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ + +package org.apache.poi.ss.util; + +import org.apache.poi.ss.util.CellReference; + +import junit.framework.TestCase; + + +/** + * Tests that the common CellReference works as we need it to + */ +public class TestCellReference extends TestCase { + + public void testGetCellRefParts() { + CellReference cellReference; + String[] parts; + + String cellRef = "A1"; + cellReference = new CellReference(cellRef); + assertEquals(0, cellReference.getCol()); + parts = cellReference.getCellRefParts(); + assertNotNull(parts); + assertEquals(null, parts[0]); + assertEquals("1", parts[1]); + assertEquals("A", parts[2]); + + cellRef = "AA1"; + cellReference = new CellReference(cellRef); + assertEquals(26, cellReference.getCol()); + parts = cellReference.getCellRefParts(); + assertNotNull(parts); + assertEquals(null, parts[0]); + assertEquals("1", parts[1]); + assertEquals("AA", parts[2]); + + cellRef = "AA100"; + cellReference = new CellReference(cellRef); + assertEquals(26, cellReference.getCol()); + parts = cellReference.getCellRefParts(); + assertNotNull(parts); + assertEquals(null, parts[0]); + assertEquals("100", parts[1]); + assertEquals("AA", parts[2]); + + cellRef = "AAA300"; + cellReference = new CellReference(cellRef); + assertEquals(702, cellReference.getCol()); + parts = cellReference.getCellRefParts(); + assertNotNull(parts); + assertEquals(null, parts[0]); + assertEquals("300", parts[1]); + assertEquals("AAA", parts[2]); + + cellRef = "ZZ100521"; + cellReference = new CellReference(cellRef); + assertEquals(26*26+25, cellReference.getCol()); + parts = cellReference.getCellRefParts(); + assertNotNull(parts); + assertEquals(null, parts[0]); + assertEquals("100521", parts[1]); + assertEquals("ZZ", parts[2]); + + cellRef = "ZYX987"; + cellReference = new CellReference(cellRef); + assertEquals(26*26*26 + 25*26 + 24 - 1, cellReference.getCol()); + parts = cellReference.getCellRefParts(); + assertNotNull(parts); + assertEquals(null, parts[0]); + assertEquals("987", parts[1]); + assertEquals("ZYX", parts[2]); + + cellRef = "AABC10065"; + cellReference = new CellReference(cellRef); + parts = cellReference.getCellRefParts(); + assertNotNull(parts); + assertEquals(null, parts[0]); + assertEquals("10065", parts[1]); + assertEquals("AABC", parts[2]); + } + + public void testGetColNumFromRef() { + String cellRef = "A1"; + CellReference cellReference = new CellReference(cellRef); + assertEquals(0, cellReference.getCol()); + + cellRef = "AA1"; + cellReference = new CellReference(cellRef); + assertEquals(26, cellReference.getCol()); + + cellRef = "AB1"; + cellReference = new CellReference(cellRef); + assertEquals(27, cellReference.getCol()); + + cellRef = "BA1"; + cellReference = new CellReference(cellRef); + assertEquals(26+26, cellReference.getCol()); + + cellRef = "CA1"; + cellReference = new CellReference(cellRef); + assertEquals(26+26+26, cellReference.getCol()); + + cellRef = "ZA1"; + cellReference = new CellReference(cellRef); + assertEquals(26*26, cellReference.getCol()); + + cellRef = "ZZ1"; + cellReference = new CellReference(cellRef); + assertEquals(26*26+25, cellReference.getCol()); + + cellRef = "AAA1"; + cellReference = new CellReference(cellRef); + assertEquals(26*26+26, cellReference.getCol()); + + + cellRef = "A1100"; + cellReference = new CellReference(cellRef); + assertEquals(0, cellReference.getCol()); + + cellRef = "BC15"; + cellReference = new CellReference(cellRef); + assertEquals(54, cellReference.getCol()); + } + + public void testGetRowNumFromRef() { + String cellRef = "A1"; + CellReference cellReference = new CellReference(cellRef); + assertEquals(0, cellReference.getRow()); + + cellRef = "A12"; + cellReference = new CellReference(cellRef); + assertEquals(11, cellReference.getRow()); + + cellRef = "AS121"; + cellReference = new CellReference(cellRef); + assertEquals(120, cellReference.getRow()); + } + + public void testConvertNumToColString() { + short col = 702; + String collRef = new CellReference(0, col).formatAsString(); + assertEquals("AAA1", collRef); + + short col2 = 0; + String collRef2 = new CellReference(0, col2).formatAsString(); + assertEquals("A1", collRef2); + + short col3 = 27; + String collRef3 = new CellReference(0, col3).formatAsString(); + assertEquals("AB1", collRef3); + + short col4 = 2080; + String collRef4 = new CellReference(0, col4).formatAsString(); + assertEquals("CBA1", collRef4); + } +} diff --git a/src/testcases/org/apache/poi/ss/util/TestRegion.java b/src/testcases/org/apache/poi/ss/util/TestRegion.java new file mode 100644 index 0000000000..8cd620d38c --- /dev/null +++ b/src/testcases/org/apache/poi/ss/util/TestRegion.java @@ -0,0 +1,49 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ + +package org.apache.poi.ss.util; + +import junit.framework.TestCase; + + +/** + * Tests that the common CellReference works as we need it to + */ +public class TestRegion extends TestCase { + + public void testGetRegionRef() { + int rowFrom = 3; + short colFrom = 3; + int rowTo = 9; + short colTo = 9; + Region region = new Region(rowFrom, colFrom, rowTo, colTo); + assertEquals("D4:J10", region.getRegionRef()); + } + + public void testContains() { + int rowFrom = 3; + short colFrom = 3; + int rowTo = 9; + short colTo = 9; + Region region = new Region(rowFrom, colFrom, rowTo, colTo); + assertEquals("D4:J10", region.getRegionRef()); + assertTrue(region.contains(5, (short) 7)); + assertTrue(region.contains(9, (short) 9)); + assertFalse(region.contains(9, (short) 10)); + } + +}