diff --git a/build.xml b/build.xml
index d3623395d..bdf0033f3 100644
--- a/build.xml
+++ b/build.xml
@@ -78,7 +78,7 @@ under the License.
JVM system properties for running tests,
user.language and user.country are required as we have locale-sensitive formatters
-->
-
+
diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFPivotCacheDefinition.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFPivotCacheDefinition.java
index 8ab465aff..26735c782 100644
--- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFPivotCacheDefinition.java
+++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFPivotCacheDefinition.java
@@ -28,10 +28,13 @@ import javax.xml.namespace.QName;
import org.apache.poi.POIXMLDocumentPart;
import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.openxml4j.opc.PackageRelationship;
+import org.apache.poi.ss.SpreadsheetVersion;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
+import org.apache.poi.ss.usermodel.Name;
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.AreaReference;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.util.Beta;
@@ -41,6 +44,7 @@ import org.apache.xmlbeans.XmlOptions;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCacheField;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCacheFields;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPivotCacheDefinition;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheetSource;
public class XSSFPivotCacheDefinition extends POIXMLDocumentPart{
@@ -116,6 +120,40 @@ public class XSSFPivotCacheDefinition extends POIXMLDocumentPart{
out.close();
}
+ /**
+ * Find the 2D base data area for the pivot table, either from its direct reference or named table/range.
+ * @return AreaReference representing the current area defined by the pivot table
+ * @throws IllegalArgumentException if the ref attribute is not contiguous or the name attribute is not found.
+ */
+ @Beta
+ public AreaReference getPivotArea(Workbook wb) throws IllegalArgumentException {
+ final CTWorksheetSource wsSource = ctPivotCacheDefinition.getCacheSource().getWorksheetSource();
+
+ final String ref = wsSource.getRef();
+ final String name = wsSource.getName();
+
+ if (ref == null && name == null) throw new IllegalArgumentException("Pivot cache must reference an area, named range, or table.");
+
+ // this is the XML format, so tell the reference that.
+ if (ref != null) return new AreaReference(ref, SpreadsheetVersion.EXCEL2007);
+
+ if (name != null) {
+ // named range or table?
+ final Name range = wb.getName(name);
+ if (range != null) return new AreaReference(range.getRefersToFormula(), SpreadsheetVersion.EXCEL2007);
+ // not a named range, check for a table.
+ // do this second, as tables are sheet-specific, but named ranges are not, and may not have a sheet name given.
+ final XSSFSheet sheet = (XSSFSheet) wb.getSheet(wsSource.getSheet());
+ for (XSSFTable table : sheet.getTables()) {
+ if (table.getName().equals(name)) { //case-sensitive?
+ return new AreaReference(table.getStartCellReference(), table.getEndCellReference());
+ }
+ }
+ }
+
+ throw new IllegalArgumentException("Name '" + name + "' was not found.");
+ }
+
/**
* Generates a cache field for each column in the reference area for the pivot table.
* @param sheet The sheet where the data i collected from
@@ -123,7 +161,7 @@ public class XSSFPivotCacheDefinition extends POIXMLDocumentPart{
@Beta
protected void createCacheFields(Sheet sheet) {
//Get values for start row, start and end column
- AreaReference ar = new AreaReference(ctPivotCacheDefinition.getCacheSource().getWorksheetSource().getRef());
+ AreaReference ar = getPivotArea(sheet.getWorkbook());
CellReference firstCell = ar.getFirstCell();
CellReference lastCell = ar.getLastCell();
int columnStart = firstCell.getCol();
diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFPivotTable.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFPivotTable.java
index bf85f65de..e08af900b 100644
--- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFPivotTable.java
+++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFPivotTable.java
@@ -30,11 +30,11 @@ import javax.xml.namespace.QName;
import org.apache.poi.POIXMLDocumentPart;
import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.openxml4j.opc.PackageRelationship;
-import org.apache.poi.ss.SpreadsheetVersion;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.DataConsolidateFunction;
import org.apache.poi.ss.usermodel.Sheet;
+import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.AreaReference;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.util.Beta;
@@ -214,13 +214,8 @@ public class XSSFPivotTable extends POIXMLDocumentPart {
}
protected AreaReference getPivotArea() {
- AreaReference pivotArea = new AreaReference(
- getPivotCacheDefinition()
- .getCTPivotCacheDefinition()
- .getCacheSource()
- .getWorksheetSource()
- .getRef(),
- SpreadsheetVersion.EXCEL2007);
+ final Workbook wb = getDataSheet().getWorkbook();
+ AreaReference pivotArea = getPivotCacheDefinition().getPivotArea(wb);
return pivotArea;
}
@@ -419,12 +414,14 @@ public class XSSFPivotTable extends POIXMLDocumentPart {
/**
* Creates cacheSource and workSheetSource for pivot table and sets the source reference as well assets the location of the pivot table
- * @param source Source for data for pivot table
+ * @param sourceRef Source for data for pivot table - mutually exclusive with sourceName
+ * @param sourceName Source for data for pivot table - mutually exclusive with sourceRef
* @param position Position for pivot table in sheet
* @param sourceSheet Sheet where the source will be collected from
*/
@Beta
- protected void createSourceReferences(AreaReference source, CellReference position, Sheet sourceSheet){
+ protected void createSourceReferences(CellReference position, Sheet sourceSheet, PivotTableReferenceConfigurator refConfig){
+
//Get cell one to the right and one down from position, add both to AreaReference and set pivot table location.
AreaReference destination = new AreaReference(position, new CellReference(position.getRow()+1, position.getCol()+1));
@@ -448,9 +445,8 @@ public class XSSFPivotTable extends POIXMLDocumentPart {
worksheetSource.setSheet(sourceSheet.getSheetName());
setDataSheet(sourceSheet);
- String[] firstCell = source.getFirstCell().getCellRefParts();
- String[] lastCell = source.getLastCell().getCellRefParts();
- worksheetSource.setRef(firstCell[2]+firstCell[1]+':'+lastCell[2]+lastCell[1]);
+ refConfig.configureReference(worksheetSource);
+ if (worksheetSource.getName() == null && worksheetSource.getRef() == null) throw new IllegalArgumentException("Pivot table source area reference or name must be specified.");
}
@Beta
@@ -465,11 +461,20 @@ public class XSSFPivotTable extends POIXMLDocumentPart {
int firstColumn = sourceArea.getFirstCell().getCol();
int lastColumn = sourceArea.getLastCell().getCol();
CTPivotField pivotField;
- for(int i = 0; i<=lastColumn-firstColumn; i++) {
+ for(int i = firstColumn; i<=lastColumn; i++) {
pivotField = pivotFields.addNewPivotField();
pivotField.setDataField(false);
pivotField.setShowAll(false);
}
pivotFields.setCount(pivotFields.sizeOfPivotFieldArray());
}
+
+ protected static interface PivotTableReferenceConfigurator {
+
+ /**
+ * Configure the name or area reference for the pivot table
+ * @param wsSource CTWorksheetSource that needs the pivot source reference assignment
+ */
+ public void configureReference(CTWorksheetSource wsSource);
+ }
}
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 d72dca445..8b9746489 100644
--- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java
+++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java
@@ -64,8 +64,10 @@ import org.apache.poi.ss.usermodel.Footer;
import org.apache.poi.ss.usermodel.Header;
import org.apache.poi.ss.usermodel.IgnoredErrorType;
import org.apache.poi.ss.usermodel.IndexedColors;
+import org.apache.poi.ss.usermodel.Name;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
+import org.apache.poi.ss.usermodel.Table;
import org.apache.poi.ss.util.AreaReference;
import org.apache.poi.ss.util.CellAddress;
import org.apache.poi.ss.util.CellRangeAddress;
@@ -80,6 +82,7 @@ import org.apache.poi.util.POILogFactory;
import org.apache.poi.util.POILogger;
import org.apache.poi.util.Removal;
import org.apache.poi.xssf.model.CommentsTable;
+import org.apache.poi.xssf.usermodel.XSSFPivotTable.PivotTableReferenceConfigurator;
import org.apache.poi.xssf.usermodel.helpers.ColumnHelper;
import org.apache.poi.xssf.usermodel.helpers.XSSFIgnoredErrorHelper;
import org.apache.poi.xssf.usermodel.helpers.XSSFRowShifter;
@@ -4158,27 +4161,56 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
}
/**
- * Create a pivot table and set area of source, source sheet and a position for pivot table
- * @param source Area from where data will be collected
- * @param position A reference to the cell where the table will start
- * @param sourceSheet The sheet where source will be collected from
+ * Create a pivot table using the AreaReference range on sourceSheet, at the given position.
+ * If the source reference contains a sheet name, it must match the sourceSheet
+ * @param source location of pivot data
+ * @param position A reference to the top left cell where the pivot table will start
+ * @param sourceSheet The sheet containing the source data, if the source reference doesn't contain a sheet name
+ * @throws IllegalArgumentException if source references a sheet different than sourceSheet
* @return The pivot table
*/
@Beta
- public XSSFPivotTable createPivotTable(AreaReference source, CellReference position, Sheet sourceSheet) {
+ public XSSFPivotTable createPivotTable(final AreaReference source, CellReference position, Sheet sourceSheet) {
final String sourceSheetName = source.getFirstCell().getSheetName();
if(sourceSheetName != null && !sourceSheetName.equalsIgnoreCase(sourceSheet.getSheetName())) {
throw new IllegalArgumentException("The area is referenced in another sheet than the "
+ "defined source sheet " + sourceSheet.getSheetName() + ".");
}
+
+ return createPivotTable(position, sourceSheet, new PivotTableReferenceConfigurator() {
+ public void configureReference(CTWorksheetSource wsSource) {
+ final String[] firstCell = source.getFirstCell().getCellRefParts();
+ final String firstRow = firstCell[1];
+ final String firstCol = firstCell[2];
+ final String[] lastCell = source.getLastCell().getCellRefParts();
+ final String lastRow = lastCell[1];
+ final String lastCol = lastCell[2];
+ final String ref = firstCol+firstRow+':'+lastCol+lastRow; //or just source.formatAsString()
+ wsSource.setRef(ref);
+ }
+ });
+ }
+
+ /**
+ * Create a pivot table using the AreaReference or named/table range on sourceSheet, at the given position.
+ * If the source reference contains a sheet name, it must match the sourceSheet.
+ * @param sourceRef location of pivot data - mutually exclusive with SourceName
+ * @param sourceName range or table name for pivot data - mutually exclusive with SourceRef
+ * @param position A reference to the top left cell where the pivot table will start
+ * @param sourceSheet The sheet containing the source data, if the source reference doesn't contain a sheet name
+ * @throws IllegalArgumentException if source references a sheet different than sourceSheet
+ * @return The pivot table
+ */
+ private XSSFPivotTable createPivotTable(CellReference position, Sheet sourceSheet, PivotTableReferenceConfigurator refConfig) {
+
XSSFPivotTable pivotTable = createPivotTable();
//Creates default settings for the pivot table
pivotTable.setDefaultPivotTableDefinition();
//Set sources and references
- pivotTable.createSourceReferences(source, position, sourceSheet);
+ pivotTable.createSourceReferences(position, sourceSheet, refConfig);
- //Create cachefield/s and empty SharedItems
+ //Create cachefield/s and empty SharedItems - must be after creating references
pivotTable.getPivotCacheDefinition().createCacheFields(sourceSheet);
pivotTable.createDefaultDataColumns();
@@ -4186,9 +4218,10 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
}
/**
- * Create a pivot table and set area of source and a position for pivot table
- * @param source Area from where data will be collected
- * @param position A reference to the cell where the table will start
+ * Create a pivot table using the AreaReference range, at the given position.
+ * If the source reference contains a sheet name, that sheet is used, otherwise this sheet is assumed as the source sheet.
+ * @param source location of pivot data
+ * @param position A reference to the top left cell where the pivot table will start
* @return The pivot table
*/
@Beta
@@ -4201,6 +4234,57 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
return createPivotTable(source, position, this);
}
+ /**
+ * Create a pivot table using the Name range reference on sourceSheet, at the given position.
+ * If the source reference contains a sheet name, it must match the sourceSheet
+ * @param source location of pivot data
+ * @param position A reference to the top left cell where the pivot table will start
+ * @param sourceSheet The sheet containing the source data, if the source reference doesn't contain a sheet name
+ * @throws IllegalArgumentException if source references a sheet different than sourceSheet
+ * @return The pivot table
+ */
+ @Beta
+ public XSSFPivotTable createPivotTable(final Name source, CellReference position, Sheet sourceSheet) {
+ if(source.getSheetName() != null && !source.getSheetName().equals(sourceSheet.getSheetName())) {
+ throw new IllegalArgumentException("The named range references another sheet than the "
+ + "defined source sheet " + sourceSheet.getSheetName() + ".");
+ }
+
+ return createPivotTable(position, sourceSheet, new PivotTableReferenceConfigurator() {
+ public void configureReference(CTWorksheetSource wsSource) {
+ wsSource.setName(source.getNameName());
+ }
+ });
+ }
+
+ /**
+ * Create a pivot table using the Name range, at the given position.
+ * If the source reference contains a sheet name, that sheet is used, otherwise this sheet is assumed as the source sheet.
+ * @param source location of pivot data
+ * @param position A reference to the top left cell where the pivot table will start
+ * @return The pivot table
+ */
+ @Beta
+ public XSSFPivotTable createPivotTable(Name source, CellReference position) {
+ return createPivotTable(source, position, getWorkbook().getSheet(source.getSheetName()));
+ }
+
+ /**
+ * Create a pivot table using the Table, at the given position.
+ * Tables are required to have a sheet reference, so no additional logic around reference sheet is needed.
+ * @param source location of pivot data
+ * @param position A reference to the top left cell where the pivot table will start
+ * @return The pivot table
+ */
+ @Beta
+ public XSSFPivotTable createPivotTable(final Table source, CellReference position) {
+ return createPivotTable(position, getWorkbook().getSheet(source.getSheetName()), new PivotTableReferenceConfigurator() {
+ public void configureReference(CTWorksheetSource wsSource) {
+ wsSource.setName(source.getName());
+ }
+ });
+ }
+
/**
* Returns all the pivot tables for this Sheet
*/
diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFPivotTable.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/BaseTestXSSFPivotTable.java
similarity index 74%
rename from src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFPivotTable.java
rename to src/ooxml/testcases/org/apache/poi/xssf/usermodel/BaseTestXSSFPivotTable.java
index 851ca33d6..445812566 100644
--- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFPivotTable.java
+++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/BaseTestXSSFPivotTable.java
@@ -16,16 +16,13 @@
==================================================================== */
package org.apache.poi.xssf.usermodel;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.fail;
+import static org.junit.Assert.*;
import java.io.IOException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.DataConsolidateFunction;
-import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.util.AreaReference;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.xssf.XSSFITestDataProvider;
@@ -38,97 +35,27 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPivotFields;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPivotTableDefinition;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STDataConsolidateFunction;
-public class TestXSSFPivotTable {
+public abstract class BaseTestXSSFPivotTable {
private static final XSSFITestDataProvider _testDataProvider = XSSFITestDataProvider.instance;
- private XSSFWorkbook wb;
- private XSSFPivotTable pivotTable;
- private XSSFPivotTable offsetPivotTable;
- private Cell offsetOuterCell;
+ protected XSSFWorkbook wb;
+ protected XSSFPivotTable pivotTable;
+ protected XSSFPivotTable offsetPivotTable;
+ protected Cell offsetOuterCell;
+ /**
+ * required to set up the test pivot tables and cell reference, either by name or reference.
+ * @see junit.framework.TestCase#setUp()
+ */
@Before
- public void setUp(){
- wb = new XSSFWorkbook();
- XSSFSheet sheet = wb.createSheet();
-
- Row row1 = sheet.createRow(0);
- // Create a cell and put a value in it.
- Cell cell = row1.createCell(0);
- cell.setCellValue("Names");
- Cell cell2 = row1.createCell(1);
- cell2.setCellValue("#");
- Cell cell7 = row1.createCell(2);
- cell7.setCellValue("Data");
- Cell cell10 = row1.createCell(3);
- cell10.setCellValue("Value");
-
- Row row2 = sheet.createRow(1);
- Cell cell3 = row2.createCell(0);
- cell3.setCellValue("Jan");
- Cell cell4 = row2.createCell(1);
- cell4.setCellValue(10);
- Cell cell8 = row2.createCell(2);
- cell8.setCellValue("Apa");
- Cell cell11 = row1.createCell(3);
- cell11.setCellValue(11.11);
-
- Row row3 = sheet.createRow(2);
- Cell cell5 = row3.createCell(0);
- cell5.setCellValue("Ben");
- Cell cell6 = row3.createCell(1);
- cell6.setCellValue(9);
- Cell cell9 = row3.createCell(2);
- cell9.setCellValue("Bepa");
- Cell cell12 = row1.createCell(3);
- cell12.setCellValue(12.12);
-
- AreaReference source = new AreaReference("A1:C2", _testDataProvider.getSpreadsheetVersion());
- pivotTable = sheet.createPivotTable(source, new CellReference("H5"));
-
- XSSFSheet offsetSheet = wb.createSheet();
-
- Row tableRow_1 = offsetSheet.createRow(1);
- offsetOuterCell = tableRow_1.createCell(1);
- offsetOuterCell.setCellValue(-1);
- Cell tableCell_1_1 = tableRow_1.createCell(2);
- tableCell_1_1.setCellValue("Row #");
- Cell tableCell_1_2 = tableRow_1.createCell(3);
- tableCell_1_2.setCellValue("Exponent");
- Cell tableCell_1_3 = tableRow_1.createCell(4);
- tableCell_1_3.setCellValue("10^Exponent");
-
- Row tableRow_2 = offsetSheet.createRow(2);
- Cell tableCell_2_1 = tableRow_2.createCell(2);
- tableCell_2_1.setCellValue(0);
- Cell tableCell_2_2 = tableRow_2.createCell(3);
- tableCell_2_2.setCellValue(0);
- Cell tableCell_2_3 = tableRow_2.createCell(4);
- tableCell_2_3.setCellValue(1);
-
- Row tableRow_3= offsetSheet.createRow(3);
- Cell tableCell_3_1 = tableRow_3.createCell(2);
- tableCell_3_1.setCellValue(1);
- Cell tableCell_3_2 = tableRow_3.createCell(3);
- tableCell_3_2.setCellValue(1);
- Cell tableCell_3_3 = tableRow_3.createCell(4);
- tableCell_3_3.setCellValue(10);
-
- Row tableRow_4 = offsetSheet.createRow(4);
- Cell tableCell_4_1 = tableRow_4.createCell(2);
- tableCell_4_1.setCellValue(2);
- Cell tableCell_4_2 = tableRow_4.createCell(3);
- tableCell_4_2.setCellValue(2);
- Cell tableCell_4_3 = tableRow_4.createCell(4);
- tableCell_4_3.setCellValue(100);
-
- AreaReference offsetSource = new AreaReference(new CellReference("C2"), new CellReference("E4"));
- offsetPivotTable = offsetSheet.createPivotTable(offsetSource, new CellReference("C6"));
- }
+ public abstract void setUp();
@After
public void tearDown() throws IOException {
- XSSFWorkbook wb2 = _testDataProvider.writeOutAndReadBack(wb);
- wb.close();
- wb2.close();
+ if (wb != null) {
+ XSSFWorkbook wb2 = _testDataProvider.writeOutAndReadBack(wb);
+ wb.close();
+ wb2.close();
+ }
}
/**
@@ -155,6 +82,7 @@ public class TestXSSFPivotTable {
assertEquals(0, (int)pivotTable.getRowLabelColumns().get(0));
assertEquals(1, (int)pivotTable.getRowLabelColumns().get(1));
}
+
/**
* Verify that it's not possible to create a row label outside of the referenced area.
*/
@@ -314,6 +242,7 @@ public class TestXSSFPivotTable {
/**
* Verify that it's possible to create a new filter
*/
+ @Test
public void testAddReportFilter() {
int columnIndex = 0;
diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFPivotTableName.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFPivotTableName.java
new file mode 100644
index 000000000..8df2a663d
--- /dev/null
+++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFPivotTableName.java
@@ -0,0 +1,112 @@
+/* ====================================================================
+ 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.usermodel;
+
+import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.ss.usermodel.Row;
+import org.apache.poi.ss.util.CellReference;
+import org.junit.Before;
+
+/**
+ * Test pivot tables created by named range
+ */
+public class TestXSSFPivotTableName extends BaseTestXSSFPivotTable {
+
+ @Override
+ @Before
+ public void setUp(){
+ wb = new XSSFWorkbook();
+ XSSFSheet sheet = wb.createSheet();
+
+ Row row1 = sheet.createRow(0);
+ // Create a cell and put a value in it.
+ Cell cell = row1.createCell(0);
+ cell.setCellValue("Names");
+ Cell cell2 = row1.createCell(1);
+ cell2.setCellValue("#");
+ Cell cell7 = row1.createCell(2);
+ cell7.setCellValue("Data");
+ Cell cell10 = row1.createCell(3);
+ cell10.setCellValue("Value");
+
+ Row row2 = sheet.createRow(1);
+ Cell cell3 = row2.createCell(0);
+ cell3.setCellValue("Jan");
+ Cell cell4 = row2.createCell(1);
+ cell4.setCellValue(10);
+ Cell cell8 = row2.createCell(2);
+ cell8.setCellValue("Apa");
+ Cell cell11 = row1.createCell(3);
+ cell11.setCellValue(11.11);
+
+ Row row3 = sheet.createRow(2);
+ Cell cell5 = row3.createCell(0);
+ cell5.setCellValue("Ben");
+ Cell cell6 = row3.createCell(1);
+ cell6.setCellValue(9);
+ Cell cell9 = row3.createCell(2);
+ cell9.setCellValue("Bepa");
+ Cell cell12 = row1.createCell(3);
+ cell12.setCellValue(12.12);
+
+ XSSFName namedRange = sheet.getWorkbook().createName();
+ namedRange.setRefersToFormula(sheet.getSheetName() + "!" + "A1:C2");
+ pivotTable = sheet.createPivotTable(namedRange, new CellReference("H5"));
+
+ XSSFSheet offsetSheet = wb.createSheet();
+
+ Row tableRow_1 = offsetSheet.createRow(1);
+ offsetOuterCell = tableRow_1.createCell(1);
+ offsetOuterCell.setCellValue(-1);
+ Cell tableCell_1_1 = tableRow_1.createCell(2);
+ tableCell_1_1.setCellValue("Row #");
+ Cell tableCell_1_2 = tableRow_1.createCell(3);
+ tableCell_1_2.setCellValue("Exponent");
+ Cell tableCell_1_3 = tableRow_1.createCell(4);
+ tableCell_1_3.setCellValue("10^Exponent");
+
+ Row tableRow_2 = offsetSheet.createRow(2);
+ Cell tableCell_2_1 = tableRow_2.createCell(2);
+ tableCell_2_1.setCellValue(0);
+ Cell tableCell_2_2 = tableRow_2.createCell(3);
+ tableCell_2_2.setCellValue(0);
+ Cell tableCell_2_3 = tableRow_2.createCell(4);
+ tableCell_2_3.setCellValue(1);
+
+ Row tableRow_3= offsetSheet.createRow(3);
+ Cell tableCell_3_1 = tableRow_3.createCell(2);
+ tableCell_3_1.setCellValue(1);
+ Cell tableCell_3_2 = tableRow_3.createCell(3);
+ tableCell_3_2.setCellValue(1);
+ Cell tableCell_3_3 = tableRow_3.createCell(4);
+ tableCell_3_3.setCellValue(10);
+
+ Row tableRow_4 = offsetSheet.createRow(4);
+ Cell tableCell_4_1 = tableRow_4.createCell(2);
+ tableCell_4_1.setCellValue(2);
+ Cell tableCell_4_2 = tableRow_4.createCell(3);
+ tableCell_4_2.setCellValue(2);
+ Cell tableCell_4_3 = tableRow_4.createCell(4);
+ tableCell_4_3.setCellValue(100);
+
+ namedRange = sheet.getWorkbook().createName();
+ namedRange.setRefersToFormula("C2:E4");
+ namedRange.setSheetIndex(sheet.getWorkbook().getSheetIndex(sheet));
+ offsetPivotTable = offsetSheet.createPivotTable(namedRange, new CellReference("C6"));
+ }
+}
diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFPivotTableRef.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFPivotTableRef.java
new file mode 100644
index 000000000..ec0c5c6c1
--- /dev/null
+++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFPivotTableRef.java
@@ -0,0 +1,111 @@
+/* ====================================================================
+ 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.usermodel;
+
+import org.apache.poi.ss.SpreadsheetVersion;
+import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.ss.usermodel.Row;
+import org.apache.poi.ss.util.AreaReference;
+import org.apache.poi.ss.util.CellReference;
+import org.junit.Before;
+
+/**
+ * Test pivot tables created by area reference
+ */
+public class TestXSSFPivotTableRef extends BaseTestXSSFPivotTable {
+
+ @Override
+ @Before
+ public void setUp(){
+ wb = new XSSFWorkbook();
+ XSSFSheet sheet = wb.createSheet();
+
+ Row row1 = sheet.createRow(0);
+ // Create a cell and put a value in it.
+ Cell cell = row1.createCell(0);
+ cell.setCellValue("Names");
+ Cell cell2 = row1.createCell(1);
+ cell2.setCellValue("#");
+ Cell cell7 = row1.createCell(2);
+ cell7.setCellValue("Data");
+ Cell cell10 = row1.createCell(3);
+ cell10.setCellValue("Value");
+
+ Row row2 = sheet.createRow(1);
+ Cell cell3 = row2.createCell(0);
+ cell3.setCellValue("Jan");
+ Cell cell4 = row2.createCell(1);
+ cell4.setCellValue(10);
+ Cell cell8 = row2.createCell(2);
+ cell8.setCellValue("Apa");
+ Cell cell11 = row1.createCell(3);
+ cell11.setCellValue(11.11);
+
+ Row row3 = sheet.createRow(2);
+ Cell cell5 = row3.createCell(0);
+ cell5.setCellValue("Ben");
+ Cell cell6 = row3.createCell(1);
+ cell6.setCellValue(9);
+ Cell cell9 = row3.createCell(2);
+ cell9.setCellValue("Bepa");
+ Cell cell12 = row1.createCell(3);
+ cell12.setCellValue(12.12);
+
+ AreaReference source = new AreaReference("A1:C2", SpreadsheetVersion.EXCEL2007);
+ pivotTable = sheet.createPivotTable(source, new CellReference("H5"));
+
+ XSSFSheet offsetSheet = wb.createSheet();
+
+ Row tableRow_1 = offsetSheet.createRow(1);
+ offsetOuterCell = tableRow_1.createCell(1);
+ offsetOuterCell.setCellValue(-1);
+ Cell tableCell_1_1 = tableRow_1.createCell(2);
+ tableCell_1_1.setCellValue("Row #");
+ Cell tableCell_1_2 = tableRow_1.createCell(3);
+ tableCell_1_2.setCellValue("Exponent");
+ Cell tableCell_1_3 = tableRow_1.createCell(4);
+ tableCell_1_3.setCellValue("10^Exponent");
+
+ Row tableRow_2 = offsetSheet.createRow(2);
+ Cell tableCell_2_1 = tableRow_2.createCell(2);
+ tableCell_2_1.setCellValue(0);
+ Cell tableCell_2_2 = tableRow_2.createCell(3);
+ tableCell_2_2.setCellValue(0);
+ Cell tableCell_2_3 = tableRow_2.createCell(4);
+ tableCell_2_3.setCellValue(1);
+
+ Row tableRow_3= offsetSheet.createRow(3);
+ Cell tableCell_3_1 = tableRow_3.createCell(2);
+ tableCell_3_1.setCellValue(1);
+ Cell tableCell_3_2 = tableRow_3.createCell(3);
+ tableCell_3_2.setCellValue(1);
+ Cell tableCell_3_3 = tableRow_3.createCell(4);
+ tableCell_3_3.setCellValue(10);
+
+ Row tableRow_4 = offsetSheet.createRow(4);
+ Cell tableCell_4_1 = tableRow_4.createCell(2);
+ tableCell_4_1.setCellValue(2);
+ Cell tableCell_4_2 = tableRow_4.createCell(3);
+ tableCell_4_2.setCellValue(2);
+ Cell tableCell_4_3 = tableRow_4.createCell(4);
+ tableCell_4_3.setCellValue(100);
+
+ AreaReference offsetSource = new AreaReference(new CellReference("C2"), new CellReference("E4"));
+ offsetPivotTable = offsetSheet.createPivotTable(offsetSource, new CellReference("C6"));
+ }
+}