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 f587f022f..ea525149c 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 c44d81617..fa66d76f0 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 7dddca966..4302357c4 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 d74d54ab1..224f5e5c1 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 81154ebb2..d91269ed3 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 f45b79b00..41cb4a2a2 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 d9e470a68..6a08c4b30 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 4aad1e1b1..ca0c126f4 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/ss/util/Region.java similarity index 93% rename from src/java/org/apache/poi/hssf/util/Region.java rename to src/java/org/apache/poi/ss/util/Region.java index 67d52eb13..e44026cde 100644 --- a/src/java/org/apache/poi/hssf/util/Region.java +++ b/src/java/org/apache/poi/ss/util/Region.java @@ -16,7 +16,7 @@ ==================================================================== */ -package org.apache.poi.hssf.util; +package org.apache.poi.ss.util; import org.apache.poi.hssf.record.MergeCellsRecord.MergedRegion; @@ -218,4 +218,14 @@ public class Region 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 fe533b284..e9fd943ba 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 d6106ff20..50ae19b89 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/testcases/org/apache/poi/hssf/usermodel/TestBugs.java b/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java index f05c1d115..119556c22 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 a663602ed..6a0b15158 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 34885e7a2..2c28edf3a 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 5dcc61aa4..d03a3a407 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 13eafa4fd..f3283677b 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 efe726a6a..bf6953e78 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/ooxml/testcases/org/apache/poi/xssf/util/TestCellReference.java b/src/testcases/org/apache/poi/ss/util/TestCellReference.java similarity index 99% rename from src/ooxml/testcases/org/apache/poi/xssf/util/TestCellReference.java rename to src/testcases/org/apache/poi/ss/util/TestCellReference.java index 79a1c3717..f13b22113 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/util/TestCellReference.java +++ b/src/testcases/org/apache/poi/ss/util/TestCellReference.java @@ -15,7 +15,7 @@ limitations under the License. ==================================================================== */ -package org.apache.poi.xssf.util; +package org.apache.poi.ss.util; import org.apache.poi.ss.util.CellReference; 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 000000000..8cd620d38 --- /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)); + } + +}