Ensure SpreadsheetVersion is set for AreaReferences

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1800208 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
PJ Fanning 2017-06-28 22:11:26 +00:00
parent c80988698f
commit 5d4a4a7f28
28 changed files with 227 additions and 102 deletions

View File

@ -19,6 +19,7 @@ package org.apache.poi.xssf.usermodel.examples;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.ss.SpreadsheetVersion;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.AreaReference;
import org.apache.poi.ss.util.CellReference;
@ -80,8 +81,8 @@ public class CreateTable {
table.addColumn();
// Set which area the table should be placed in
AreaReference reference = new AreaReference(new CellReference(0, 0),
new CellReference(2,2));
AreaReference reference = wb.getCreationHelper().createAreaReference(
new CellReference(0, 0), new CellReference(2, 2));
table.setCellReferences(reference);
// Save

View File

@ -20,6 +20,8 @@ package org.apache.poi.hssf.usermodel;
import org.apache.poi.common.usermodel.HyperlinkType;
import org.apache.poi.hssf.record.common.ExtendedColor;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.util.AreaReference;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.util.Internal;
public class HSSFCreationHelper implements CreationHelper {
@ -75,4 +77,21 @@ public class HSSFCreationHelper implements CreationHelper {
public HSSFClientAnchor createClientAnchor(){
return new HSSFClientAnchor();
}
/**
* {@inheritDoc}
*/
@Override
public AreaReference createAreaReference(String reference) {
return new AreaReference(reference, workbook.getSpreadsheetVersion());
}
/**
* {@inheritDoc}
*/
@Override
public AreaReference createAreaReference(CellReference topLeft, CellReference bottomRight) {
return new AreaReference(topLeft, bottomRight, workbook.getSpreadsheetVersion());
}
}

View File

@ -17,6 +17,8 @@
package org.apache.poi.hssf.util;
import org.apache.poi.ss.SpreadsheetVersion;
/**
* @deprecated POI 3.15 beta 3. Use {@link org.apache.poi.ss.util.AreaReference} instead.
*/
@ -28,7 +30,7 @@ public final class AreaReference extends org.apache.poi.ss.util.AreaReference {
* The area reference must be contiguous (i.e. represent a single rectangle, not a union of rectangles)
*/
public AreaReference(String reference) {
super(reference);
super(reference, SpreadsheetVersion.EXCEL97);
}
/**

View File

@ -817,7 +817,7 @@ public final class FormulaParser {
CellReference topLeft = new CellReference(actualStartRow, actualStartCol);
CellReference bottomRight = new CellReference(actualEndRow, actualEndCol);
SheetIdentifier sheetIden = new SheetIdentifier( null, new NameIdentifier(sheetName, true));
Ptg ptg = _book.get3DReferencePtg(new AreaReference(topLeft, bottomRight), sheetIden);
Ptg ptg = _book.get3DReferencePtg(new AreaReference(topLeft, bottomRight, _ssVersion), sheetIden);
return new ParseNode(ptg);
}
@ -997,7 +997,7 @@ public final class FormulaParser {
if (part1.isColumn()) {
return AreaReference.getWholeColumn(_ssVersion, part1.getRep(), part2.getRep());
}
return new AreaReference(part1.getCellReference(), part2.getCellReference());
return new AreaReference(part1.getCellReference(), part2.getCellReference(), _ssVersion);
}
/**

View File

@ -17,6 +17,7 @@
package org.apache.poi.ss.formula.ptg;
import org.apache.poi.ss.SpreadsheetVersion;
import org.apache.poi.ss.formula.ExternSheetReferenceToken;
import org.apache.poi.ss.formula.FormulaRenderingWorkbook;
import org.apache.poi.ss.formula.WorkbookDependentFormula;
@ -41,7 +42,7 @@ public final class Area3DPtg extends AreaPtgBase implements WorkbookDependentFor
public Area3DPtg(String arearef, int externIdx) {
super(new AreaReference(arearef));
super(new AreaReference(arearef, SpreadsheetVersion.EXCEL97));
setExternSheetIndex(externIdx);
}

View File

@ -17,6 +17,7 @@
package org.apache.poi.ss.formula.ptg;
import org.apache.poi.ss.SpreadsheetVersion;
import org.apache.poi.ss.formula.SheetIdentifier;
import org.apache.poi.ss.formula.SheetNameFormatter;
import org.apache.poi.ss.formula.SheetRangeIdentifier;
@ -37,7 +38,7 @@ public final class Area3DPxg extends AreaPtgBase implements Pxg3D {
private String lastSheetName;
public Area3DPxg(int externalWorkbookNumber, SheetIdentifier sheetName, String arearef) {
this(externalWorkbookNumber, sheetName, new AreaReference(arearef));
this(externalWorkbookNumber, sheetName, new AreaReference(arearef, SpreadsheetVersion.EXCEL2007));
}
public Area3DPxg(int externalWorkbookNumber, SheetIdentifier sheetName, AreaReference arearef) {
super(arearef);
@ -51,7 +52,7 @@ public final class Area3DPxg extends AreaPtgBase implements Pxg3D {
}
public Area3DPxg(SheetIdentifier sheetName, String arearef) {
this(sheetName, new AreaReference(arearef));
this(sheetName, new AreaReference(arearef, SpreadsheetVersion.EXCEL2007));
}
public Area3DPxg(SheetIdentifier sheetName, AreaReference arearef) {
this(-1, sheetName, arearef);

View File

@ -17,8 +17,10 @@
package org.apache.poi.ss.formula.ptg;
import org.apache.poi.ss.SpreadsheetVersion;
import org.apache.poi.ss.util.AreaReference;
import org.apache.poi.util.LittleEndianInput;
import org.apache.poi.util.Removal;
/**
* Specifies a rectangular area of cells A1:A4 for instance.
@ -30,15 +32,24 @@ public final class AreaPtg extends Area2DPtgBase {
public AreaPtg(int firstRow, int lastRow, int firstColumn, int lastColumn, boolean firstRowRelative, boolean lastRowRelative, boolean firstColRelative, boolean lastColRelative) {
super(firstRow, lastRow, firstColumn, lastColumn, firstRowRelative, lastRowRelative, firstColRelative, lastColRelative);
}
public AreaPtg(LittleEndianInput in) {
super(in);
}
public AreaPtg(String arearef) {
super(new AreaReference(arearef));
}
public AreaPtg(AreaReference areaRef) {
super(areaRef);
}
/**
* @deprecated use {@link #AreaPtg(<code>AreaReference</code>)} instead
*/
@Deprecated()
@Removal(version="3.19")
public AreaPtg(String arearef) {
this(new AreaReference(arearef, SpreadsheetVersion.EXCEL97));
}
public AreaPtg(AreaReference arearef) {
super(arearef);
}
@Override
protected byte getSid() {
return sid;

View File

@ -287,7 +287,7 @@ public abstract class AreaPtgBase extends OperandPtg implements AreaI {
CellReference botRight = new CellReference(getLastRow(),getLastColumn(),!isLastRowRelative(),!isLastColRelative());
if(AreaReference.isWholeColumnReference(SpreadsheetVersion.EXCEL97, topLeft, botRight)) {
return (new AreaReference(topLeft, botRight)).formatAsString();
return (new AreaReference(topLeft, botRight, SpreadsheetVersion.EXCEL97)).formatAsString();
}
return topLeft.formatAsString() + ":" + botRight.formatAsString();
}

View File

@ -17,6 +17,8 @@
package org.apache.poi.ss.usermodel;
import org.apache.poi.common.usermodel.HyperlinkType;
import org.apache.poi.ss.util.AreaReference;
import org.apache.poi.ss.util.CellReference;
/**
* An object that handles instantiating concrete
@ -66,4 +68,22 @@ public interface CreationHelper {
* @see org.apache.poi.ss.usermodel.Drawing
*/
ClientAnchor createClientAnchor();
/**
* Creates an AreaReference.
*
* @param reference cell reference
* @return an AreaReference instance
*/
AreaReference createAreaReference(String reference);
/**
* Creates an area ref from a pair of Cell References..
*
* @param topLeft cell reference
* @param bottomRight cell reference
* @return an AreaReference instance
*/
AreaReference createAreaReference(CellReference topLeft, CellReference bottomRight);
}

View File

@ -22,6 +22,7 @@ import java.util.List;
import java.util.StringTokenizer;
import org.apache.poi.ss.SpreadsheetVersion;
import org.apache.poi.util.Removal;
public class AreaReference {
@ -37,15 +38,6 @@ public class AreaReference {
private final CellReference _lastCell;
private final boolean _isSingleCell;
private final SpreadsheetVersion _version; // never null
/**
* @deprecated POI 3.13 beta 1. Prefer supplying a version.
*/
@Deprecated
public AreaReference(String reference) {
this(reference, DEFAULT_SPREADSHEET_VERSION);
// generateContiguous must be updated before this can be deleted.
}
/**
* Create an area ref from a string representation. Sheet names containing special characters should be
@ -116,9 +108,19 @@ public class AreaReference {
/**
* Creates an area ref from a pair of Cell References.
* @deprecated use {@link #new AreaReference(<code>CellReference</code>, <code>CellReference</code>, <code>SpreadsheetVersion</code>)} instead
*/
@Deprecated
@Removal(version="3.19")
public AreaReference(CellReference topLeft, CellReference botRight) {
_version = DEFAULT_SPREADSHEET_VERSION;
this(topLeft, botRight, DEFAULT_SPREADSHEET_VERSION);
}
/**
* Creates an area ref from a pair of Cell References.
*/
public AreaReference(CellReference topLeft, CellReference botRight, SpreadsheetVersion version) {
_version = (null != version) ? version : DEFAULT_SPREADSHEET_VERSION;
boolean swapRows = topLeft.getRow() > botRight.getRow();
boolean swapCols = topLeft.getCol() > botRight.getCol();
if (swapRows || swapCols) {
@ -166,7 +168,7 @@ public class AreaReference {
* unbroken) area, or is it made up of
* several different parts?
* (If it is, you will need to call
* {@link #generateContiguous(String)})
* {@link #generateContiguous(<code>SpreadsheetVersion</code>, String)})
*/
public static boolean isContiguous(String reference) {
// If there's a sheet name, strip it off
@ -216,15 +218,29 @@ public class AreaReference {
}
/**
* Takes a non-contiguous area reference, and
* returns an array of contiguous area references.
* Takes a non-contiguous area reference, and returns an array of contiguous area references
* @return an array of contiguous area references.
* @deprecated use {@link #generateContiguous(<code>SpreadsheetVersion</code>, String)} instead
*/
@Deprecated
@Removal(version="3.19")
public static AreaReference[] generateContiguous(String reference) {
return generateContiguous(DEFAULT_SPREADSHEET_VERSION, reference);
}
/**
* Takes a non-contiguous area reference, and returns an array of contiguous area references
* @return an array of contiguous area references.
*/
public static AreaReference[] generateContiguous(SpreadsheetVersion version, String reference) {
if (null == version) {
version = DEFAULT_SPREADSHEET_VERSION; // how the code used to behave.
}
List<AreaReference> refs = new ArrayList<AreaReference>();
StringTokenizer st = new StringTokenizer(reference, ",");
while(st.hasMoreTokens()) {
refs.add(
new AreaReference(st.nextToken())
new AreaReference(st.nextToken(), version)
);
}
return refs.toArray(new AreaReference[refs.size()]);

View File

@ -23,6 +23,8 @@ import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.DataFormat;
import org.apache.poi.ss.usermodel.ExtendedColor;
import org.apache.poi.ss.usermodel.Hyperlink;
import org.apache.poi.ss.util.AreaReference;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.util.Internal;
import org.apache.poi.util.POILogFactory;
import org.apache.poi.util.POILogger;
@ -79,4 +81,21 @@ public class SXSSFCreationHelper implements CreationHelper {
public ClientAnchor createClientAnchor() {
return helper.createClientAnchor();
}
/**
* {@inheritDoc}
*/
@Override
public AreaReference createAreaReference(String reference) {
return new AreaReference(reference, wb.getSpreadsheetVersion());
}
/**
* {@inheritDoc}
*/
@Override
public AreaReference createAreaReference(CellReference topLeft, CellReference bottomRight) {
return new AreaReference(topLeft, bottomRight, wb.getSpreadsheetVersion());
}
}

View File

@ -18,13 +18,12 @@
package org.apache.poi.xssf.usermodel;
import org.apache.poi.ss.usermodel.ClientAnchor;
import org.apache.poi.ss.usermodel.ClientAnchor.AnchorType;
import org.apache.poi.util.Internal;
import org.apache.poi.util.Removal;
import org.apache.poi.util.Units;
import org.openxmlformats.schemas.drawingml.x2006.main.CTPoint2D;
import org.openxmlformats.schemas.drawingml.x2006.main.CTPositiveSize2D;
import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTMarker;
import org.apache.poi.ss.usermodel.ClientAnchor.AnchorType;
/**
* A client anchor is attached to an excel worksheet. It anchors against:

View File

@ -19,6 +19,8 @@ package org.apache.poi.xssf.usermodel;
import org.apache.poi.common.usermodel.HyperlinkType;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.Hyperlink;
import org.apache.poi.ss.util.AreaReference;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.util.Internal;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor;
@ -86,4 +88,20 @@ public class XSSFCreationHelper implements CreationHelper {
public XSSFClientAnchor createClientAnchor() {
return new XSSFClientAnchor();
}
/**
* {@inheritDoc}
*/
@Override
public AreaReference createAreaReference(String reference) {
return new AreaReference(reference, workbook.getSpreadsheetVersion());
}
/**
* {@inheritDoc}
*/
@Override
public AreaReference createAreaReference(CellReference topLeft, CellReference bottomRight) {
return new AreaReference(topLeft, bottomRight, workbook.getSpreadsheetVersion());
}
}

View File

@ -146,7 +146,8 @@ public class XSSFPivotCacheDefinition extends POIXMLDocumentPart{
for (XSSFTable table : sheet.getTables()) {
// TODO: case-sensitive?
if (name.equals(table.getName())) {
return new AreaReference(table.getStartCellReference(), table.getEndCellReference());
return new AreaReference(table.getStartCellReference(), table.getEndCellReference(),
SpreadsheetVersion.EXCEL2007);
}
}

View File

@ -28,6 +28,7 @@ import java.util.List;
import org.apache.poi.POIXMLDocumentPart;
import org.apache.poi.openxml4j.opc.PackagePart;
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;
@ -431,7 +432,8 @@ public class XSSFPivotTable extends POIXMLDocumentPart {
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));
AreaReference destination = new AreaReference(position, new CellReference(
position.getRow()+1, position.getCol()+1), SpreadsheetVersion.EXCEL2007);
CTLocation location;
if(pivotTableDefinition.getLocation() == null) {

View File

@ -30,6 +30,7 @@ import java.util.Locale;
import org.apache.poi.POIXMLDocumentPart;
import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.ss.SpreadsheetVersion;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.Table;
@ -349,7 +350,8 @@ public class XSSFTable extends POIXMLDocumentPart implements Table {
public AreaReference getCellReferences() {
return new AreaReference(
getStartCellReference(),
getEndCellReference()
getEndCellReference(),
SpreadsheetVersion.EXCEL2007
);
}
/**

View File

@ -22,6 +22,7 @@ import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
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.CellValue;
@ -91,7 +92,9 @@ public class TestStructuredReferences {
// update Table
final XSSFTable table = wb.getTable("\\_Prime.1");
final AreaReference newArea = new AreaReference(table.getStartCellReference(), new CellReference(table.getEndRowIndex() + 1, table.getEndColIndex()));
final AreaReference newArea = wb.getCreationHelper().createAreaReference(
table.getStartCellReference(),
new CellReference(table.getEndRowIndex() + 1, table.getEndColIndex()));
String newAreaStr = newArea.formatAsString();
table.getCTTable().setRef(newAreaStr);
table.getCTTable().getAutoFilter().setRef(newAreaStr);

View File

@ -290,7 +290,7 @@ public abstract class BaseTestXSSFPivotTable {
assertNotNull(original);
assertNotNull(offset);
AreaReference source = new AreaReference("ORIGinal!A1:C2", _testDataProvider.getSpreadsheetVersion());
AreaReference source = wb.getCreationHelper().createAreaReference("ORIGinal!A1:C2");
// create a pivot table on the same sheet, case insensitive
original.createPivotTable(source, new CellReference("W1"));
// create a pivot table on a different sheet, case insensitive

View File

@ -64,7 +64,6 @@ import org.apache.poi.openxml4j.opc.PackagingURIHelper;
import org.apache.poi.openxml4j.util.ZipSecureFile;
import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.SpreadsheetVersion;
import org.apache.poi.ss.formula.WorkbookEvaluator;
import org.apache.poi.ss.formula.eval.ErrorEval;
import org.apache.poi.ss.formula.eval.NumberEval;
@ -1346,7 +1345,7 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues {
Name name = wb.getName("Intekon.ProdCodes");
assertEquals("'Abc,1'!$A$1:$A$2", name.getRefersToFormula());
AreaReference ref = new AreaReference(name.getRefersToFormula(), SpreadsheetVersion.EXCEL2007);
AreaReference ref = wb.getCreationHelper().createAreaReference(name.getRefersToFormula());
assertEquals(0, ref.getFirstCell().getRow());
assertEquals(0, ref.getFirstCell().getCol());
assertEquals(1, ref.getLastCell().getRow());

View File

@ -17,7 +17,6 @@
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;
@ -66,7 +65,7 @@ public class TestXSSFPivotTableRef extends BaseTestXSSFPivotTable {
Cell cell12 = row1.createCell(3);
cell12.setCellValue(12.12);
AreaReference source = new AreaReference("A1:C2", SpreadsheetVersion.EXCEL2007);
AreaReference source = wb.getCreationHelper().createAreaReference("A1:C2");
pivotTable = sheet.createPivotTable(source, new CellReference("H5"));
XSSFSheet offsetSheet = wb.createSheet();
@ -105,7 +104,8 @@ public class TestXSSFPivotTableRef extends BaseTestXSSFPivotTable {
Cell tableCell_4_3 = tableRow_4.createCell(4);
tableCell_4_3.setCellValue(100);
AreaReference offsetSource = new AreaReference(new CellReference("C2"), new CellReference("E4"));
AreaReference offsetSource = wb.getCreationHelper().createAreaReference(
new CellReference("C2"), new CellReference("E4"));
offsetPivotTable = offsetSheet.createPivotTable(offsetSource, new CellReference("C6"));
}
}

View File

@ -40,7 +40,6 @@ import org.apache.poi.POIXMLException;
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.poifs.crypt.CryptoFunctions;
import org.apache.poi.poifs.crypt.HashAlgorithm;
import org.apache.poi.ss.SpreadsheetVersion;
import org.apache.poi.ss.usermodel.AutoFilter;
import org.apache.poi.ss.usermodel.BaseTestXSheet;
import org.apache.poi.ss.usermodel.Cell;
@ -56,7 +55,6 @@ import org.apache.poi.ss.usermodel.IndexedColors;
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.CellAddress;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.CellReference;
@ -1337,10 +1335,12 @@ public final class TestXSSFSheet extends BaseTestXSheet {
assertNotNull(wb);
assertNotNull(sheet);
XSSFPivotTable pivotTable = sheet.createPivotTable(new AreaReference("A1:B2", SpreadsheetVersion.EXCEL2007), new CellReference("H5"));
XSSFPivotTable pivotTable = sheet.createPivotTable(wb.getCreationHelper().createAreaReference("A1:B2"),
new CellReference("H5"));
assertNotNull(pivotTable);
assertTrue(wb.getPivotTables().size() > 0);
XSSFPivotTable pivotTable2 = sheet.createPivotTable(new AreaReference("A1:B2", SpreadsheetVersion.EXCEL2007), new CellReference("L5"), sheet);
XSSFPivotTable pivotTable2 = sheet.createPivotTable(wb.getCreationHelper().createAreaReference("A1:B2"),
new CellReference("L5"), sheet);
assertNotNull(pivotTable2);
assertTrue(wb.getPivotTables().size() > 1);
wb.close();
@ -1353,12 +1353,13 @@ public final class TestXSSFSheet extends BaseTestXSheet {
assertNotNull(wb);
assertNotNull(sheet);
XSSFPivotTable pivotTable = sheet.createPivotTable(new AreaReference("A1:B2", SpreadsheetVersion.EXCEL2007), new CellReference("H5"));
XSSFPivotTable pivotTable = sheet.createPivotTable(wb.getCreationHelper().createAreaReference("A1:B2"), new CellReference("H5"));
assertNotNull(pivotTable);
assertTrue(wb.getPivotTables().size() > 0);
assertNotNull(wb);
XSSFSheet sheet2 = wb.createSheet();
XSSFPivotTable pivotTable2 = sheet2.createPivotTable(new AreaReference("A1:B2", SpreadsheetVersion.EXCEL2007), new CellReference("H5"), sheet);
XSSFPivotTable pivotTable2 = sheet2.createPivotTable(wb.getCreationHelper().createAreaReference("A1:B2"),
new CellReference("H5"), sheet);
assertNotNull(pivotTable2);
assertTrue(wb.getPivotTables().size() > 1);
wb.close();
@ -1371,7 +1372,7 @@ public final class TestXSSFSheet extends BaseTestXSheet {
assertNotNull(wb);
assertNotNull(sheet);
XSSFPivotTable pivotTable = sheet.createPivotTable(new AreaReference("A1:B2", SpreadsheetVersion.EXCEL2007), new CellReference("H5"));
XSSFPivotTable pivotTable = sheet.createPivotTable(wb.getCreationHelper().createAreaReference("A1:B2"), new CellReference("H5"));
assertNotNull(pivotTable);
assertTrue(wb.getPivotTables().size() > 0);
wb.close();
@ -1384,7 +1385,7 @@ public final class TestXSSFSheet extends BaseTestXSheet {
XSSFSheet sheet2 = wb.createSheet();
XSSFPivotTable pivotTable = sheet2.createPivotTable
(new AreaReference("A1:B2", SpreadsheetVersion.EXCEL2007), new CellReference("H5"), sheet1);
(wb.getCreationHelper().createAreaReference("A1:B2"), new CellReference("H5"), sheet1);
assertEquals(0, pivotTable.getRowLabelColumns().size());
assertEquals(1, wb.getPivotTables().size());
@ -1400,7 +1401,7 @@ public final class TestXSSFSheet extends BaseTestXSheet {
XSSFSheet sheet2 = wb.createSheet("TEST");
XSSFPivotTable pivotTable = sheet2.createPivotTable(
new AreaReference(sheet.getSheetName()+"!A$1:B$2", SpreadsheetVersion.EXCEL2007),
wb.getCreationHelper().createAreaReference(sheet.getSheetName()+"!A$1:B$2"),
new CellReference("H5"));
assertEquals(0, pivotTable.getRowLabelColumns().size());
wb.close();
@ -1413,7 +1414,7 @@ public final class TestXSSFSheet extends BaseTestXSheet {
XSSFSheet sheet2 = wb.createSheet("TEST");
sheet2.createPivotTable(
new AreaReference(sheet.getSheetName()+"!A$1:B$2", SpreadsheetVersion.EXCEL2007),
wb.getCreationHelper().createAreaReference(sheet.getSheetName()+"!A$1:B$2"),
new CellReference("H5"),
sheet2);
wb.close();

View File

@ -31,15 +31,14 @@ import java.util.ArrayList;
import java.util.List;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.util.AreaReference;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.util.IOUtils;
import org.apache.poi.util.TempFile;
import org.apache.poi.xssf.XSSFTestDataSamples;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.junit.Test;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTable;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableColumn;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableColumns;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableStyleInfo;
public final class TestXSSFTable {
@ -267,7 +266,8 @@ public final class TestXSSFTable {
assertEquals(new CellReference("C1"), table.getStartCellReference());
assertEquals(new CellReference("M3"), table.getEndCellReference());
IOUtils.closeQuietly(wb);
}
@Test
@ -288,6 +288,8 @@ public final class TestXSSFTable {
// update cell references to clear the cache
table.updateReferences();
assertEquals(11, table.getRowCount());
IOUtils.closeQuietly(wb);
}
@Test
@ -350,13 +352,14 @@ public final class TestXSSFTable {
t.addColumn();
t.addColumn();
t.addColumn();
t.setCellReferences(new AreaReference(
t.setCellReferences(wb.getCreationHelper().createAreaReference(
new CellReference(c1), new CellReference(c6)
));
// Save and re-load
wb = XSSFTestDataSamples.writeOutAndReadBack(wb);
s = wb.getSheetAt(0);
XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb);
IOUtils.closeQuietly(wb);
s = wb2.getSheetAt(0);
// Check
assertEquals(1, s.getTables().size());
@ -370,6 +373,6 @@ public final class TestXSSFTable {
assertEquals("ABCD", t.getCTTable().getTableColumns().getTableColumnArray(2).getName());
// Done
wb.close();
IOUtils.closeQuietly(wb2);
}
}

View File

@ -794,7 +794,7 @@ public final class TestXSSFWorkbook extends BaseTestXWorkbook {
Cell cell9 = row3.createCell(2);
cell9.setCellValue("Bepa");
AreaReference source = new AreaReference("A1:B2", SpreadsheetVersion.EXCEL2007);
AreaReference source = wb.getCreationHelper().createAreaReference("A1:B2");
sheet.createPivotTable(source, new CellReference("H5"));
}

View File

@ -22,6 +22,7 @@ import org.apache.poi.hssf.HSSFITestDataProvider;
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.hssf.model.HSSFFormulaParser;
import org.apache.poi.hssf.record.NameRecord;
import org.apache.poi.ss.SpreadsheetVersion;
import org.apache.poi.ss.formula.FormulaType;
import org.apache.poi.ss.formula.ptg.Ptg;
import org.apache.poi.ss.usermodel.BaseTestNamedRange;
@ -197,7 +198,6 @@ public final class TestHSSFName extends BaseTestNamedRange {
workbook.close();
}
@SuppressWarnings("deprecation")
@Test
public void testDeletedReference() throws Exception {
HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("24207.xls");
@ -206,7 +206,7 @@ public final class TestHSSFName extends BaseTestNamedRange {
HSSFName name1 = wb.getNameAt(0);
assertEquals("a", name1.getNameName());
assertEquals("Sheet1!$A$1", name1.getRefersToFormula());
new AreaReference(name1.getRefersToFormula());
wb.getCreationHelper().createAreaReference(name1.getRefersToFormula());
assertTrue("Successfully constructed first reference", true);
HSSFName name2 = wb.getNameAt(1);
@ -214,7 +214,7 @@ public final class TestHSSFName extends BaseTestNamedRange {
assertEquals("Sheet1!#REF!", name2.getRefersToFormula());
assertTrue(name2.isDeleted());
try {
new AreaReference(name2.getRefersToFormula());
wb.getCreationHelper().createAreaReference(name2.getRefersToFormula());
fail("attempt to supply an invalid reference to AreaReference constructor results in exception");
} catch (IllegalArgumentException e) { // TODO - use a stronger typed exception for this condition
// expected during successful test

View File

@ -25,6 +25,7 @@ import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.hssf.model.HSSFFormulaParser;
import org.apache.poi.hssf.model.InternalWorkbook;
import org.apache.poi.hssf.record.NameRecord;
import org.apache.poi.ss.SpreadsheetVersion;
import org.apache.poi.ss.formula.ptg.Area3DPtg;
import org.apache.poi.ss.formula.ptg.MemFuncPtg;
import org.apache.poi.ss.formula.ptg.Ptg;
@ -38,13 +39,11 @@ import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.TestHSSFWorkbook;
import org.apache.poi.ss.util.AreaReference;
import org.apache.poi.ss.util.CellReference;
/**
*
*/
public final class TestAreaReference extends TestCase {
public void testAreaRef1() {
AreaReference ar = new AreaReference("$A$1:$B$2");
AreaReference ar = new AreaReference("$A$1:$B$2", SpreadsheetVersion.EXCEL97);
assertFalse("Two cells expected", ar.isSingleCell());
CellReference cf = ar.getFirstCell();
assertTrue("row is 4",cf.getRow()==0);
@ -87,14 +86,14 @@ public final class TestAreaReference extends TestCase {
public void testReferenceWithSheet() {
AreaReference ar;
ar = new AreaReference("Tabelle1!B5:B5");
ar = new AreaReference("Tabelle1!B5:B5", SpreadsheetVersion.EXCEL97);
assertTrue(ar.isSingleCell());
TestCellReference.confirmCell(ar.getFirstCell(), "Tabelle1", 4, 1, false, false, "Tabelle1!B5");
assertEquals(1, ar.getAllReferencedCells().length);
ar = new AreaReference("Tabelle1!$B$5:$B$7");
ar = new AreaReference("Tabelle1!$B$5:$B$7", SpreadsheetVersion.EXCEL97);
assertFalse(ar.isSingleCell());
TestCellReference.confirmCell(ar.getFirstCell(), "Tabelle1", 4, 1, true, true, "Tabelle1!$B$5");
@ -123,22 +122,22 @@ public final class TestAreaReference extends TestCase {
assertFalse(AreaReference.isContiguous(refDC3D));
// Check we can only create contiguous entries
new AreaReference(refSimple);
new AreaReference(ref2D);
new AreaReference(refSimple, SpreadsheetVersion.EXCEL97);
new AreaReference(ref2D, SpreadsheetVersion.EXCEL97);
try {
new AreaReference(refDCSimple);
new AreaReference(refDCSimple, SpreadsheetVersion.EXCEL97);
fail();
} catch(IllegalArgumentException e) {
// expected during successful test
}
try {
new AreaReference(refDC2D);
new AreaReference(refDC2D, SpreadsheetVersion.EXCEL97);
fail();
} catch(IllegalArgumentException e) {
// expected during successful test
}
try {
new AreaReference(refDC3D);
new AreaReference(refDC3D, SpreadsheetVersion.EXCEL97);
fail();
} catch(IllegalArgumentException e) {
// expected during successful test
@ -147,17 +146,17 @@ public final class TestAreaReference extends TestCase {
// Test that we split as expected
AreaReference[] refs;
refs = AreaReference.generateContiguous(refSimple);
refs = AreaReference.generateContiguous(SpreadsheetVersion.EXCEL97, refSimple);
assertEquals(1, refs.length);
assertTrue(refs[0].isSingleCell());
assertEquals("$C$10", refs[0].formatAsString());
refs = AreaReference.generateContiguous(ref2D);
refs = AreaReference.generateContiguous(SpreadsheetVersion.EXCEL97, ref2D);
assertEquals(1, refs.length);
assertFalse(refs[0].isSingleCell());
assertEquals("$C$10:$D$11", refs[0].formatAsString());
refs = AreaReference.generateContiguous(refDCSimple);
refs = AreaReference.generateContiguous(SpreadsheetVersion.EXCEL97, refDCSimple);
assertEquals(3, refs.length);
assertTrue(refs[0].isSingleCell());
assertTrue(refs[1].isSingleCell());
@ -166,7 +165,7 @@ public final class TestAreaReference extends TestCase {
assertEquals("$D$12", refs[1].formatAsString());
assertEquals("$E$14", refs[2].formatAsString());
refs = AreaReference.generateContiguous(refDC2D);
refs = AreaReference.generateContiguous(SpreadsheetVersion.EXCEL97, refDC2D);
assertEquals(3, refs.length);
assertFalse(refs[0].isSingleCell());
assertTrue(refs[1].isSingleCell());
@ -175,7 +174,7 @@ public final class TestAreaReference extends TestCase {
assertEquals("$D$12", refs[1].formatAsString());
assertEquals("$E$14:$E$20", refs[2].formatAsString());
refs = AreaReference.generateContiguous(refDC3D);
refs = AreaReference.generateContiguous(SpreadsheetVersion.EXCEL97, refDC3D);
assertEquals(2, refs.length);
assertFalse(refs[0].isSingleCell());
assertFalse(refs[0].isSingleCell());
@ -230,7 +229,7 @@ public final class TestAreaReference extends TestCase {
// Check the parsing of the reference into cells
assertFalse(AreaReference.isContiguous(aNamedCell.getRefersToFormula()));
AreaReference[] arefs = AreaReference.generateContiguous(aNamedCell.getRefersToFormula());
AreaReference[] arefs = AreaReference.generateContiguous(SpreadsheetVersion.EXCEL97, aNamedCell.getRefersToFormula());
assertEquals(2, arefs.length);
assertEquals(refA, arefs[0].formatAsString());
assertEquals(refB, arefs[1].formatAsString());
@ -250,16 +249,16 @@ public final class TestAreaReference extends TestCase {
public void testSpecialSheetNames() {
AreaReference ar;
ar = new AreaReference("'Sheet A'!A1:A1");
ar = new AreaReference("'Sheet A'!A1:A1", SpreadsheetVersion.EXCEL97);
confirmAreaSheetName(ar, "Sheet A", "'Sheet A'!A1");
ar = new AreaReference("'Hey! Look Here!'!A1:A1");
ar = new AreaReference("'Hey! Look Here!'!A1:A1", SpreadsheetVersion.EXCEL97);
confirmAreaSheetName(ar, "Hey! Look Here!", "'Hey! Look Here!'!A1");
ar = new AreaReference("'O''Toole'!A1:B2");
ar = new AreaReference("'O''Toole'!A1:B2", SpreadsheetVersion.EXCEL97);
confirmAreaSheetName(ar, "O'Toole", "'O''Toole'!A1:B2");
ar = new AreaReference("'one:many'!A1:B2");
ar = new AreaReference("'one:many'!A1:B2", SpreadsheetVersion.EXCEL97);
confirmAreaSheetName(ar, "one:many", "'one:many'!A1:B2");
}
@ -274,9 +273,19 @@ public final class TestAreaReference extends TestCase {
confirmWholeColumnRef("$C:D", 2, 3, true, false);
confirmWholeColumnRef("AD:$AE", 29, 30, false, true);
}
@SuppressWarnings("deprecation")
public void testDeprecatedMethod() {
String refSimple = "$C$10:$C$10";
AreaReference[] arefs1 = AreaReference.generateContiguous(SpreadsheetVersion.EXCEL97, refSimple);
AreaReference[] arefs2 = AreaReference.generateContiguous(refSimple);
assertEquals(1, arefs1.length);
assertEquals(arefs1.length, arefs2.length);
assertEquals(arefs1[0].formatAsString(), arefs2[0].formatAsString());
}
private static void confirmWholeColumnRef(String ref, int firstCol, int lastCol, boolean firstIsAbs, boolean lastIsAbs) {
AreaReference ar = new AreaReference(ref);
AreaReference ar = new AreaReference(ref, SpreadsheetVersion.EXCEL97);
confirmCell(ar.getFirstCell(), 0, firstCol, true, firstIsAbs);
confirmCell(ar.getLastCell(), 0xFFFF, lastCol, true, lastIsAbs);
}

View File

@ -20,10 +20,14 @@ package org.apache.poi.ss.formula.eval;
import junit.framework.AssertionFailedError;
import junit.framework.TestCase;
import java.util.Arrays;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.SpreadsheetVersion;
import org.apache.poi.ss.formula.TwoDEval;
import org.apache.poi.ss.formula.ptg.AreaI;
import org.apache.poi.ss.formula.ptg.AreaI.OffsetArea;
@ -54,15 +58,17 @@ public final class TestRangeEval extends TestCase {
createRefEval(refA),
createRefEval(refB),
};
@SuppressWarnings("deprecation")
AreaReference ar = new AreaReference(expectedAreaRef);
ValueEval result = EvalInstances.Range.evaluate(args, 0, (short)0);
assertTrue(result instanceof AreaEval);
AreaEval ae = (AreaEval) result;
assertEquals(ar.getFirstCell().getRow(), ae.getFirstRow());
assertEquals(ar.getLastCell().getRow(), ae.getLastRow());
assertEquals(ar.getFirstCell().getCol(), ae.getFirstColumn());
assertEquals(ar.getLastCell().getCol(), ae.getLastColumn());
List<SpreadsheetVersion> versions = Arrays.asList(new SpreadsheetVersion[] {SpreadsheetVersion.EXCEL97, SpreadsheetVersion.EXCEL2007});
for(SpreadsheetVersion version : versions) {
AreaReference ar = new AreaReference(expectedAreaRef, version);
ValueEval result = EvalInstances.Range.evaluate(args, 0, (short)0);
assertTrue(result instanceof AreaEval);
AreaEval ae = (AreaEval) result;
assertEquals(ar.getFirstCell().getRow(), ae.getFirstRow());
assertEquals(ar.getLastCell().getRow(), ae.getLastRow());
assertEquals(ar.getFirstCell().getCol(), ae.getFirstColumn());
assertEquals(ar.getLastCell().getCol(), ae.getLastColumn());
}
}
private static ValueEval createRefEval(String refStr) {

View File

@ -500,8 +500,7 @@ public abstract class BaseTestNamedRange {
assertNotNull(aNamedCell);
// retrieve the cell at the named range and test its contents
@SuppressWarnings("deprecation")
AreaReference aref = new AreaReference(aNamedCell.getRefersToFormula());
AreaReference aref = wb.getCreationHelper().createAreaReference(aNamedCell.getRefersToFormula());
assertTrue("Should be exactly 1 cell in the named cell :'" +cellName+"'", aref.isSingleCell());
CellReference cref = aref.getFirstCell();
@ -512,7 +511,6 @@ public abstract class BaseTestNamedRange {
Cell c = r.getCell(cref.getCol());
String contents = c.getRichStringCellValue().getString();
assertEquals("Contents of cell retrieved by its named reference", contents, cellValue);
wb.close();
}

View File

@ -62,10 +62,4 @@ public class TestAreaReference extends TestCase {
assertEquals(SpreadsheetVersion.EXCEL2007.getLastColumnIndex(), newStyle.getLastCell().getCol());
assertEquals(1, newStyle.getLastCell().getRow());
}
@SuppressWarnings("deprecation") // deliberate test for behaviour if deprecated constructor used.
public void testFallbackToExcel97IfVersionNotSupplied() {
assertTrue(new AreaReference("A:B").isWholeColumnReference());
assertTrue(AreaReference.isWholeColumnReference(null, new CellReference("A$1"), new CellReference("A$" + SpreadsheetVersion.EXCEL97.getMaxRows())));
}
}