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ů.

SharedValueManager.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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.hssf.record.aggregates;
  16. import java.util.Arrays;
  17. import java.util.Comparator;
  18. import java.util.HashMap;
  19. import java.util.Map;
  20. import org.apache.poi.hssf.record.ArrayRecord;
  21. import org.apache.poi.hssf.record.FormulaRecord;
  22. import org.apache.poi.hssf.record.SharedFormulaRecord;
  23. import org.apache.poi.hssf.record.SharedValueRecordBase;
  24. import org.apache.poi.hssf.record.TableRecord;
  25. import org.apache.poi.hssf.record.formula.ExpPtg;
  26. import org.apache.poi.hssf.util.CellRangeAddress8Bit;
  27. import org.apache.poi.ss.util.CellReference;
  28. /**
  29. * Manages various auxiliary records while constructing a
  30. * {@link RowRecordsAggregate}:
  31. * <ul>
  32. * <li>{@link SharedFormulaRecord}s</li>
  33. * <li>{@link ArrayRecord}s</li>
  34. * <li>{@link TableRecord}s</li>
  35. * </ul>
  36. *
  37. * @author Josh Micich
  38. */
  39. public final class SharedValueManager {
  40. private static final class SharedFormulaGroup {
  41. private final SharedFormulaRecord _sfr;
  42. private final FormulaRecordAggregate[] _frAggs;
  43. private int _numberOfFormulas;
  44. /**
  45. * Coordinates of the first cell having a formula that uses this shared formula.
  46. * This is often <i>but not always</i> the top left cell in the range covered by
  47. * {@link #_sfr}
  48. */
  49. private final CellReference _firstCell;
  50. public SharedFormulaGroup(SharedFormulaRecord sfr, CellReference firstCell) {
  51. if (!sfr.isInRange(firstCell.getRow(), firstCell.getCol())) {
  52. throw new IllegalArgumentException("First formula cell " + firstCell.formatAsString()
  53. + " is not shared formula range " + sfr.getRange().toString() + ".");
  54. }
  55. _sfr = sfr;
  56. _firstCell = firstCell;
  57. int width = sfr.getLastColumn() - sfr.getFirstColumn() + 1;
  58. int height = sfr.getLastRow() - sfr.getFirstRow() + 1;
  59. _frAggs = new FormulaRecordAggregate[width * height];
  60. _numberOfFormulas = 0;
  61. }
  62. public void add(FormulaRecordAggregate agg) {
  63. if (_numberOfFormulas == 0) {
  64. if (_firstCell.getRow() != agg.getRow() || _firstCell.getCol() != agg.getColumn()) {
  65. throw new IllegalStateException("shared formula coding error");
  66. }
  67. }
  68. if (_numberOfFormulas >= _frAggs.length) {
  69. throw new RuntimeException("Too many formula records for shared formula group");
  70. }
  71. _frAggs[_numberOfFormulas++] = agg;
  72. }
  73. public void unlinkSharedFormulas() {
  74. for (int i = 0; i < _numberOfFormulas; i++) {
  75. _frAggs[i].unlinkSharedFormula();
  76. }
  77. }
  78. public SharedFormulaRecord getSFR() {
  79. return _sfr;
  80. }
  81. public final String toString() {
  82. StringBuffer sb = new StringBuffer(64);
  83. sb.append(getClass().getName()).append(" [");
  84. sb.append(_sfr.getRange().toString());
  85. sb.append("]");
  86. return sb.toString();
  87. }
  88. /**
  89. * Note - the 'first cell' of a shared formula group is not always the top-left cell
  90. * of the enclosing range.
  91. * @return <code>true</code> if the specified coordinates correspond to the 'first cell'
  92. * of this shared formula group.
  93. */
  94. public boolean isFirstCell(int row, int column) {
  95. return _firstCell.getRow() == row && _firstCell.getCol() == column;
  96. }
  97. }
  98. public static final SharedValueManager EMPTY = new SharedValueManager(
  99. new SharedFormulaRecord[0], new CellReference[0], new ArrayRecord[0], new TableRecord[0]);
  100. private final ArrayRecord[] _arrayRecords;
  101. private final TableRecord[] _tableRecords;
  102. private final Map<SharedFormulaRecord, SharedFormulaGroup> _groupsBySharedFormulaRecord;
  103. /** cached for optimization purposes */
  104. private SharedFormulaGroup[] _groups;
  105. private SharedValueManager(SharedFormulaRecord[] sharedFormulaRecords,
  106. CellReference[] firstCells, ArrayRecord[] arrayRecords, TableRecord[] tableRecords) {
  107. int nShF = sharedFormulaRecords.length;
  108. if (nShF != firstCells.length) {
  109. throw new IllegalArgumentException("array sizes don't match: " + nShF + "!=" + firstCells.length + ".");
  110. }
  111. _arrayRecords = arrayRecords;
  112. _tableRecords = tableRecords;
  113. Map<SharedFormulaRecord, SharedFormulaGroup> m = new HashMap<SharedFormulaRecord, SharedFormulaGroup>(nShF * 3 / 2);
  114. for (int i = 0; i < nShF; i++) {
  115. SharedFormulaRecord sfr = sharedFormulaRecords[i];
  116. m.put(sfr, new SharedFormulaGroup(sfr, firstCells[i]));
  117. }
  118. _groupsBySharedFormulaRecord = m;
  119. }
  120. /**
  121. * @param firstCells
  122. * @param recs list of sheet records (possibly contains records for other parts of the Excel file)
  123. * @param startIx index of first row/cell record for current sheet
  124. * @param endIx one past index of last row/cell record for current sheet. It is important
  125. * that this code does not inadvertently collect <tt>SharedFormulaRecord</tt>s from any other
  126. * sheet (which could happen if endIx is chosen poorly). (see bug 44449)
  127. */
  128. public static SharedValueManager create(SharedFormulaRecord[] sharedFormulaRecords,
  129. CellReference[] firstCells, ArrayRecord[] arrayRecords, TableRecord[] tableRecords) {
  130. if (sharedFormulaRecords.length + firstCells.length + arrayRecords.length + tableRecords.length < 1) {
  131. return EMPTY;
  132. }
  133. return new SharedValueManager(sharedFormulaRecords, firstCells, arrayRecords, tableRecords);
  134. }
  135. /**
  136. * @param firstCell as extracted from the {@link ExpPtg} from the cell's formula.
  137. * @return never <code>null</code>
  138. */
  139. public SharedFormulaRecord linkSharedFormulaRecord(CellReference firstCell, FormulaRecordAggregate agg) {
  140. SharedFormulaGroup result = findFormulaGroup(getGroups(), firstCell);
  141. result.add(agg);
  142. return result.getSFR();
  143. }
  144. private static SharedFormulaGroup findFormulaGroup(SharedFormulaGroup[] groups, CellReference firstCell) {
  145. int row = firstCell.getRow();
  146. int column = firstCell.getCol();
  147. // Traverse the list of shared formulas and try to find the correct one for us
  148. // perhaps this could be optimised to some kind of binary search
  149. for (int i = 0; i < groups.length; i++) {
  150. SharedFormulaGroup svg = groups[i];
  151. if (svg.isFirstCell(row, column)) {
  152. return svg;
  153. }
  154. }
  155. // TODO - fix file "15228.xls" so it opens in Excel after rewriting with POI
  156. throw new RuntimeException("Failed to find a matching shared formula record");
  157. }
  158. private SharedFormulaGroup[] getGroups() {
  159. if (_groups == null) {
  160. SharedFormulaGroup[] groups = new SharedFormulaGroup[_groupsBySharedFormulaRecord.size()];
  161. _groupsBySharedFormulaRecord.values().toArray(groups);
  162. Arrays.sort(groups, SVGComparator); // make search behaviour more deterministic
  163. _groups = groups;
  164. }
  165. return _groups;
  166. }
  167. private static final Comparator<SharedFormulaGroup> SVGComparator = new Comparator<SharedFormulaGroup>() {
  168. public int compare(SharedFormulaGroup a, SharedFormulaGroup b) {
  169. CellRangeAddress8Bit rangeA = a.getSFR().getRange();
  170. CellRangeAddress8Bit rangeB = b.getSFR().getRange();
  171. int cmp;
  172. cmp = rangeA.getFirstRow() - rangeB.getFirstRow();
  173. if (cmp != 0) {
  174. return cmp;
  175. }
  176. cmp = rangeA.getFirstColumn() - rangeB.getFirstColumn();
  177. if (cmp != 0) {
  178. return cmp;
  179. }
  180. return 0;
  181. }
  182. };
  183. /**
  184. * Gets the {@link SharedValueRecordBase} record if it should be encoded immediately after the
  185. * formula record contained in the specified {@link FormulaRecordAggregate} agg. Note - the
  186. * shared value record always appears after the first formula record in the group. For arrays
  187. * and tables the first formula is always the in the top left cell. However, since shared
  188. * formula groups can be sparse and/or overlap, the first formula may not actually be in the
  189. * top left cell.
  190. *
  191. * @return the SHRFMLA, TABLE or ARRAY record for the formula cell, if it is the first cell of
  192. * a table or array region. <code>null</code> if the formula cell is not shared/array/table,
  193. * or if the specified formula is not the the first in the group.
  194. */
  195. public SharedValueRecordBase getRecordForFirstCell(FormulaRecordAggregate agg) {
  196. CellReference firstCell = agg.getFormulaRecord().getFormula().getExpReference();
  197. // perhaps this could be optimised by consulting the (somewhat unreliable) isShared flag
  198. // and/or distinguishing between tExp and tTbl.
  199. if (firstCell == null) {
  200. // not a shared/array/table formula
  201. return null;
  202. }
  203. int row = firstCell.getRow();
  204. int column = firstCell.getCol();
  205. if (agg.getRow() != row || agg.getColumn() != column) {
  206. // not the first formula cell in the group
  207. return null;
  208. }
  209. SharedFormulaGroup[] groups = getGroups();
  210. for (int i = 0; i < groups.length; i++) {
  211. // note - logic for finding correct shared formula group is slightly
  212. // more complicated since the first cell
  213. SharedFormulaGroup sfg = groups[i];
  214. if (sfg.isFirstCell(row, column)) {
  215. return sfg.getSFR();
  216. }
  217. }
  218. // Since arrays and tables cannot be sparse (all cells in range participate)
  219. // The first cell will be the top left in the range. So we can match the
  220. // ARRAY/TABLE record directly.
  221. for (int i = 0; i < _tableRecords.length; i++) {
  222. TableRecord tr = _tableRecords[i];
  223. if (tr.isFirstCell(row, column)) {
  224. return tr;
  225. }
  226. }
  227. for (int i = 0; i < _arrayRecords.length; i++) {
  228. ArrayRecord ar = _arrayRecords[i];
  229. if (ar.isFirstCell(row, column)) {
  230. return ar;
  231. }
  232. }
  233. return null;
  234. }
  235. /**
  236. * Converts all {@link FormulaRecord}s handled by <tt>sharedFormulaRecord</tt>
  237. * to plain unshared formulas
  238. */
  239. public void unlink(SharedFormulaRecord sharedFormulaRecord) {
  240. SharedFormulaGroup svg = _groupsBySharedFormulaRecord.remove(sharedFormulaRecord);
  241. _groups = null; // be sure to reset cached value
  242. if (svg == null) {
  243. throw new IllegalStateException("Failed to find formulas for shared formula");
  244. }
  245. svg.unlinkSharedFormulas();
  246. }
  247. }