Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

AreaPtgBase.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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.formula.ptg;
  16. import java.util.Map;
  17. import java.util.function.Supplier;
  18. import org.apache.poi.ss.SpreadsheetVersion;
  19. import org.apache.poi.ss.util.AreaReference;
  20. import org.apache.poi.ss.util.CellReference;
  21. import org.apache.poi.util.BitField;
  22. import org.apache.poi.util.BitFieldFactory;
  23. import org.apache.poi.util.GenericRecordUtil;
  24. import org.apache.poi.util.LittleEndianInput;
  25. import org.apache.poi.util.LittleEndianOutput;
  26. /**
  27. * Specifies a rectangular area of cells A1:A4 for instance.
  28. */
  29. public abstract class AreaPtgBase extends OperandPtg implements AreaI {
  30. private static final BitField rowRelative = BitFieldFactory.getInstance(0x8000);
  31. private static final BitField colRelative = BitFieldFactory.getInstance(0x4000);
  32. private static final BitField columnMask = BitFieldFactory.getInstance(0x3FFF);
  33. /** zero based, unsigned 16 bit */
  34. private int field_1_first_row;
  35. /** zero based, unsigned 16 bit */
  36. private int field_2_last_row;
  37. /** zero based, unsigned 8 bit */
  38. private int field_3_first_column; //BitFields: (first row relative, first col relative, first column number)
  39. /** zero based, unsigned 8 bit */
  40. private int field_4_last_column; //BitFields: (last row relative, last col relative, last column number)
  41. protected AreaPtgBase() {}
  42. protected AreaPtgBase(AreaPtgBase other) {
  43. super(other);
  44. field_1_first_row = other.field_1_first_row;
  45. field_2_last_row = other.field_2_last_row;
  46. field_3_first_column = other.field_3_first_column;
  47. field_4_last_column = other.field_4_last_column;
  48. }
  49. protected AreaPtgBase(AreaReference ar) {
  50. CellReference firstCell = ar.getFirstCell();
  51. CellReference lastCell = ar.getLastCell();
  52. setFirstRow(firstCell.getRow());
  53. setFirstColumn(firstCell.getCol() == -1 ? 0 : firstCell.getCol());
  54. setLastRow(lastCell.getRow());
  55. setLastColumn(lastCell.getCol() == -1 ? 0xFF : lastCell.getCol());
  56. setFirstColRelative(!firstCell.isColAbsolute());
  57. setLastColRelative(!lastCell.isColAbsolute());
  58. setFirstRowRelative(!firstCell.isRowAbsolute());
  59. setLastRowRelative(!lastCell.isRowAbsolute());
  60. }
  61. protected AreaPtgBase(int firstRow, int lastRow, int firstColumn, int lastColumn,
  62. boolean firstRowRelative, boolean lastRowRelative, boolean firstColRelative, boolean lastColRelative) {
  63. if (lastRow >= firstRow) {
  64. setFirstRow(firstRow);
  65. setLastRow(lastRow);
  66. setFirstRowRelative(firstRowRelative);
  67. setLastRowRelative(lastRowRelative);
  68. } else {
  69. setFirstRow(lastRow);
  70. setLastRow(firstRow);
  71. setFirstRowRelative(lastRowRelative);
  72. setLastRowRelative(firstRowRelative);
  73. }
  74. if (lastColumn >= firstColumn) {
  75. setFirstColumn(firstColumn);
  76. setLastColumn(lastColumn);
  77. setFirstColRelative(firstColRelative);
  78. setLastColRelative(lastColRelative);
  79. } else {
  80. setFirstColumn(lastColumn);
  81. setLastColumn(firstColumn);
  82. setFirstColRelative(lastColRelative);
  83. setLastColRelative(firstColRelative);
  84. }
  85. }
  86. /**
  87. * Sort the first and last row and columns in-place to the preferred (top left:bottom right) order
  88. * Note: Sort only occurs when an instance is constructed or when this method is called.
  89. *
  90. * <p>For example, <code>$E5:B$10</code> becomes <code>B5:$E$10</code></p>
  91. */
  92. public void sortTopLeftToBottomRight() {
  93. if (getFirstRow() > getLastRow()) {
  94. //swap first row and last row numbers and relativity
  95. //Note: cannot just swap the fields because row relativity is stored in fields 3 and 4
  96. final int firstRow = getFirstRow();
  97. final boolean firstRowRel = isFirstRowRelative();
  98. setFirstRow(getLastRow());
  99. setFirstRowRelative(isLastRowRelative());
  100. setLastRow(firstRow);
  101. setLastRowRelative(firstRowRel);
  102. }
  103. if (getFirstColumn() > getLastColumn()) {
  104. //swap first column and last column numbers and relativity
  105. //Note: cannot just swap the fields because row relativity is stored in fields 3 and 4
  106. final int firstCol = getFirstColumn();
  107. final boolean firstColRel = isFirstColRelative();
  108. setFirstColumn(getLastColumn());
  109. setFirstColRelative(isLastColRelative());
  110. setLastColumn(firstCol);
  111. setLastColRelative(firstColRel);
  112. }
  113. }
  114. protected final void readCoordinates(LittleEndianInput in) {
  115. field_1_first_row = in.readUShort();
  116. field_2_last_row = in.readUShort();
  117. field_3_first_column = in.readUShort();
  118. field_4_last_column = in.readUShort();
  119. }
  120. protected final void writeCoordinates(LittleEndianOutput out) {
  121. out.writeShort(field_1_first_row);
  122. out.writeShort(field_2_last_row);
  123. out.writeShort(field_3_first_column);
  124. out.writeShort(field_4_last_column);
  125. }
  126. /**
  127. * @return the first row in the area
  128. */
  129. public final int getFirstRow() {
  130. return field_1_first_row;
  131. }
  132. /**
  133. * sets the first row
  134. * @param rowIx number (0-based)
  135. */
  136. public final void setFirstRow(int rowIx) {
  137. field_1_first_row = rowIx;
  138. }
  139. /**
  140. * @return last row in the range (x2 in x1,y1-x2,y2)
  141. */
  142. public final int getLastRow() {
  143. return field_2_last_row;
  144. }
  145. /**
  146. * @param rowIx last row number in the area
  147. */
  148. public final void setLastRow(int rowIx) {
  149. field_2_last_row = rowIx;
  150. }
  151. /**
  152. * @return the first column number in the area.
  153. */
  154. public final int getFirstColumn() {
  155. return columnMask.getValue(field_3_first_column);
  156. }
  157. /**
  158. * @return the first column number + the options bit settings unstripped
  159. */
  160. public final short getFirstColumnRaw() {
  161. return (short) field_3_first_column; // TODO
  162. }
  163. /**
  164. * @return whether or not the first row is a relative reference or not.
  165. */
  166. public final boolean isFirstRowRelative() {
  167. return rowRelative.isSet(field_3_first_column);
  168. }
  169. /**
  170. * sets the first row to relative or not
  171. * @param rel is relative or not.
  172. */
  173. public final void setFirstRowRelative(boolean rel) {
  174. field_3_first_column=rowRelative.setBoolean(field_3_first_column,rel);
  175. }
  176. /**
  177. * @return isrelative first column to relative or not
  178. */
  179. public final boolean isFirstColRelative() {
  180. return colRelative.isSet(field_3_first_column);
  181. }
  182. /**
  183. * set whether the first column is relative
  184. */
  185. public final void setFirstColRelative(boolean rel) {
  186. field_3_first_column=colRelative.setBoolean(field_3_first_column,rel);
  187. }
  188. /**
  189. * set the first column in the area
  190. */
  191. public final void setFirstColumn(int colIx) {
  192. field_3_first_column=columnMask.setValue(field_3_first_column, colIx);
  193. }
  194. /**
  195. * set the first column irrespective of the bitmasks
  196. */
  197. public final void setFirstColumnRaw(int column) {
  198. field_3_first_column = column;
  199. }
  200. /**
  201. * @return lastcolumn in the area
  202. */
  203. public final int getLastColumn() {
  204. return columnMask.getValue(field_4_last_column);
  205. }
  206. /**
  207. * @return last column and bitmask (the raw field)
  208. */
  209. public final short getLastColumnRaw() {
  210. return (short) field_4_last_column;
  211. }
  212. /**
  213. * @return last row relative or not
  214. */
  215. public final boolean isLastRowRelative() {
  216. return rowRelative.isSet(field_4_last_column);
  217. }
  218. /**
  219. * set whether the last row is relative or not
  220. * @param rel <code>true</code> if the last row relative, else
  221. * <code>false</code>
  222. */
  223. public final void setLastRowRelative(boolean rel) {
  224. field_4_last_column=rowRelative.setBoolean(field_4_last_column,rel);
  225. }
  226. /**
  227. * @return lastcol relative or not
  228. */
  229. public final boolean isLastColRelative() {
  230. return colRelative.isSet(field_4_last_column);
  231. }
  232. /**
  233. * set whether the last column should be relative or not
  234. */
  235. public final void setLastColRelative(boolean rel) {
  236. field_4_last_column=colRelative.setBoolean(field_4_last_column,rel);
  237. }
  238. /**
  239. * set the last column in the area
  240. */
  241. public final void setLastColumn(int colIx) {
  242. field_4_last_column=columnMask.setValue(field_4_last_column, colIx);
  243. }
  244. /**
  245. * set the last column irrespective of the bitmasks
  246. */
  247. public final void setLastColumnRaw(short column) {
  248. field_4_last_column = column;
  249. }
  250. protected final String formatReferenceAsString() {
  251. CellReference topLeft = new CellReference(getFirstRow(),getFirstColumn(),!isFirstRowRelative(),!isFirstColRelative());
  252. CellReference botRight = new CellReference(getLastRow(),getLastColumn(),!isLastRowRelative(),!isLastColRelative());
  253. if(AreaReference.isWholeColumnReference(SpreadsheetVersion.EXCEL97, topLeft, botRight)) {
  254. return (new AreaReference(topLeft, botRight, SpreadsheetVersion.EXCEL97)).formatAsString();
  255. }
  256. return topLeft.formatAsString() + ":" + botRight.formatAsString();
  257. }
  258. public String toFormulaString() {
  259. return formatReferenceAsString();
  260. }
  261. public byte getDefaultOperandClass() {
  262. return Ptg.CLASS_REF;
  263. }
  264. @Override
  265. public Map<String, Supplier<?>> getGenericProperties() {
  266. return GenericRecordUtil.getGenericProperties(
  267. "firstRow", this::getFirstRow,
  268. "firstRowRelative", this::isFirstRowRelative,
  269. "firstColumn", this::getFirstColumn,
  270. "firstColRelative", this::isFirstColRelative,
  271. "lastRow", this::getLastRow,
  272. "lastRowRelative", this::isLastRowRelative,
  273. "lastColumn", this::getLastColumn,
  274. "lastColRelative", this::isLastColRelative,
  275. "formatReference", this::formatReferenceAsString
  276. );
  277. }
  278. }