FileDocCategorySizeDatePackage
CellReference.javaAPI DocApache Poi 3.0.14936Mon Jan 01 12:39:36 GMT 2007org.apache.poi.hssf.util

CellReference

public class CellReference extends Object
author
Avik Sengupta
author
Dennis Doubleday (patch to seperateRowColumns())

Fields Summary
private int
row
Creates new CellReference
private int
col
private String
sheetName
private boolean
rowAbs
private boolean
colAbs
Constructors Summary
public CellReference(String cellRef)

        String[] parts = separateRefParts(cellRef);
        sheetName = parts[0];
        String ref = parts[1]; 
        if ((ref == null)||("".equals(ref)))
        	throw new IllegalArgumentException("Invalid Formula cell reference: '"+cellRef+"'");
        if (ref.charAt(0) == '$") {
            colAbs=true;
            ref=ref.substring(1);
        }
        col = convertColStringToNum(ref);
        ref=parts[2];
        if ((ref == null)||("".equals(ref)))
        	throw new IllegalArgumentException("Invalid Formula cell reference: '"+cellRef+"'");
        if (ref.charAt(0) == '$") {
            rowAbs=true;
            ref=ref.substring(1);
        }
        row = Integer.parseInt(ref)-1;
    
public CellReference(int pRow, int pCol)

        this(pRow,pCol,false,false);
    
public CellReference(int pRow, int pCol, boolean pAbsRow, boolean pAbsCol)

        row=pRow;col=pCol;
        rowAbs = pAbsRow;
        colAbs=pAbsCol;

    
Methods Summary
private intconvertColStringToNum(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.

        int len = ref.length();
        int retval=0;
        int pos = 0;

        for (int k = ref.length()-1; k > -1; k--) {
            char thechar = ref.charAt(k);
            if ( pos == 0) {
                retval += (Character.getNumericValue(thechar)-9);
            } else {
                retval += (Character.getNumericValue(thechar)-9) * (pos * 26);
            }
            pos++;
        }
        return retval-1;
    
private static java.lang.StringconvertNumToColString(int col)
takes in a 0-based base-10 column and returns a ALPHA-26 representation

        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;
    
public shortgetCol()

return (short) col;
public intgetRow()

return row;
public java.lang.StringgetSheetName()

return sheetName;
public booleanisColAbsolute()

return colAbs;
public booleanisRowAbsolute()

return rowAbs;
private java.lang.String[]separateRefParts(java.lang.String reference)
Seperates the row from the columns and returns an array. Element in position one is the substring containing the columns still in ALPHA-26 number format.


        // Look for end of sheet name. This will either set
        // start to 0 (if no sheet name present) or the
        // index after the sheet reference ends.
        String retval[] = new String[3];

        int start = reference.indexOf("!");
        if (start != -1) retval[0] = reference.substring(0, start);
        start += 1;

        int length = reference.length();


        char[] chars = reference.toCharArray();
        int loc = start;
        if (chars[loc]=='$") loc++;
        for (; loc < chars.length; loc++) {
            if (Character.isDigit(chars[loc]) || chars[loc] == '$") {
                break;
            }
        }

        retval[1] = reference.substring(start,loc);
        retval[2] = reference.substring(loc);
        return retval;
    
public java.lang.StringtoString()

        StringBuffer retval = new StringBuffer();
        retval.append( (colAbs)?"$":"");
        retval.append( convertNumToColString(col));
        retval.append((rowAbs)?"$":"");
        retval.append(row+1);

    return retval.toString();