2002-05-04 15:45:05 +00:00
|
|
|
/* ====================================================================
|
|
|
|
|
* The Apache Software License, Version 1.1
|
|
|
|
|
*
|
2003-04-30 04:39:21 +00:00
|
|
|
* Copyright (c) 2003 The Apache Software Foundation. All rights
|
2002-05-04 15:45:05 +00:00
|
|
|
* reserved.
|
|
|
|
|
*
|
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
|
* are met:
|
|
|
|
|
*
|
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
|
*
|
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
|
* distribution.
|
|
|
|
|
*
|
|
|
|
|
* 3. The end-user documentation included with the redistribution,
|
|
|
|
|
* if any, must include the following acknowledgment:
|
|
|
|
|
* "This product includes software developed by the
|
|
|
|
|
* Apache Software Foundation (http://www.apache.org/)."
|
|
|
|
|
* Alternately, this acknowledgment may appear in the software itself,
|
|
|
|
|
* if and wherever such third-party acknowledgments normally appear.
|
|
|
|
|
*
|
|
|
|
|
* 4. The names "Apache" and "Apache Software Foundation" and
|
|
|
|
|
* "Apache POI" must not be used to endorse or promote products
|
|
|
|
|
* derived from this software without prior written permission. For
|
|
|
|
|
* written permission, please contact apache@apache.org.
|
|
|
|
|
*
|
|
|
|
|
* 5. Products derived from this software may not be called "Apache",
|
|
|
|
|
* "Apache POI", nor may "Apache" appear in their name, without
|
|
|
|
|
* prior written permission of the Apache Software Foundation.
|
|
|
|
|
*
|
|
|
|
|
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
|
|
|
|
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
|
|
|
|
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
|
|
|
|
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
|
|
|
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
|
|
|
|
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
|
* ====================================================================
|
|
|
|
|
*
|
|
|
|
|
* This software consists of voluntary contributions made by many
|
|
|
|
|
* individuals on behalf of the Apache Software Foundation. For more
|
|
|
|
|
* information on the Apache Software Foundation, please see
|
|
|
|
|
* <http://www.apache.org/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package org.apache.poi.hssf.util;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @author Avik Sengupta
|
2002-11-28 19:26:20 +00:00
|
|
|
* @author Dennis Doubleday (patch to seperateRowColumns())
|
2002-05-04 15:45:05 +00:00
|
|
|
*/
|
|
|
|
|
public class CellReference {
|
2003-05-17 17:53:38 +00:00
|
|
|
|
2002-05-04 15:45:05 +00:00
|
|
|
/** Creates new CellReference */
|
|
|
|
|
private int row;
|
|
|
|
|
private int col;
|
2003-05-17 17:53:38 +00:00
|
|
|
private String sheetName;
|
2002-05-04 15:45:05 +00:00
|
|
|
private boolean rowAbs;
|
|
|
|
|
private boolean colAbs;
|
2003-05-17 17:53:38 +00:00
|
|
|
|
2002-05-04 15:45:05 +00:00
|
|
|
public CellReference(String cellRef) {
|
2003-05-17 17:53:38 +00:00
|
|
|
String[] parts = separateRefParts(cellRef);
|
|
|
|
|
sheetName = parts[0];
|
|
|
|
|
String ref = parts[1];
|
2002-05-04 15:45:05 +00:00
|
|
|
if (ref.charAt(0) == '$') {
|
2003-05-17 17:53:38 +00:00
|
|
|
colAbs=true;
|
2002-05-04 15:45:05 +00:00
|
|
|
ref=ref.substring(1);
|
|
|
|
|
}
|
|
|
|
|
col = convertColStringToNum(ref);
|
2003-05-17 17:53:38 +00:00
|
|
|
ref=parts[2];
|
2002-05-04 15:45:05 +00:00
|
|
|
if (ref.charAt(0) == '$') {
|
2003-05-17 17:53:38 +00:00
|
|
|
rowAbs=true;
|
2002-05-04 15:45:05 +00:00
|
|
|
ref=ref.substring(1);
|
|
|
|
|
}
|
|
|
|
|
row = Integer.parseInt(ref)-1;
|
|
|
|
|
}
|
2003-05-17 17:53:38 +00:00
|
|
|
|
2002-05-04 15:45:05 +00:00
|
|
|
public CellReference(int pRow, int pCol) {
|
|
|
|
|
this(pRow,pCol,false,false);
|
|
|
|
|
}
|
2003-05-17 17:53:38 +00:00
|
|
|
|
2002-05-04 15:45:05 +00:00
|
|
|
public CellReference(int pRow, int pCol, boolean pAbsRow, boolean pAbsCol) {
|
|
|
|
|
row=pRow;col=pCol;
|
|
|
|
|
rowAbs = pAbsRow;
|
|
|
|
|
colAbs=pAbsCol;
|
2003-05-17 17:53:38 +00:00
|
|
|
|
2002-05-04 15:45:05 +00:00
|
|
|
}
|
2003-05-17 17:53:38 +00:00
|
|
|
|
2002-05-04 15:45:05 +00:00
|
|
|
public int getRow(){return row;}
|
2003-05-17 17:53:38 +00:00
|
|
|
public short getCol(){return (short) col;}
|
2002-05-04 15:45:05 +00:00
|
|
|
public boolean isRowAbsolute(){return rowAbs;}
|
|
|
|
|
public boolean isColAbsolute(){return colAbs;}
|
2003-05-17 17:53:38 +00:00
|
|
|
public String getSheetName(){return sheetName;}
|
|
|
|
|
|
2002-05-04 15:45:05 +00:00
|
|
|
/**
|
|
|
|
|
* takes in a column reference portion of a CellRef and converts it from
|
|
|
|
|
* ALPHA-26 number format to 0-based base 10.
|
|
|
|
|
*/
|
|
|
|
|
private int convertColStringToNum(String ref) {
|
|
|
|
|
int len = ref.length();
|
|
|
|
|
int retval=0;
|
|
|
|
|
int pos = 0;
|
2003-05-17 17:53:38 +00:00
|
|
|
|
2002-05-04 15:45:05 +00:00
|
|
|
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;
|
|
|
|
|
}
|
2003-05-17 17:53:38 +00:00
|
|
|
|
|
|
|
|
|
2002-05-04 15:45:05 +00:00
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
2003-05-17 17:53:38 +00:00
|
|
|
private String[] separateRefParts(String reference) {
|
|
|
|
|
|
2002-11-28 19:26:20 +00:00
|
|
|
// 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.
|
2003-05-17 17:53:38 +00:00
|
|
|
String retval[] = new String[3];
|
|
|
|
|
|
|
|
|
|
int start = reference.indexOf("!");
|
|
|
|
|
if (start != -1) retval[0] = reference.substring(0, start);
|
|
|
|
|
start += 1;
|
2002-11-28 19:26:20 +00:00
|
|
|
|
2002-05-04 15:45:05 +00:00
|
|
|
int length = reference.length();
|
2002-11-28 19:26:20 +00:00
|
|
|
|
|
|
|
|
|
2002-05-04 15:45:05 +00:00
|
|
|
char[] chars = reference.toCharArray();
|
2002-11-28 19:26:20 +00:00
|
|
|
int loc = start;
|
2002-05-04 15:45:05 +00:00
|
|
|
if (chars[loc]=='$') loc++;
|
|
|
|
|
for (; loc < chars.length; loc++) {
|
|
|
|
|
if (Character.isDigit(chars[loc]) || chars[loc] == '$') {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2003-05-17 17:53:38 +00:00
|
|
|
|
|
|
|
|
retval[1] = reference.substring(start,loc);
|
|
|
|
|
retval[2] = reference.substring(loc);
|
2002-05-04 15:45:05 +00:00
|
|
|
return retval;
|
|
|
|
|
}
|
2003-05-17 17:53:38 +00:00
|
|
|
|
2002-05-04 15:45:05 +00:00
|
|
|
/**
|
|
|
|
|
* takes in a 0-based base-10 column and returns a ALPHA-26 representation
|
|
|
|
|
*/
|
|
|
|
|
private 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);
|
2003-05-17 17:53:38 +00:00
|
|
|
|
2002-05-04 15:45:05 +00:00
|
|
|
if (div == 0) {
|
|
|
|
|
retval = ""+small;
|
|
|
|
|
} else {
|
|
|
|
|
retval = ""+big+""+small;
|
|
|
|
|
}
|
2003-05-17 17:53:38 +00:00
|
|
|
|
2002-05-04 15:45:05 +00:00
|
|
|
return retval;
|
|
|
|
|
}
|
2003-05-17 17:53:38 +00:00
|
|
|
|
|
|
|
|
|
2002-05-04 15:45:05 +00:00
|
|
|
public String toString() {
|
|
|
|
|
StringBuffer retval = new StringBuffer();
|
|
|
|
|
retval.append( (colAbs)?"$":"");
|
|
|
|
|
retval.append( convertNumToColString(col));
|
|
|
|
|
retval.append((rowAbs)?"$":"");
|
|
|
|
|
retval.append(row+1);
|
2003-05-17 17:53:38 +00:00
|
|
|
|
2002-05-04 15:45:05 +00:00
|
|
|
return retval.toString();
|
|
|
|
|
}
|
|
|
|
|
}
|