| Constructor: |
public CellReference(String cellRef) {
String[] parts = separateRefParts(cellRef);
_sheetName = parts[0];
String colRef = parts[1];
if (colRef.length() < 1) {
throw new IllegalArgumentException("Invalid Formula cell reference: '"+cellRef+"'");
}
_isColAbs = colRef.charAt(0) == '$";
if (_isColAbs) {
colRef=colRef.substring(1);
}
_colIndex = convertColStringToNum(colRef);
String rowRef=parts[2];
if (rowRef.length() < 1) {
throw new IllegalArgumentException("Invalid Formula cell reference: '"+cellRef+"'");
}
_isRowAbs = rowRef.charAt(0) == '$";
if (_isRowAbs) {
rowRef=rowRef.substring(1);
}
_rowIndex = Integer.parseInt(rowRef)-1; // -1 to convert 1-based to zero-based
}
Create an cell ref from a string representation. Sheet names containing special characters should be
delimited and escaped as per normal syntax rules for formulas. |
public CellReference(int pRow,
int pCol,
boolean pAbsRow,
boolean pAbsCol) {
this(null, pRow, pCol, pAbsRow, pAbsCol);
}
|
public CellReference(String pSheetName,
int pRow,
int pCol,
boolean pAbsRow,
boolean pAbsCol) {
// TODO - "-1" is a special value being temporarily used for whole row and whole column area references.
// so these checks are currently N.Q.R.
if(pRow < -1) {
throw new IllegalArgumentException("row index may not be negative");
}
if(pCol < -1) {
throw new IllegalArgumentException("column index may not be negative");
}
_sheetName = pSheetName;
_rowIndex=pRow;
_colIndex=pCol;
_isRowAbs = pAbsRow;
_isColAbs=pAbsCol;
}
|
| Method from org.apache.poi.hssf.util.CellReference Detail: |
void appendCellReference(StringBuffer sb) {
if(_isColAbs) {
sb.append(ABSOLUTE_REFERENCE_MARKER);
}
sb.append( convertNumToColString(_colIndex));
if(_isRowAbs) {
sb.append(ABSOLUTE_REFERENCE_MARKER);
}
sb.append(_rowIndex+1);
}
Appends cell reference with '$' markers for absolute values as required.
Sheet name is not included. |
protected static String convertNumToColString(int col) {
String retval = null;
int mod = col % 26;
int div = col / 26;
char small=(char)(mod + 65);
char big = (char)(div + 64);
if (div == 0) {
retval = ""+small;
} else {
retval = ""+big+""+small;
}
return retval;
}
Takes in a 0-based base-10 column and returns a ALPHA-26
representation.
eg column #3 -> D |
public String formatAsString() {
StringBuffer sb = new StringBuffer(32);
if(_sheetName != null) {
SheetNameFormatter.appendFormat(sb, _sheetName);
sb.append(SHEET_NAME_DELIMITER);
}
appendCellReference(sb);
return sb.toString();
}
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 short getCol() {
return (short) _colIndex;
}
|
public int getRow() {
return _rowIndex;
}
|
public String getSheetName() {
return _sheetName;
}
|
public boolean isColAbsolute() {
return _isColAbs;
}
|
public boolean isRowAbsolute() {
return _isRowAbs;
}
|
public String toString() {
StringBuffer sb = new StringBuffer(64);
sb.append(getClass().getName()).append(" [");
sb.append(formatAsString());
sb.append("]");
return sb.toString();
}
|