1 19 20 package jxl.biff; 21 22 import common.Logger; 23 24 import jxl.Cell; 25 import jxl.biff.formula.ExternalSheet; 26 27 35 public final class CellReferenceHelper 36 { 37 40 private static Logger logger = Logger.getLogger(CellReferenceHelper.class); 41 42 45 private static final char fixedInd='$'; 46 47 50 private CellReferenceHelper() 51 { 52 } 53 54 61 public static void getCellReference(int column, int row, StringBuffer buf) 62 { 63 getColumnReference(column, buf); 65 66 buf.append(Integer.toString(row+1)); 68 } 69 70 79 public static void getCellReference(int column, boolean colabs, 80 int row, boolean rowabs, 81 StringBuffer buf) 82 { 83 if (colabs) 84 { 85 buf.append(fixedInd); 86 } 87 88 getColumnReference(column, buf); 90 91 if (rowabs) 92 { 93 buf.append(fixedInd); 94 } 95 96 buf.append(Integer.toString(row+1)); 98 } 99 100 106 public static String getColumnReference(int column) 107 { 108 StringBuffer buf = new StringBuffer (); 109 getColumnReference(column, buf); 110 return buf.toString(); 111 } 112 113 119 public static void getColumnReference(int column, StringBuffer buf) 120 { 121 int v = column/26; 122 int r = column%26; 123 124 StringBuffer tmp = new StringBuffer (); 125 while (v != 0) 126 { 127 char col = (char) ((int) 'A' + r) ; 128 129 tmp.append(col); 130 131 r = v%26 - 1; v = v/26; 133 } 134 135 char col = (char) ((int) 'A' + r) ; 136 tmp.append(col); 137 138 for (int i = tmp.length() - 1; i >= 0; i--) 140 { 141 buf.append(tmp.charAt(i)); 142 } 143 } 144 145 155 public static void getCellReference 156 (int sheet, int column, int row, 157 ExternalSheet workbook, StringBuffer buf) 158 { 159 buf.append('\''); 160 buf.append(workbook.getExternalSheetName(sheet)); 161 buf.append('\''); 162 buf.append('!'); 163 getCellReference(column, row, buf); 164 } 165 166 178 public static void getCellReference 179 (int sheet, int column, boolean colabs, 180 int row, boolean rowabs, 181 ExternalSheet workbook, StringBuffer buf) 182 { 183 buf.append('\''); 184 buf.append(workbook.getExternalSheetName(sheet)); 185 buf.append('\''); 186 buf.append('!'); 187 getCellReference(column, colabs, row, rowabs, buf); 188 } 189 190 200 public static String getCellReference 201 (int sheet, int column, int row, 202 ExternalSheet workbook) 203 { 204 StringBuffer sb = new StringBuffer (); 205 getCellReference(sheet, column, row, workbook, sb); 206 return sb.toString(); 207 } 208 209 210 217 public static String getCellReference(int column, int row) 218 { 219 StringBuffer buf = new StringBuffer (); 220 getCellReference(column, row, buf); 221 return buf.toString(); 222 } 223 224 230 public static int getColumn(String s) 231 { 232 int colnum = 0; 233 int numindex = getNumberIndex(s); 234 235 String s2 = s.toUpperCase(); 236 237 int startPos = 0; 238 if (s.charAt(0) == fixedInd) 239 { 240 startPos = 1; 241 } 242 243 int endPos = numindex; 244 if (s.charAt(numindex - 1) == fixedInd) 245 { 246 endPos--; 247 } 248 249 for (int i = startPos; i < endPos ; i++) 250 { 251 252 if (i != startPos) 253 { 254 colnum = (colnum+1) * 26; 255 } 256 colnum += (int) s2.charAt(i) - (int) 'A'; 257 } 258 259 return colnum; 260 } 261 262 265 public static int getRow(String s) 266 { 267 try 268 { 269 return (Integer.parseInt(s.substring(getNumberIndex(s))) - 1); 270 } 271 catch (NumberFormatException e) 272 { 273 logger.warn(e, e); 274 return 0xffff; 275 } 276 } 277 278 281 private static int getNumberIndex(String s) 282 { 283 boolean numberFound = false; 285 int pos = 0; 286 char c = '\0'; 287 288 while (!numberFound && pos < s.length() ) 289 { 290 c = s.charAt(pos); 291 292 if (c >= '0' && c <= '9') 293 { 294 numberFound = true; 295 } 296 else 297 { 298 pos++; 299 } 300 } 301 302 return pos; 303 } 304 305 311 public static boolean isColumnRelative(String s) 312 { 313 return s.charAt(0) != fixedInd; 314 } 315 316 322 public static boolean isRowRelative(String s) 323 { 324 return s.charAt(getNumberIndex(s) - 1) != fixedInd; 325 } 326 327 } 328 | Popular Tags |