public class CellReference
extends java.lang.Object
Common conversion functions between Excel style A1, C27 style cell references, and POI usermodel style row=0, column=0 style references. Handles sheet-based and sheet-free references as well, eg "Sheet1!A1" and "$B$72"
Use CellReference when the concept of
relative/absolute does apply (such as a cell reference in a formula).
Use CellAddress
when you want to refer to the location of a cell in a sheet
when the concept of relative/absolute does not apply (such as the anchor location
of a cell comment).
CellReferences have a concept of "sheet", while CellAddresses do not.
Modifier and Type | Class and Description |
---|---|
static class |
CellReference.NameType
Used to classify identifiers found in formulas as cell references or not.
|
Constructor and Description |
---|
CellReference(Cell cell) |
CellReference(int pRow,
int pCol) |
CellReference(int pRow,
int pCol,
boolean pAbsRow,
boolean pAbsCol) |
CellReference(int pRow,
short pCol) |
CellReference(java.lang.String cellRef)
Create an cell ref from a string representation.
|
CellReference(java.lang.String pSheetName,
int pRow,
int pCol,
boolean pAbsRow,
boolean pAbsCol) |
Modifier and Type | Method and Description |
---|---|
static boolean |
cellReferenceIsWithinRange(java.lang.String colStr,
java.lang.String rowStr,
SpreadsheetVersion ssVersion)
Used to decide whether a name of the form "[A-Z]*[0-9]*" that appears in a formula can be
interpreted as a cell reference.
|
static CellReference.NameType |
classifyCellReference(java.lang.String str,
SpreadsheetVersion ssVersion)
Classifies an identifier as either a simple (2D) cell reference or a named range name
|
static int |
convertColStringToIndex(java.lang.String ref)
takes in a column reference portion of a CellRef and converts it from
ALPHA-26 number format to 0-based base 10.
|
static java.lang.String |
convertNumToColString(int col)
Takes in a 0-based base-10 column and returns a ALPHA-26
representation.
|
boolean |
equals(java.lang.Object o)
Checks whether this cell reference is equal to another object.
|
java.lang.String |
formatAsString()
Returns a text representation of this cell reference.
|
java.lang.String |
formatAsString(boolean includeSheetName)
Returns a text representation of this cell reference and allows to control
if the sheetname is included in the reference.
|
java.lang.String[] |
getCellRefParts()
Returns the three parts of the cell reference, the
Sheet name (or null if none supplied), the 1 based
row number, and the A based column letter.
|
short |
getCol() |
int |
getRow() |
java.lang.String |
getSheetName() |
int |
hashCode() |
boolean |
isColAbsolute() |
static boolean |
isColumnWithinRange(java.lang.String colStr,
SpreadsheetVersion ssVersion) |
static boolean |
isPartAbsolute(java.lang.String part) |
boolean |
isRowAbsolute() |
static boolean |
isRowWithinRange(int rowNum,
SpreadsheetVersion ssVersion)
Determines whether
row is a valid row number for a given SpreadsheetVersion. |
static boolean |
isRowWithinRange(java.lang.String rowStr,
SpreadsheetVersion ssVersion)
Determines whether
rowStr is a valid row number for a given SpreadsheetVersion. |
java.lang.String |
toString() |
public CellReference(java.lang.String cellRef)
public CellReference(int pRow, int pCol)
public CellReference(int pRow, short pCol)
public CellReference(Cell cell)
public CellReference(int pRow, int pCol, boolean pAbsRow, boolean pAbsCol)
public CellReference(java.lang.String pSheetName, int pRow, int pCol, boolean pAbsRow, boolean pAbsCol)
public int getRow()
public short getCol()
public boolean isRowAbsolute()
public boolean isColAbsolute()
public java.lang.String getSheetName()
null
if this is a 2D reference. Special characters are not
escaped or delimitedpublic static boolean isPartAbsolute(java.lang.String part)
public static int convertColStringToIndex(java.lang.String ref)
public static CellReference.NameType classifyCellReference(java.lang.String str, SpreadsheetVersion ssVersion)
public static boolean cellReferenceIsWithinRange(java.lang.String colStr, java.lang.String rowStr, SpreadsheetVersion ssVersion)
Note - that the maximum sheet size varies across Excel versions:
POI currently targets BIFF8 (Excel 97-2003), so the following behaviour can be observed for this method:
Version File Format Last Column Last Row 97-2003 BIFF8 "IV" (2^8) 65536 (2^14) 2007 BIFF12 "XFD" (2^14) 1048576 (2^20)
Input Result "A", "1" true "a", "111" true "A", "65536" true "A", "65537" false "iv", "1" true "IW", "1" false "AAA", "1" false "a", "111" true "Sheet", "1" false
colStr
- a string of only letter charactersrowStr
- a string of only digit characterstrue
if the row and col parameters are within range of a BIFF8 spreadsheet.public static boolean isColumnWithinRange(java.lang.String colStr, SpreadsheetVersion ssVersion)
public static boolean isRowWithinRange(java.lang.String rowStr, SpreadsheetVersion ssVersion)
rowStr
is a valid row number for a given SpreadsheetVersion.rowStr
- the numeric portion of an A1-style cell reference (1-based index)ssVersion
- the spreadsheet versionjava.lang.NumberFormatException
- if rowStr is not parseable as an integerpublic static boolean isRowWithinRange(int rowNum, SpreadsheetVersion ssVersion)
row
is a valid row number for a given SpreadsheetVersion.rowNum
- the row number (0-based index)ssVersion
- the spreadsheet versionpublic static java.lang.String convertNumToColString(int col)
convertNumToColString(3)
returns "D"
public java.lang.String formatAsString()
Example return values:
Result | Comment |
---|---|
A1 | Cell reference without sheet |
Sheet1!A1 | Standard sheet name |
'O''Brien''s Sales'!A1' | Sheet name with special characters |
public java.lang.String formatAsString(boolean includeSheetName)
Example return values:
Result | Comment |
---|---|
A1 | Cell reference without sheet |
Sheet1!A1 | Standard sheet name |
'O''Brien''s Sales'!A1' | Sheet name with special characters |
includeSheetName
- If true and there is a sheet name set for this cell reference,
the reference is prefixed with the sheet name and '!'public java.lang.String toString()
toString
in class java.lang.Object
public java.lang.String[] getCellRefParts()
formatAsString()
to properly turn references into strings.public boolean equals(java.lang.Object o)
Two cells references are assumed to be equal if their string representations
(formatAsString()
are equal.
equals
in class java.lang.Object
public int hashCode()
hashCode
in class java.lang.Object
Copyright 2020 The Apache Software Foundation or its licensors, as applicable.