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.

FormulaRecordAggregate.java 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 org.apache.poi.hssf.record.CellValueRecordInterface;
  17. import org.apache.poi.hssf.record.FormulaRecord;
  18. import org.apache.poi.hssf.record.Record;
  19. import org.apache.poi.hssf.record.RecordFormatException;
  20. import org.apache.poi.hssf.record.SharedFormulaRecord;
  21. import org.apache.poi.hssf.record.StringRecord;
  22. import org.apache.poi.hssf.record.formula.ExpPtg;
  23. import org.apache.poi.hssf.record.formula.Ptg;
  24. import org.apache.poi.hssf.util.CellReference;
  25. /**
  26. * The formula record aggregate is used to join together the formula record and it's
  27. * (optional) string record and (optional) Shared Formula Record (template reads, excel optimization).
  28. *
  29. * @author Glen Stampoultzis (glens at apache.org)
  30. */
  31. public final class FormulaRecordAggregate extends RecordAggregate implements CellValueRecordInterface {
  32. private final FormulaRecord _formulaRecord;
  33. private SharedValueManager _sharedValueManager;
  34. /** caches the calculated result of the formula */
  35. private StringRecord _stringRecord;
  36. private SharedFormulaRecord _sharedFormulaRecord;
  37. /**
  38. * @param stringRec may be <code>null</code> if this formula does not have a cached text
  39. * value.
  40. * @param svm the {@link SharedValueManager} for the current sheet
  41. */
  42. public FormulaRecordAggregate(FormulaRecord formulaRec, StringRecord stringRec, SharedValueManager svm) {
  43. if (svm == null) {
  44. throw new IllegalArgumentException("sfm must not be null");
  45. }
  46. if (formulaRec.hasCachedResultString()) {
  47. if (stringRec == null) {
  48. throw new RecordFormatException("Formula record flag is set but String record was not found");
  49. }
  50. _stringRecord = stringRec;
  51. } else {
  52. // Usually stringRec is null here (in agreement with what the formula rec says).
  53. // In the case where an extra StringRecord is erroneously present, Excel (2007)
  54. // ignores it (see bug 46213).
  55. _stringRecord = null;
  56. }
  57. _formulaRecord = formulaRec;
  58. _sharedValueManager = svm;
  59. if (formulaRec.isSharedFormula()) {
  60. CellReference firstCell = formulaRec.getFormula().getExpReference();
  61. if (firstCell == null) {
  62. handleMissingSharedFormulaRecord(formulaRec);
  63. } else {
  64. _sharedFormulaRecord = svm.linkSharedFormulaRecord(firstCell, this);
  65. }
  66. }
  67. }
  68. /**
  69. * Sometimes the shared formula flag "seems" to be erroneously set (because the corresponding
  70. * {@link SharedFormulaRecord} does not exist). Normally this would leave no way of determining
  71. * the {@link Ptg} tokens for the formula. However as it turns out in these
  72. * cases, Excel encodes the unshared {@link Ptg} tokens in the right place (inside the {@link
  73. * FormulaRecord}). So the the only thing that needs to be done is to ignore the erroneous
  74. * shared formula flag.<br/>
  75. *
  76. * This method may also be used for setting breakpoints to help diagnose issues regarding the
  77. * abnormally-set 'shared formula' flags.
  78. * (see TestValueRecordsAggregate.testSpuriousSharedFormulaFlag()).<p/>
  79. */
  80. private static void handleMissingSharedFormulaRecord(FormulaRecord formula) {
  81. // make sure 'unshared' formula is actually available
  82. Ptg firstToken = formula.getParsedExpression()[0];
  83. if (firstToken instanceof ExpPtg) {
  84. throw new RecordFormatException(
  85. "SharedFormulaRecord not found for FormulaRecord with (isSharedFormula=true)");
  86. }
  87. // could log an info message here since this is a fairly unusual occurrence.
  88. formula.setSharedFormula(false); // no point leaving the flag erroneously set
  89. }
  90. public FormulaRecord getFormulaRecord() {
  91. return _formulaRecord;
  92. }
  93. /**
  94. * debug only
  95. * TODO - encapsulate
  96. */
  97. public StringRecord getStringRecord() {
  98. return _stringRecord;
  99. }
  100. public short getXFIndex() {
  101. return _formulaRecord.getXFIndex();
  102. }
  103. public void setXFIndex(short xf) {
  104. _formulaRecord.setXFIndex(xf);
  105. }
  106. public void setColumn(short col) {
  107. _formulaRecord.setColumn(col);
  108. }
  109. public void setRow(int row) {
  110. _formulaRecord.setRow(row);
  111. }
  112. public short getColumn() {
  113. return _formulaRecord.getColumn();
  114. }
  115. public int getRow() {
  116. return _formulaRecord.getRow();
  117. }
  118. public String toString() {
  119. return _formulaRecord.toString();
  120. }
  121. public void visitContainedRecords(RecordVisitor rv) {
  122. rv.visitRecord(_formulaRecord);
  123. Record sharedFormulaRecord = _sharedValueManager.getRecordForFirstCell(this);
  124. if (sharedFormulaRecord != null) {
  125. rv.visitRecord(sharedFormulaRecord);
  126. }
  127. if (_formulaRecord.hasCachedResultString() && _stringRecord != null) {
  128. rv.visitRecord(_stringRecord);
  129. }
  130. }
  131. public String getStringValue() {
  132. if(_stringRecord==null) {
  133. return null;
  134. }
  135. return _stringRecord.getString();
  136. }
  137. public void setCachedStringResult(String value) {
  138. // Save the string into a String Record, creating one if required
  139. if(_stringRecord == null) {
  140. _stringRecord = new StringRecord();
  141. }
  142. _stringRecord.setString(value);
  143. if (value.length() < 1) {
  144. _formulaRecord.setCachedResultTypeEmptyString();
  145. } else {
  146. _formulaRecord.setCachedResultTypeString();
  147. }
  148. }
  149. public void setCachedBooleanResult(boolean value) {
  150. _stringRecord = null;
  151. _formulaRecord.setCachedResultBoolean(value);
  152. }
  153. public void setCachedErrorResult(int errorCode) {
  154. _stringRecord = null;
  155. _formulaRecord.setCachedResultErrorCode(errorCode);
  156. }
  157. public void setCachedDoubleResult(double value) {
  158. _stringRecord = null;
  159. _formulaRecord.setValue(value);
  160. }
  161. public Ptg[] getFormulaTokens() {
  162. if (_sharedFormulaRecord == null) {
  163. return _formulaRecord.getParsedExpression();
  164. }
  165. return _sharedFormulaRecord.getFormulaTokens(_formulaRecord);
  166. }
  167. /**
  168. * Also checks for a related shared formula and unlinks it if found
  169. */
  170. public void setParsedExpression(Ptg[] ptgs) {
  171. notifyFormulaChanging();
  172. _formulaRecord.setParsedExpression(ptgs);
  173. }
  174. public void unlinkSharedFormula() {
  175. SharedFormulaRecord sfr = _sharedFormulaRecord;
  176. if (sfr == null) {
  177. throw new IllegalStateException("Formula not linked to shared formula");
  178. }
  179. Ptg[] ptgs = sfr.getFormulaTokens(_formulaRecord);
  180. _formulaRecord.setParsedExpression(ptgs);
  181. //Now its not shared!
  182. _formulaRecord.setSharedFormula(false);
  183. _sharedFormulaRecord = null;
  184. }
  185. /**
  186. * Should be called by any code which is either deleting this formula cell, or changing
  187. * its type. This method gives the aggregate a chance to unlink any shared formula
  188. * that may be involved with this cell formula.
  189. */
  190. public void notifyFormulaChanging() {
  191. if (_sharedFormulaRecord != null) {
  192. _sharedValueManager.unlink(_sharedFormulaRecord);
  193. }
  194. }
  195. }