You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CellReference.java 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.ss.util;
  16. import static org.apache.poi.util.StringUtil.endsWithIgnoreCase;
  17. import java.util.Locale;
  18. import java.util.Map;
  19. import java.util.Objects;
  20. import java.util.function.Supplier;
  21. import java.util.regex.Matcher;
  22. import java.util.regex.Pattern;
  23. import org.apache.poi.common.usermodel.GenericRecord;
  24. import org.apache.poi.ss.SpreadsheetVersion;
  25. import org.apache.poi.ss.formula.SheetNameFormatter;
  26. import org.apache.poi.ss.usermodel.Cell;
  27. import org.apache.poi.util.GenericRecordUtil;
  28. /**
  29. * <p>Common conversion functions between Excel style A1, C27 style
  30. * cell references, and POI usermodel style row=0, column=0
  31. * style references. Handles sheet-based and sheet-free references
  32. * as well, eg "Sheet1!A1" and "$B$72"</p>
  33. *
  34. * <p>Use {@code CellReference} when the concept of
  35. * relative/absolute does apply (such as a cell reference in a formula).
  36. * Use {@link CellAddress} when you want to refer to the location of a cell in a sheet
  37. * when the concept of relative/absolute does not apply (such as the anchor location
  38. * of a cell comment).
  39. * {@code CellReference}s have a concept of "sheet", while {@code CellAddress}es do not.</p>
  40. */
  41. public class CellReference implements GenericRecord {
  42. /**
  43. * Used to classify identifiers found in formulas as cell references or not.
  44. */
  45. public enum NameType {
  46. CELL,
  47. NAMED_RANGE,
  48. COLUMN,
  49. ROW,
  50. BAD_CELL_OR_NAMED_RANGE
  51. }
  52. /** The character ($) that signifies a row or column value is absolute instead of relative */
  53. private static final char ABSOLUTE_REFERENCE_MARKER = '$';
  54. /** The character (!) that separates sheet names from cell references */
  55. private static final char SHEET_NAME_DELIMITER = '!';
  56. /** The character (') used to quote sheet names when they contain special characters */
  57. private static final char SPECIAL_NAME_DELIMITER = '\'';
  58. /**
  59. * Matches a run of one or more letters followed by a run of one or more digits.
  60. * Both the letter and number groups are optional.
  61. * The run of letters is group 1 and the run of digits is group 2.
  62. * Each group may optionally be prefixed with a single '$'.
  63. */
  64. private static final Pattern CELL_REF_PATTERN = Pattern.compile("(\\$?[A-Z]+)?" + "(\\$?[0-9]+)?", Pattern.CASE_INSENSITIVE);
  65. /**
  66. * Matches references only where row and column are included.
  67. * Matches a run of one or more letters followed by a run of one or more digits.
  68. * If a reference does not match this pattern, it might match COLUMN_REF_PATTERN or ROW_REF_PATTERN
  69. * References may optionally include a single '$' before each group, but these are excluded from the Matcher.group(int).
  70. */
  71. private static final Pattern STRICTLY_CELL_REF_PATTERN = Pattern.compile("\\$?([A-Z]+)" + "\\$?([0-9]+)", Pattern.CASE_INSENSITIVE);
  72. /**
  73. * Matches a run of one or more letters. The run of letters is group 1.
  74. * References may optionally include a single '$' before the group, but these are excluded from the Matcher.group(int).
  75. */
  76. private static final Pattern COLUMN_REF_PATTERN = Pattern.compile("\\$?([A-Z]+)", Pattern.CASE_INSENSITIVE);
  77. /**
  78. * Matches a run of one or more letters. The run of numbers is group 1.
  79. * References may optionally include a single '$' before the group, but these are excluded from the Matcher.group(int).
  80. */
  81. private static final Pattern ROW_REF_PATTERN = Pattern.compile("\\$?([0-9]+)");
  82. /**
  83. * Named range names must start with a letter or underscore. Subsequent characters may include
  84. * digits or dot. (They can even end in dot).
  85. */
  86. private static final Pattern NAMED_RANGE_NAME_PATTERN = Pattern.compile("[_A-Z][_.A-Z0-9]*", Pattern.CASE_INSENSITIVE);
  87. //private static final String BIFF8_LAST_COLUMN = SpreadsheetVersion.EXCEL97.getLastColumnName();
  88. //private static final int BIFF8_LAST_COLUMN_TEXT_LEN = BIFF8_LAST_COLUMN.length();
  89. //private static final String BIFF8_LAST_ROW = String.valueOf(SpreadsheetVersion.EXCEL97.getMaxRows());
  90. //private static final int BIFF8_LAST_ROW_TEXT_LEN = BIFF8_LAST_ROW.length();
  91. // FIXME: _sheetName may be null, depending on the entry point.
  92. // Perhaps it would be better to declare _sheetName is never null, using an empty string to represent a 2D reference.
  93. private final String _sheetName;
  94. private final int _rowIndex;
  95. private final int _colIndex;
  96. private final boolean _isRowAbs;
  97. private final boolean _isColAbs;
  98. /**
  99. * Create an cell ref from a string representation. Sheet names containing special characters should be
  100. * delimited and escaped as per normal syntax rules for formulas.
  101. */
  102. public CellReference(String cellRef) {
  103. if(endsWithIgnoreCase(cellRef, "#REF!")) {
  104. throw new IllegalArgumentException("Cell reference invalid: " + cellRef);
  105. }
  106. CellRefParts parts = separateRefParts(cellRef);
  107. _sheetName = parts.sheetName;
  108. String colRef = parts.colRef;
  109. _isColAbs = (colRef.length() > 0) && colRef.charAt(0) == '$';
  110. if (_isColAbs) {
  111. colRef = colRef.substring(1);
  112. }
  113. if (colRef.length() == 0) {
  114. _colIndex = -1;
  115. } else {
  116. _colIndex = convertColStringToIndex(colRef);
  117. }
  118. String rowRef=parts.rowRef;
  119. _isRowAbs = (rowRef.length() > 0) && rowRef.charAt(0) == '$';
  120. if (_isRowAbs) {
  121. rowRef = rowRef.substring(1);
  122. }
  123. if (rowRef.length() == 0) {
  124. _rowIndex = -1;
  125. } else {
  126. // throws NumberFormatException if rowRef is not convertible to an int
  127. _rowIndex = Integer.parseInt(rowRef)-1; // -1 to convert 1-based to zero-based
  128. }
  129. }
  130. public CellReference(int pRow, int pCol) {
  131. this(pRow, pCol, false, false);
  132. }
  133. public CellReference(int pRow, short pCol) {
  134. this(pRow, pCol & 0xFFFF, false, false);
  135. }
  136. public CellReference(Cell cell) {
  137. this(cell.getSheet().getSheetName(), cell.getRowIndex(), cell.getColumnIndex(), false, false);
  138. }
  139. public CellReference(int pRow, int pCol, boolean pAbsRow, boolean pAbsCol) {
  140. this(null, pRow, pCol, pAbsRow, pAbsCol);
  141. }
  142. public CellReference(String pSheetName, int pRow, int pCol, boolean pAbsRow, boolean pAbsCol) {
  143. // TODO - "-1" is a special value being temporarily used for whole row and whole column area references.
  144. // so these checks are currently N.Q.R.
  145. if(pRow < -1) {
  146. throw new IllegalArgumentException("row index may not be negative, but had " + pRow);
  147. }
  148. if(pCol < -1) {
  149. throw new IllegalArgumentException("column index may not be negative, but had " + pCol);
  150. }
  151. _sheetName = pSheetName;
  152. _rowIndex=pRow;
  153. _colIndex=pCol;
  154. _isRowAbs = pAbsRow;
  155. _isColAbs=pAbsCol;
  156. }
  157. public int getRow(){return _rowIndex;}
  158. public short getCol(){return (short) _colIndex;}
  159. public boolean isRowAbsolute(){return _isRowAbs;}
  160. public boolean isColAbsolute(){return _isColAbs;}
  161. /**
  162. * @return possibly {@code null} if this is a 2D reference. Special characters are not
  163. * escaped or delimited
  164. */
  165. public String getSheetName(){
  166. return _sheetName;
  167. }
  168. public static boolean isPartAbsolute(String part) {
  169. return part.charAt(0) == ABSOLUTE_REFERENCE_MARKER;
  170. }
  171. /**
  172. * takes in a column reference portion of a CellRef and converts it from
  173. * ALPHA-26 number format to 0-based base 10.
  174. * 'A' -&gt; 0
  175. * 'Z' -&gt; 25
  176. * 'AA' -&gt; 26
  177. * 'IV' -&gt; 255
  178. * @return zero based column index
  179. */
  180. public static int convertColStringToIndex(String ref) {
  181. int retval=0;
  182. char[] refs = ref.toUpperCase(Locale.ROOT).toCharArray();
  183. for (int k=0; k<refs.length; k++) {
  184. char thechar = refs[k];
  185. if (thechar == ABSOLUTE_REFERENCE_MARKER) {
  186. if (k != 0) {
  187. throw new IllegalArgumentException("Bad col ref format '" + ref + "'");
  188. }
  189. continue;
  190. }
  191. // Character is uppercase letter, find relative value to A
  192. retval = (retval * 26) + (thechar - 'A' + 1);
  193. }
  194. return retval-1;
  195. }
  196. /**
  197. * Classifies an identifier as either a simple (2D) cell reference or a named range name
  198. * @return one of the values from {@code NameType}
  199. */
  200. public static NameType classifyCellReference(String str, SpreadsheetVersion ssVersion) {
  201. int len = str.length();
  202. if (len < 1) {
  203. throw new IllegalArgumentException("Empty string not allowed");
  204. }
  205. char firstChar = str.charAt(0);
  206. switch (firstChar) {
  207. case ABSOLUTE_REFERENCE_MARKER:
  208. case '.':
  209. case '_':
  210. break;
  211. default:
  212. if (!Character.isLetter(firstChar) && !Character.isDigit(firstChar)) {
  213. throw new IllegalArgumentException("Invalid first char (" + firstChar
  214. + ") of cell reference or named range. Letter expected");
  215. }
  216. }
  217. if (!Character.isDigit(str.charAt(len-1))) {
  218. // no digits at end of str
  219. return validateNamedRangeName(str, ssVersion);
  220. }
  221. Matcher cellRefPatternMatcher = STRICTLY_CELL_REF_PATTERN.matcher(str);
  222. if (!cellRefPatternMatcher.matches()) {
  223. return validateNamedRangeName(str, ssVersion);
  224. }
  225. String lettersGroup = cellRefPatternMatcher.group(1);
  226. String digitsGroup = cellRefPatternMatcher.group(2);
  227. if (cellReferenceIsWithinRange(lettersGroup, digitsGroup, ssVersion)) {
  228. // valid cell reference
  229. return NameType.CELL;
  230. }
  231. // If str looks like a cell reference, but is out of (row/col) range, it is a valid
  232. // named range name
  233. // This behaviour is a little weird. For example, "IW123" is a valid named range name
  234. // because the column "IW" is beyond the maximum "IV". Note - this behaviour is version
  235. // dependent. In BIFF12, "IW123" is not a valid named range name, but in BIFF8 it is.
  236. if (str.indexOf(ABSOLUTE_REFERENCE_MARKER) >= 0) {
  237. // Of course, named range names cannot have '$'
  238. return NameType.BAD_CELL_OR_NAMED_RANGE;
  239. }
  240. return NameType.NAMED_RANGE;
  241. }
  242. private static NameType validateNamedRangeName(String str, SpreadsheetVersion ssVersion) {
  243. Matcher colMatcher = COLUMN_REF_PATTERN.matcher(str);
  244. if (colMatcher.matches()) {
  245. String colStr = colMatcher.group(1);
  246. if (isColumnWithinRange(colStr, ssVersion)) {
  247. return NameType.COLUMN;
  248. }
  249. }
  250. Matcher rowMatcher = ROW_REF_PATTERN.matcher(str);
  251. if (rowMatcher.matches()) {
  252. String rowStr = rowMatcher.group(1);
  253. if (isRowWithinRange(rowStr, ssVersion)) {
  254. return NameType.ROW;
  255. }
  256. }
  257. if (!NAMED_RANGE_NAME_PATTERN.matcher(str).matches()) {
  258. return NameType.BAD_CELL_OR_NAMED_RANGE;
  259. }
  260. return NameType.NAMED_RANGE;
  261. }
  262. /**
  263. * Used to decide whether a name of the form "[A-Z]*[0-9]*" that appears in a formula can be
  264. * interpreted as a cell reference. Names of that form can be also used for sheets and/or
  265. * named ranges, and in those circumstances, the question of whether the potential cell
  266. * reference is valid (in range) becomes important.
  267. * <p>
  268. * Note - that the maximum sheet size varies across Excel versions:
  269. * <table>
  270. * <caption>Notable cases.</caption>
  271. * <tr><th>Version&nbsp;&nbsp;</th><th>File Format&nbsp;&nbsp;</th>
  272. * <th>Last Column&nbsp;&nbsp;</th><th>Last Row</th></tr>
  273. * <tr><td>97-2003</td><td>BIFF8</td><td>"IV" (2^8)</td><td>65536 (2^14)</td></tr>
  274. * <tr><td>2007</td><td>BIFF12</td><td>"XFD" (2^14)</td><td>1048576 (2^20)</td></tr>
  275. * </table>
  276. *
  277. * POI currently targets BIFF8 (Excel 97-2003), so the following behaviour can be observed for
  278. * this method:
  279. * <blockquote><table border="0" cellpadding="1" cellspacing="0"
  280. * summary="Notable cases.">
  281. * <tr><th>Input&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</th>
  282. * <th>Result&nbsp;</th></tr>
  283. * <tr><td>"A", "1"</td><td>true</td></tr>
  284. * <tr><td>"a", "111"</td><td>true</td></tr>
  285. * <tr><td>"A", "65536"</td><td>true</td></tr>
  286. * <tr><td>"A", "65537"</td><td>false</td></tr>
  287. * <tr><td>"iv", "1"</td><td>true</td></tr>
  288. * <tr><td>"IW", "1"</td><td>false</td></tr>
  289. * <tr><td>"AAA", "1"</td><td>false</td></tr>
  290. * <tr><td>"a", "111"</td><td>true</td></tr>
  291. * <tr><td>"Sheet", "1"</td><td>false</td></tr>
  292. * </table></blockquote>
  293. *
  294. * @param colStr a string of only letter characters
  295. * @param rowStr a string of only digit characters
  296. * @return {@code true} if the row and col parameters are within range of a BIFF8 spreadsheet.
  297. */
  298. public static boolean cellReferenceIsWithinRange(String colStr, String rowStr, SpreadsheetVersion ssVersion) {
  299. if (!isColumnWithinRange(colStr, ssVersion)) {
  300. return false;
  301. }
  302. return isRowWithinRange(rowStr, ssVersion);
  303. }
  304. public static boolean isColumnWithinRange(String colStr, SpreadsheetVersion ssVersion) {
  305. // Equivalent to 0 <= CellReference.convertColStringToIndex(colStr) <= ssVersion.getLastColumnIndex()
  306. String lastCol = ssVersion.getLastColumnName();
  307. int lastColLength = lastCol.length();
  308. int numberOfLetters = colStr.length();
  309. if(numberOfLetters > lastColLength) {
  310. // "Sheet1" case etc
  311. return false; // that was easy
  312. }
  313. if(numberOfLetters == lastColLength) {
  314. if(colStr.toUpperCase(Locale.ROOT).compareTo(lastCol) > 0) {
  315. return false;
  316. }
  317. } /*else {
  318. // apparent column name has less chars than max
  319. // no need to check range
  320. }*/
  321. return true;
  322. }
  323. /**
  324. * Determines whether {@code rowStr} is a valid row number for a given SpreadsheetVersion.
  325. * @param rowStr the numeric portion of an A1-style cell reference (1-based index)
  326. * @param ssVersion the spreadsheet version
  327. * @throws NumberFormatException if rowStr is not parseable as an integer
  328. */
  329. public static boolean isRowWithinRange(String rowStr, SpreadsheetVersion ssVersion) {
  330. final long rowNum = Long.parseLong(rowStr) - 1;
  331. if(rowNum > Integer.MAX_VALUE) {
  332. return false;
  333. }
  334. return isRowWithinRange(Math.toIntExact(rowNum), ssVersion);
  335. }
  336. /**
  337. * Determines whether {@code row} is a valid row number for a given SpreadsheetVersion.
  338. * @param rowNum the row number (0-based index)
  339. * @param ssVersion the spreadsheet version
  340. * @since 3.17 beta 1
  341. */
  342. public static boolean isRowWithinRange(int rowNum, SpreadsheetVersion ssVersion) {
  343. return 0 <= rowNum && rowNum <= ssVersion.getLastRowIndex();
  344. }
  345. private static final class CellRefParts {
  346. final String sheetName;
  347. final String rowRef;
  348. final String colRef;
  349. private CellRefParts(String sheetName, String rowRef, String colRef) {
  350. this.sheetName = sheetName;
  351. this.rowRef = (rowRef != null) ? rowRef : "";
  352. this.colRef = (colRef != null) ? colRef : "";
  353. }
  354. }
  355. /**
  356. * Separates the sheet name, row, and columns from a cell reference string.
  357. *
  358. * @param reference is a string that identifies a cell within the sheet or workbook
  359. * reference may not refer to a cell in an external workbook
  360. * reference may be absolute or relative.
  361. * @return String array of sheetName, column (in ALPHA-26 format), and row
  362. * output column or row elements will contain absolute reference markers if they
  363. * existed in the input reference.
  364. */
  365. private static CellRefParts separateRefParts(String reference) {
  366. int plingPos = reference.lastIndexOf(SHEET_NAME_DELIMITER);
  367. final String sheetName = parseSheetName(reference, plingPos);
  368. String cell = reference.substring(plingPos+1).toUpperCase(Locale.ROOT);
  369. Matcher matcher = CELL_REF_PATTERN.matcher(cell);
  370. if (!matcher.matches()) {
  371. throw new IllegalArgumentException("Invalid CellReference: " + reference);
  372. }
  373. String col = matcher.group(1);
  374. String row = matcher.group(2);
  375. return new CellRefParts(sheetName, row, col);
  376. }
  377. private static String parseSheetName(String reference, int indexOfSheetNameDelimiter) {
  378. if(indexOfSheetNameDelimiter < 0) {
  379. return null;
  380. }
  381. boolean isQuoted = reference.charAt(0) == SPECIAL_NAME_DELIMITER;
  382. if(!isQuoted) {
  383. // sheet names with spaces must be quoted
  384. if (! reference.contains(" ")) {
  385. return reference.substring(0, indexOfSheetNameDelimiter);
  386. } else {
  387. throw new IllegalArgumentException("Sheet names containing spaces must be quoted: (" + reference + ")");
  388. }
  389. }
  390. int lastQuotePos = indexOfSheetNameDelimiter-1;
  391. if(reference.charAt(lastQuotePos) != SPECIAL_NAME_DELIMITER) {
  392. throw new IllegalArgumentException("Mismatched quotes: (" + reference + ")");
  393. }
  394. // TODO - refactor cell reference parsing logic to one place.
  395. // Current known incarnations:
  396. // FormulaParser.GetName()
  397. // CellReference.parseSheetName() (here)
  398. // AreaReference.separateAreaRefs()
  399. // SheetNameFormatter.format() (inverse)
  400. StringBuilder sb = new StringBuilder(indexOfSheetNameDelimiter);
  401. for(int i=1; i<lastQuotePos; i++) { // Note boundaries - skip outer quotes
  402. char ch = reference.charAt(i);
  403. if(ch != SPECIAL_NAME_DELIMITER) {
  404. sb.append(ch);
  405. continue;
  406. }
  407. if(i+1 < lastQuotePos && reference.charAt(i+1) == SPECIAL_NAME_DELIMITER) {
  408. // two consecutive quotes is the escape sequence for a single one
  409. i++; // skip this and keep parsing the special name
  410. sb.append(ch);
  411. continue;
  412. }
  413. throw new IllegalArgumentException("Bad sheet name quote escaping: (" + reference + ")");
  414. }
  415. return sb.toString();
  416. }
  417. /**
  418. * Takes in a 0-based base-10 column and returns a ALPHA-26
  419. * representation.
  420. * eg {@code convertNumToColString(3)} returns {@code "D"}
  421. */
  422. public static String convertNumToColString(int col) {
  423. // Excel counts column A as the 1st column, we
  424. // treat it as the 0th one
  425. int excelColNum = col + 1;
  426. StringBuilder colRef = new StringBuilder(2);
  427. int colRemain = excelColNum;
  428. while(colRemain > 0) {
  429. int thisPart = colRemain % 26;
  430. if(thisPart == 0) { thisPart = 26; }
  431. colRemain = (colRemain - thisPart) / 26;
  432. // The letter A is at 65
  433. char colChar = (char)(thisPart+64);
  434. colRef.insert(0, colChar);
  435. }
  436. return colRef.toString();
  437. }
  438. /**
  439. * Returns a text representation of this cell reference.
  440. * <p>
  441. * Example return values:
  442. * <table>
  443. * <caption>Example return values</caption>
  444. * <tr><th align='left'>Result</th><th align='left'>Comment</th></tr>
  445. * <tr><td>A1</td><td>Cell reference without sheet</td></tr>
  446. * <tr><td>Sheet1!A1</td><td>Standard sheet name</td></tr>
  447. * <tr><td>'O''Brien''s Sales'!A1'&nbsp;</td><td>Sheet name with special characters</td></tr>
  448. * </table>
  449. * @return the text representation of this cell reference as it would appear in a formula.
  450. */
  451. public String formatAsString() {
  452. return formatAsString(true);
  453. }
  454. /**
  455. * Returns a text representation of this cell reference and allows to control
  456. * if the sheetname is included in the reference.
  457. *
  458. * <p>
  459. * Example return values:
  460. * <table border="0" cellpadding="1" cellspacing="0" summary="Example return values">
  461. * <tr><th align='left'>Result</th><th align='left'>Comment</th></tr>
  462. * <tr><td>A1</td><td>Cell reference without sheet</td></tr>
  463. * <tr><td>Sheet1!A1</td><td>Standard sheet name</td></tr>
  464. * <tr><td>'O''Brien''s Sales'!A1'&nbsp;</td><td>Sheet name with special characters</td></tr>
  465. * </table>
  466. * @param includeSheetName If true and there is a sheet name set for this cell reference,
  467. * the reference is prefixed with the sheet name and '!'
  468. * @return the text representation of this cell reference as it would appear in a formula.
  469. */
  470. public String formatAsString(boolean includeSheetName) {
  471. StringBuilder sb = new StringBuilder(32);
  472. if(includeSheetName && _sheetName != null) {
  473. SheetNameFormatter.appendFormat(sb, _sheetName);
  474. sb.append(SHEET_NAME_DELIMITER);
  475. }
  476. appendCellReference(sb);
  477. return sb.toString();
  478. }
  479. @Override
  480. public String toString() {
  481. return getClass().getName() + " [" + formatAsString() + "]";
  482. }
  483. /**
  484. * Returns the three parts of the cell reference, the
  485. * Sheet name (or null if none supplied), the 1 based
  486. * row number, and the A based column letter.
  487. * This will not include any markers for absolute
  488. * references, so use {@link #formatAsString()}
  489. * to properly turn references into strings.
  490. * @return String array of { sheetName, rowString, colString }
  491. */
  492. public String[] getCellRefParts() {
  493. return new String[] {
  494. _sheetName,
  495. Integer.toString(_rowIndex+1),
  496. convertNumToColString(_colIndex)
  497. };
  498. }
  499. /**
  500. * Appends cell reference with '$' markers for absolute values as required.
  501. * Sheet name is not included.
  502. */
  503. /* package */ void appendCellReference(StringBuilder sb) {
  504. if (_colIndex != -1) {
  505. if(_isColAbs) {
  506. sb.append(ABSOLUTE_REFERENCE_MARKER);
  507. }
  508. sb.append( convertNumToColString(_colIndex));
  509. }
  510. if (_rowIndex != -1) {
  511. if(_isRowAbs) {
  512. sb.append(ABSOLUTE_REFERENCE_MARKER);
  513. }
  514. sb.append(_rowIndex+1);
  515. }
  516. }
  517. /**
  518. * Checks whether this cell reference is equal to another object.
  519. * <p>
  520. * Two cells references are assumed to be equal if their string representations
  521. * ({@link #formatAsString()} are equal.
  522. * </p>
  523. */
  524. @Override
  525. public boolean equals(Object o){
  526. if (this == o) {
  527. return true;
  528. }
  529. if(!(o instanceof CellReference)) {
  530. return false;
  531. }
  532. CellReference cr = (CellReference) o;
  533. return _rowIndex == cr._rowIndex
  534. && _colIndex == cr._colIndex
  535. && _isRowAbs == cr._isRowAbs
  536. && _isColAbs == cr._isColAbs
  537. && ((_sheetName == null)
  538. ? (cr._sheetName == null)
  539. : _sheetName.equals(cr._sheetName));
  540. }
  541. @Override
  542. public int hashCode() {
  543. return Objects.hash(_rowIndex,_colIndex,_isRowAbs,_isColAbs,_sheetName);
  544. }
  545. @Override
  546. public Map<String, Supplier<?>> getGenericProperties() {
  547. return GenericRecordUtil.getGenericProperties(
  548. "sheetName", this::getSheetName,
  549. "rowIndex", this::getRow,
  550. "colIndex", this::getCol,
  551. "rowAbs", this::isRowAbsolute,
  552. "colAbs", this::isColAbsolute,
  553. "formatAsString", this::formatAsString
  554. );
  555. }
  556. }