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.

CellRangeAddress.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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.hssf.record.RecordInputStream;
  17. import org.apache.poi.hssf.record.SelectionRecord;
  18. import org.apache.poi.ss.formula.SheetNameFormatter;
  19. import org.apache.poi.util.LittleEndianOutput;
  20. /**
  21. * See OOO documentation: excelfileformat.pdf sec 2.5.14 - 'Cell Range Address'<p>
  22. *
  23. * <p>In the Microsoft documentation, this is also known as a
  24. * Ref8U - see page 831 of version 1.0 of the documentation.
  25. *
  26. * Note - {@link SelectionRecord} uses the BIFF5 version of this structure
  27. * @author Dragos Buleandra (dragos.buleandra@trade2b.ro)
  28. */
  29. public class CellRangeAddress extends CellRangeAddressBase {
  30. public static final int ENCODED_SIZE = 8;
  31. /**
  32. * Creates new cell range. Indexes are zero-based.
  33. *
  34. * @param firstRow Index of first row
  35. * @param lastRow Index of last row (inclusive), must be equal to or larger than {@code firstRow}
  36. * @param firstCol Index of first column
  37. * @param lastCol Index of last column (inclusive), must be equal to or larger than {@code firstCol}
  38. */
  39. public CellRangeAddress(int firstRow, int lastRow, int firstCol, int lastCol) {
  40. super(firstRow, lastRow, firstCol, lastCol);
  41. if (lastRow < firstRow || lastCol < firstCol) {
  42. throw new IllegalArgumentException("Invalid cell range, having lastRow < firstRow || lastCol < firstCol, " +
  43. "had rows " + lastRow + " >= " + firstRow + " or cells " + lastCol + " >= " + firstCol);
  44. }
  45. }
  46. public void serialize(LittleEndianOutput out) {
  47. out.writeShort(getFirstRow());
  48. out.writeShort(getLastRow());
  49. out.writeShort(getFirstColumn());
  50. out.writeShort(getLastColumn());
  51. }
  52. public CellRangeAddress(RecordInputStream in) {
  53. super(readUShortAndCheck(in), in.readUShort(), in.readUShort(), in.readUShort());
  54. }
  55. private static int readUShortAndCheck(RecordInputStream in) {
  56. if (in.remaining() < ENCODED_SIZE) {
  57. // Ran out of data
  58. throw new RuntimeException("Ran out of data reading CellRangeAddress");
  59. }
  60. return in.readUShort();
  61. }
  62. @Override
  63. public CellRangeAddress copy() {
  64. return new CellRangeAddress(getFirstRow(), getLastRow(), getFirstColumn(), getLastColumn());
  65. }
  66. public static int getEncodedSize(int numberOfItems) {
  67. return numberOfItems * ENCODED_SIZE;
  68. }
  69. /**
  70. * @return the text format of this range. Single cell ranges are formatted
  71. * like single cell references (e.g. 'A1' instead of 'A1:A1').
  72. */
  73. public String formatAsString() {
  74. return formatAsString(null, false);
  75. }
  76. /**
  77. * @return the text format of this range using specified sheet name.
  78. */
  79. public String formatAsString(String sheetName, boolean useAbsoluteAddress) {
  80. StringBuilder sb = new StringBuilder();
  81. if (sheetName != null) {
  82. sb.append(SheetNameFormatter.format(sheetName));
  83. sb.append("!");
  84. }
  85. CellReference cellRefFrom = new CellReference(getFirstRow(), getFirstColumn(),
  86. useAbsoluteAddress, useAbsoluteAddress);
  87. CellReference cellRefTo = new CellReference(getLastRow(), getLastColumn(),
  88. useAbsoluteAddress, useAbsoluteAddress);
  89. sb.append(cellRefFrom.formatAsString());
  90. //for a single-cell reference return A1 instead of A1:A1
  91. //for full-column ranges or full-row ranges return A:A instead of A,
  92. //and 1:1 instead of 1
  93. if(!cellRefFrom.equals(cellRefTo)
  94. || isFullColumnRange() || isFullRowRange()){
  95. sb.append(':');
  96. sb.append(cellRefTo.formatAsString());
  97. }
  98. return sb.toString();
  99. }
  100. /**
  101. * Creates a CellRangeAddress from a cell range reference string.
  102. *
  103. * @param ref usually a standard area ref (e.g. "B1:D8"). May be a single
  104. * cell ref (e.g. "B5") in which case the result is a 1 x 1 cell
  105. * range. May also be a whole row range (e.g. "3:5"), or a whole
  106. * column range (e.g. "C:F")
  107. */
  108. public static CellRangeAddress valueOf(String ref) {
  109. int sep = ref.indexOf(':');
  110. CellReference a;
  111. CellReference b;
  112. if (sep == -1) {
  113. a = new CellReference(ref);
  114. b = a;
  115. } else {
  116. a = new CellReference(ref.substring(0, sep));
  117. b = new CellReference(ref.substring(sep + 1));
  118. }
  119. return new CellRangeAddress(a.getRow(), b.getRow(), a.getCol(), b.getCol());
  120. }
  121. }