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.

CellRangeAddressBase.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 org.apache.poi.ss.SpreadsheetVersion;
  17. /**
  18. * See OOO documentation: excelfileformat.pdf sec 2.5.14 - 'Cell Range Address'<p/>
  19. *
  20. * Common subclass of 8-bit and 16-bit versions
  21. *
  22. * @author Josh Micich
  23. */
  24. public abstract class CellRangeAddressBase {
  25. private int _firstRow;
  26. private int _firstCol;
  27. private int _lastRow;
  28. private int _lastCol;
  29. protected CellRangeAddressBase(int firstRow, int lastRow, int firstCol, int lastCol) {
  30. _firstRow = firstRow;
  31. _lastRow = lastRow;
  32. _firstCol = firstCol;
  33. _lastCol = lastCol;
  34. }
  35. /**
  36. * Validate the range limits against the supplied version of Excel
  37. *
  38. * @param ssVersion the version of Excel to validate against
  39. * @throws IllegalArgumentException if the range limits are outside of the allowed range
  40. */
  41. public void validate(SpreadsheetVersion ssVersion) {
  42. validateRow(_firstRow, ssVersion);
  43. validateRow(_lastRow, ssVersion);
  44. validateColumn(_firstCol, ssVersion);
  45. validateColumn(_lastCol, ssVersion);
  46. }
  47. /**
  48. * Runs a bounds check for row numbers
  49. * @param row
  50. */
  51. private static void validateRow(int row, SpreadsheetVersion ssVersion) {
  52. int maxrow = ssVersion.getLastRowIndex();
  53. if (row > maxrow) throw new IllegalArgumentException("Maximum row number is " + maxrow);
  54. if (row < 0) throw new IllegalArgumentException("Minumum row number is 0");
  55. }
  56. /**
  57. * Runs a bounds check for column numbers
  58. * @param column
  59. */
  60. private static void validateColumn(int column, SpreadsheetVersion ssVersion) {
  61. int maxcol = ssVersion.getLastColumnIndex();
  62. if (column > maxcol) throw new IllegalArgumentException("Maximum column number is " + maxcol);
  63. if (column < 0) throw new IllegalArgumentException("Minimum column number is 0");
  64. }
  65. //TODO use the correct SpreadsheetVersion
  66. public final boolean isFullColumnRange() {
  67. return (_firstRow == 0 && _lastRow == SpreadsheetVersion.EXCEL97.getLastRowIndex())
  68. || (_firstRow == -1 && _lastRow == -1);
  69. }
  70. //TODO use the correct SpreadsheetVersion
  71. public final boolean isFullRowRange() {
  72. return (_firstCol == 0 && _lastCol == SpreadsheetVersion.EXCEL97.getLastColumnIndex())
  73. || (_firstCol == -1 && _lastCol == -1);
  74. }
  75. /**
  76. * @return column number for the upper left hand corner
  77. */
  78. public final int getFirstColumn() {
  79. return _firstCol;
  80. }
  81. /**
  82. * @return row number for the upper left hand corner
  83. */
  84. public final int getFirstRow() {
  85. return _firstRow;
  86. }
  87. /**
  88. * @return column number for the lower right hand corner
  89. */
  90. public final int getLastColumn() {
  91. return _lastCol;
  92. }
  93. /**
  94. * @return row number for the lower right hand corner
  95. */
  96. public final int getLastRow() {
  97. return _lastRow;
  98. }
  99. public boolean isInRange(int rowInd, int colInd) {
  100. return _firstRow <= rowInd && rowInd <= _lastRow &&
  101. _firstCol <= colInd && colInd <= _lastCol;
  102. }
  103. /**
  104. * @param firstCol column number for the upper left hand corner
  105. */
  106. public final void setFirstColumn(int firstCol) {
  107. _firstCol = firstCol;
  108. }
  109. /**
  110. * @param firstRow row number for the upper left hand corner
  111. */
  112. public final void setFirstRow(int firstRow) {
  113. _firstRow = firstRow;
  114. }
  115. /**
  116. * @param lastCol column number for the lower right hand corner
  117. */
  118. public final void setLastColumn(int lastCol) {
  119. _lastCol = lastCol;
  120. }
  121. /**
  122. * @param lastRow row number for the lower right hand corner
  123. */
  124. public final void setLastRow(int lastRow) {
  125. _lastRow = lastRow;
  126. }
  127. /**
  128. * @return the size of the range (number of cells in the area).
  129. */
  130. public int getNumberOfCells() {
  131. return (_lastRow - _firstRow + 1) * (_lastCol - _firstCol + 1);
  132. }
  133. public final String toString() {
  134. CellReference crA = new CellReference(_firstRow, _firstCol);
  135. CellReference crB = new CellReference(_lastRow, _lastCol);
  136. return getClass().getName() + " [" + crA.formatAsString() + ":" + crB.formatAsString() +"]";
  137. }
  138. }