001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017:
018: package org.apache.poi.hssf.util;
019:
020: /**
021: *
022: * @author Avik Sengupta
023: * @author Dennis Doubleday (patch to seperateRowColumns())
024: */
025: public class CellReference {
026:
027: /** Creates new CellReference */
028: private int row;
029: private int col;
030: private String sheetName;
031: private boolean rowAbs;
032: private boolean colAbs;
033:
034: public CellReference(String cellRef) {
035: String[] parts = separateRefParts(cellRef);
036: sheetName = parts[0];
037: String ref = parts[1];
038: if ((ref == null) || ("".equals(ref)))
039: throw new IllegalArgumentException(
040: "Invalid Formula cell reference: '" + cellRef + "'");
041: if (ref.charAt(0) == '$') {
042: colAbs = true;
043: ref = ref.substring(1);
044: }
045: col = convertColStringToNum(ref);
046: ref = parts[2];
047: if ((ref == null) || ("".equals(ref)))
048: throw new IllegalArgumentException(
049: "Invalid Formula cell reference: '" + cellRef + "'");
050: if (ref.charAt(0) == '$') {
051: rowAbs = true;
052: ref = ref.substring(1);
053: }
054: row = Integer.parseInt(ref) - 1;
055: }
056:
057: public CellReference(int pRow, int pCol) {
058: this (pRow, pCol, false, false);
059: }
060:
061: public CellReference(int pRow, int pCol, boolean pAbsRow,
062: boolean pAbsCol) {
063: row = pRow;
064: col = pCol;
065: rowAbs = pAbsRow;
066: colAbs = pAbsCol;
067:
068: }
069:
070: public int getRow() {
071: return row;
072: }
073:
074: public short getCol() {
075: return (short) col;
076: }
077:
078: public boolean isRowAbsolute() {
079: return rowAbs;
080: }
081:
082: public boolean isColAbsolute() {
083: return colAbs;
084: }
085:
086: public String getSheetName() {
087: return sheetName;
088: }
089:
090: /**
091: * takes in a column reference portion of a CellRef and converts it from
092: * ALPHA-26 number format to 0-based base 10.
093: */
094: private int convertColStringToNum(String ref) {
095: int len = ref.length();
096: int retval = 0;
097: int pos = 0;
098:
099: for (int k = ref.length() - 1; k > -1; k--) {
100: char thechar = ref.charAt(k);
101: if (pos == 0) {
102: retval += (Character.getNumericValue(thechar) - 9);
103: } else {
104: retval += (Character.getNumericValue(thechar) - 9)
105: * (pos * 26);
106: }
107: pos++;
108: }
109: return retval - 1;
110: }
111:
112: /**
113: * Seperates the row from the columns and returns an array. Element in
114: * position one is the substring containing the columns still in ALPHA-26
115: * number format.
116: */
117: private String[] separateRefParts(String reference) {
118:
119: // Look for end of sheet name. This will either set
120: // start to 0 (if no sheet name present) or the
121: // index after the sheet reference ends.
122: String retval[] = new String[3];
123:
124: int start = reference.indexOf("!");
125: if (start != -1)
126: retval[0] = reference.substring(0, start);
127: start += 1;
128:
129: int length = reference.length();
130:
131: char[] chars = reference.toCharArray();
132: int loc = start;
133: if (chars[loc] == '$')
134: loc++;
135: for (; loc < chars.length; loc++) {
136: if (Character.isDigit(chars[loc]) || chars[loc] == '$') {
137: break;
138: }
139: }
140:
141: retval[1] = reference.substring(start, loc);
142: retval[2] = reference.substring(loc);
143: return retval;
144: }
145:
146: /**
147: * takes in a 0-based base-10 column and returns a ALPHA-26 representation
148: */
149: private static String convertNumToColString(int col) {
150: String retval = null;
151: int mod = col % 26;
152: int div = col / 26;
153: char small = (char) (mod + 65);
154: char big = (char) (div + 64);
155:
156: if (div == 0) {
157: retval = "" + small;
158: } else {
159: retval = "" + big + "" + small;
160: }
161:
162: return retval;
163: }
164:
165: public String toString() {
166: StringBuffer retval = new StringBuffer();
167: retval.append((colAbs) ? "$" : "");
168: retval.append(convertNumToColString(col));
169: retval.append((rowAbs) ? "$" : "");
170: retval.append(row + 1);
171:
172: return retval.toString();
173: }
174: }
|