KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > hssf > util > CellReference


1 /* ====================================================================
2    Copyright 2002-2004 Apache Software Foundation
3
4    Licensed under the Apache License, Version 2.0 (the "License");
5    you may not use this file except in compliance with the License.
6    You may obtain a copy of the License at
7
8        http://www.apache.org/licenses/LICENSE-2.0
9
10    Unless required by applicable law or agreed to in writing, software
11    distributed under the License is distributed on an "AS IS" BASIS,
12    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13    See the License for the specific language governing permissions and
14    limitations under the License.
15 ==================================================================== */

16
17
18 package org.apache.poi.hssf.util;
19
20 /**
21  *
22  * @author Avik Sengupta
23  * @author Dennis Doubleday (patch to seperateRowColumns())
24  */

25 public class CellReference {
26
27     /** Creates new CellReference */
28     private int row;
29     private int col;
30     private String JavaDoc sheetName;
31     private boolean rowAbs;
32     private boolean colAbs;
33
34     public CellReference(String JavaDoc cellRef) {
35         String JavaDoc[] parts = separateRefParts(cellRef);
36         sheetName = parts[0];
37         String JavaDoc ref = parts[1];
38         if (ref.charAt(0) == '$') {
39             colAbs=true;
40             ref=ref.substring(1);
41         }
42         col = convertColStringToNum(ref);
43         ref=parts[2];
44         if (ref.charAt(0) == '$') {
45             rowAbs=true;
46             ref=ref.substring(1);
47         }
48         row = Integer.parseInt(ref)-1;
49     }
50
51     public CellReference(int pRow, int pCol) {
52         this(pRow,pCol,false,false);
53     }
54
55     public CellReference(int pRow, int pCol, boolean pAbsRow, boolean pAbsCol) {
56         row=pRow;col=pCol;
57         rowAbs = pAbsRow;
58         colAbs=pAbsCol;
59
60     }
61
62     public int getRow(){return row;}
63     public short getCol(){return (short) col;}
64     public boolean isRowAbsolute(){return rowAbs;}
65     public boolean isColAbsolute(){return colAbs;}
66     public String JavaDoc getSheetName(){return sheetName;}
67
68     /**
69      * takes in a column reference portion of a CellRef and converts it from
70      * ALPHA-26 number format to 0-based base 10.
71      */

72     private int convertColStringToNum(String JavaDoc ref) {
73         int len = ref.length();
74         int retval=0;
75         int pos = 0;
76
77         for (int k = ref.length()-1; k > -1; k--) {
78             char thechar = ref.charAt(k);
79             if ( pos == 0) {
80                 retval += (Character.getNumericValue(thechar)-9);
81             } else {
82                 retval += (Character.getNumericValue(thechar)-9) * (pos * 26);
83             }
84             pos++;
85         }
86         return retval-1;
87     }
88
89
90     /**
91      * Seperates the row from the columns and returns an array. Element in
92      * position one is the substring containing the columns still in ALPHA-26
93      * number format.
94      */

95     private String JavaDoc[] separateRefParts(String JavaDoc reference) {
96
97         // Look for end of sheet name. This will either set
98
// start to 0 (if no sheet name present) or the
99
// index after the sheet reference ends.
100
String JavaDoc retval[] = new String JavaDoc[3];
101
102         int start = reference.indexOf("!");
103         if (start != -1) retval[0] = reference.substring(0, start);
104         start += 1;
105
106         int length = reference.length();
107
108
109         char[] chars = reference.toCharArray();
110         int loc = start;
111         if (chars[loc]=='$') loc++;
112         for (; loc < chars.length; loc++) {
113             if (Character.isDigit(chars[loc]) || chars[loc] == '$') {
114                 break;
115             }
116         }
117
118         retval[1] = reference.substring(start,loc);
119         retval[2] = reference.substring(loc);
120         return retval;
121     }
122
123     /**
124      * takes in a 0-based base-10 column and returns a ALPHA-26 representation
125      */

126     private static String JavaDoc convertNumToColString(int col) {
127         String JavaDoc retval = null;
128         int mod = col % 26;
129         int div = col / 26;
130         char small=(char)(mod + 65);
131         char big = (char)(div + 64);
132
133         if (div == 0) {
134             retval = ""+small;
135         } else {
136             retval = ""+big+""+small;
137         }
138
139         return retval;
140     }
141
142
143     public String JavaDoc toString() {
144         StringBuffer JavaDoc retval = new StringBuffer JavaDoc();
145         retval.append( (colAbs)?"$":"");
146         retval.append( convertNumToColString(col));
147         retval.append((rowAbs)?"$":"");
148         retval.append(row+1);
149
150     return retval.toString();
151     }
152 }
153
Popular Tags