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.

HSSFFormulaEvaluator.java 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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.usermodel;
  16. import java.util.Map;
  17. import org.apache.poi.ss.formula.BaseFormulaEvaluator;
  18. import org.apache.poi.ss.formula.CollaboratingWorkbooksEnvironment;
  19. import org.apache.poi.ss.formula.IStabilityClassifier;
  20. import org.apache.poi.ss.formula.WorkbookEvaluator;
  21. import org.apache.poi.ss.formula.eval.BoolEval;
  22. import org.apache.poi.ss.formula.eval.ErrorEval;
  23. import org.apache.poi.ss.formula.eval.NumericValueEval;
  24. import org.apache.poi.ss.formula.eval.StringValueEval;
  25. import org.apache.poi.ss.formula.eval.ValueEval;
  26. import org.apache.poi.ss.formula.udf.UDFFinder;
  27. import org.apache.poi.ss.usermodel.Cell;
  28. import org.apache.poi.ss.usermodel.CellValue;
  29. import org.apache.poi.ss.usermodel.FormulaEvaluator;
  30. import org.apache.poi.ss.usermodel.RichTextString;
  31. import org.apache.poi.ss.usermodel.Workbook;
  32. /**
  33. * Evaluates formula cells.<p/>
  34. *
  35. * For performance reasons, this class keeps a cache of all previously calculated intermediate
  36. * cell values. Be sure to call {@link #clearAllCachedResultValues()} if any workbook cells are changed between
  37. * calls to evaluate~ methods on this class.
  38. */
  39. public class HSSFFormulaEvaluator extends BaseFormulaEvaluator {
  40. private final HSSFWorkbook _book;
  41. public HSSFFormulaEvaluator(HSSFWorkbook workbook) {
  42. this(workbook, null);
  43. }
  44. /**
  45. * @param workbook The workbook to perform the formula evaluations in
  46. * @param stabilityClassifier used to optimise caching performance. Pass <code>null</code>
  47. * for the (conservative) assumption that any cell may have its definition changed after
  48. * evaluation begins.
  49. */
  50. public HSSFFormulaEvaluator(HSSFWorkbook workbook, IStabilityClassifier stabilityClassifier) {
  51. this(workbook, stabilityClassifier, null);
  52. }
  53. /**
  54. * @param workbook The workbook to perform the formula evaluations in
  55. * @param stabilityClassifier used to optimise caching performance. Pass <code>null</code>
  56. * for the (conservative) assumption that any cell may have its definition changed after
  57. * evaluation begins.
  58. * @param udfFinder pass <code>null</code> for default (AnalysisToolPak only)
  59. */
  60. private HSSFFormulaEvaluator(HSSFWorkbook workbook, IStabilityClassifier stabilityClassifier, UDFFinder udfFinder) {
  61. super(new WorkbookEvaluator(HSSFEvaluationWorkbook.create(workbook), stabilityClassifier, udfFinder));
  62. _book = workbook;
  63. }
  64. /**
  65. * @param workbook The workbook to perform the formula evaluations in
  66. * @param stabilityClassifier used to optimise caching performance. Pass <code>null</code>
  67. * for the (conservative) assumption that any cell may have its definition changed after
  68. * evaluation begins.
  69. * @param udfFinder pass <code>null</code> for default (AnalysisToolPak only)
  70. */
  71. public static HSSFFormulaEvaluator create(HSSFWorkbook workbook, IStabilityClassifier stabilityClassifier, UDFFinder udfFinder) {
  72. return new HSSFFormulaEvaluator(workbook, stabilityClassifier, udfFinder);
  73. }
  74. @Override
  75. protected RichTextString createRichTextString(String str) {
  76. return new HSSFRichTextString(str);
  77. }
  78. /**
  79. * Coordinates several formula evaluators together so that formulas that involve external
  80. * references can be evaluated.
  81. * @param workbookNames the simple file names used to identify the workbooks in formulas
  82. * with external links (for example "MyData.xls" as used in a formula "[MyData.xls]Sheet1!A1")
  83. * @param evaluators all evaluators for the full set of workbooks required by the formulas.
  84. */
  85. public static void setupEnvironment(String[] workbookNames, HSSFFormulaEvaluator[] evaluators) {
  86. BaseFormulaEvaluator.setupEnvironment(workbookNames, evaluators);
  87. }
  88. @Override
  89. public void setupReferencedWorkbooks(Map<String, FormulaEvaluator> evaluators) {
  90. CollaboratingWorkbooksEnvironment.setupFormulaEvaluator(evaluators);
  91. }
  92. /**
  93. * Should be called to tell the cell value cache that the specified (value or formula) cell
  94. * has changed.
  95. * Failure to call this method after changing cell values will cause incorrect behaviour
  96. * of the evaluate~ methods of this class
  97. */
  98. public void notifyUpdateCell(HSSFCell cell) {
  99. _bookEvaluator.notifyUpdateCell(new HSSFEvaluationCell(cell));
  100. }
  101. @Override
  102. public void notifyUpdateCell(Cell cell) {
  103. _bookEvaluator.notifyUpdateCell(new HSSFEvaluationCell((HSSFCell)cell));
  104. }
  105. /**
  106. * Should be called to tell the cell value cache that the specified cell has just been
  107. * deleted.
  108. * Failure to call this method after changing cell values will cause incorrect behaviour
  109. * of the evaluate~ methods of this class
  110. */
  111. public void notifyDeleteCell(HSSFCell cell) {
  112. _bookEvaluator.notifyDeleteCell(new HSSFEvaluationCell(cell));
  113. }
  114. @Override
  115. public void notifyDeleteCell(Cell cell) {
  116. _bookEvaluator.notifyDeleteCell(new HSSFEvaluationCell((HSSFCell)cell));
  117. }
  118. /**
  119. * Should be called to tell the cell value cache that the specified (value or formula) cell
  120. * has changed.
  121. * Failure to call this method after changing cell values will cause incorrect behaviour
  122. * of the evaluate~ methods of this class
  123. */
  124. @Override
  125. public void notifySetFormula(Cell cell) {
  126. _bookEvaluator.notifyUpdateCell(new HSSFEvaluationCell((HSSFCell)cell));
  127. }
  128. @Override
  129. public HSSFCell evaluateInCell(Cell cell) {
  130. return (HSSFCell) super.evaluateInCell(cell);
  131. }
  132. /**
  133. * Loops over all cells in all sheets of the supplied
  134. * workbook.
  135. * For cells that contain formulas, their formulas are
  136. * evaluated, and the results are saved. These cells
  137. * remain as formula cells.
  138. * For cells that do not contain formulas, no changes
  139. * are made.
  140. * This is a helpful wrapper around looping over all
  141. * cells, and calling evaluateFormulaCell on each one.
  142. */
  143. public static void evaluateAllFormulaCells(HSSFWorkbook wb) {
  144. evaluateAllFormulaCells(wb, new HSSFFormulaEvaluator(wb));
  145. }
  146. /**
  147. * Loops over all cells in all sheets of the supplied
  148. * workbook.
  149. * For cells that contain formulas, their formulas are
  150. * evaluated, and the results are saved. These cells
  151. * remain as formula cells.
  152. * For cells that do not contain formulas, no changes
  153. * are made.
  154. * This is a helpful wrapper around looping over all
  155. * cells, and calling evaluateFormulaCell on each one.
  156. */
  157. public static void evaluateAllFormulaCells(Workbook wb) {
  158. BaseFormulaEvaluator.evaluateAllFormulaCells(wb);
  159. }
  160. /**
  161. * Loops over all cells in all sheets of the supplied
  162. * workbook.
  163. * For cells that contain formulas, their formulas are
  164. * evaluated, and the results are saved. These cells
  165. * remain as formula cells.
  166. * For cells that do not contain formulas, no changes
  167. * are made.
  168. * This is a helpful wrapper around looping over all
  169. * cells, and calling evaluateFormulaCell on each one.
  170. */
  171. @Override
  172. public void evaluateAll() {
  173. evaluateAllFormulaCells(_book, this);
  174. }
  175. /**
  176. * Returns a CellValue wrapper around the supplied ValueEval instance.
  177. * @param cell
  178. */
  179. protected CellValue evaluateFormulaCellValue(Cell cell) {
  180. ValueEval eval = _bookEvaluator.evaluate(new HSSFEvaluationCell((HSSFCell)cell));
  181. if (eval instanceof BoolEval) {
  182. BoolEval be = (BoolEval) eval;
  183. return CellValue.valueOf(be.getBooleanValue());
  184. }
  185. if (eval instanceof NumericValueEval) {
  186. NumericValueEval ne = (NumericValueEval) eval;
  187. return new CellValue(ne.getNumberValue());
  188. }
  189. if (eval instanceof StringValueEval) {
  190. StringValueEval ne = (StringValueEval) eval;
  191. return new CellValue(ne.getStringValue());
  192. }
  193. if (eval instanceof ErrorEval) {
  194. return CellValue.getError(((ErrorEval)eval).getErrorCode());
  195. }
  196. throw new RuntimeException("Unexpected eval class (" + eval.getClass().getName() + ")");
  197. }
  198. /** {@inheritDoc} */
  199. @Override
  200. public void setIgnoreMissingWorkbooks(boolean ignore){
  201. _bookEvaluator.setIgnoreMissingWorkbooks(ignore);
  202. }
  203. /** {@inheritDoc} */
  204. @Override
  205. public void setDebugEvaluationOutputForNextEval(boolean value){
  206. _bookEvaluator.setDebugEvaluationOutputForNextEval(value);
  207. }
  208. }