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.

FormulaEvaluator.java 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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.usermodel;
  16. import org.apache.poi.util.Removal;
  17. import java.util.Map;
  18. /**
  19. * Evaluates formula cells.<p>
  20. *
  21. * For performance reasons, this class keeps a cache of all previously calculated intermediate
  22. * cell values. Be sure to call {@link #clearAllCachedResultValues()} if any workbook cells are changed between
  23. * calls to evaluate~ methods on this class.
  24. *
  25. * @author Amol S. Deshmukh &lt; amolweb at ya hoo dot com &gt;
  26. * @author Josh Micich
  27. */
  28. public interface FormulaEvaluator {
  29. /**
  30. * Should be called whenever there are changes to input cells in the evaluated workbook.
  31. * Failure to call this method after changing cell values will cause incorrect behaviour
  32. * of the evaluate~ methods of this class
  33. */
  34. void clearAllCachedResultValues();
  35. /**
  36. * Should be called to tell the cell value cache that the specified (value or formula) cell
  37. * has changed.
  38. * Failure to call this method after changing cell values will cause incorrect behaviour
  39. * of the evaluate~ methods of this class
  40. */
  41. void notifySetFormula(Cell cell);
  42. /**
  43. * Should be called to tell the cell value cache that the specified cell has just become a
  44. * formula cell, or the formula text has changed
  45. */
  46. void notifyDeleteCell(Cell cell);
  47. /**
  48. * Should be called to tell the cell value cache that the specified (value or formula) cell
  49. * has changed.
  50. * Failure to call this method after changing cell values will cause incorrect behaviour
  51. * of the evaluate~ methods of this class
  52. */
  53. void notifyUpdateCell(Cell cell);
  54. /**
  55. * Loops over all cells in all sheets of the associated workbook.
  56. * For cells that contain formulas, their formulas are evaluated,
  57. * and the results are saved. These cells remain as formula cells.
  58. * For cells that do not contain formulas, no changes are made.
  59. * This is a helpful wrapper around looping over all cells, and
  60. * calling evaluateFormulaCell on each one.
  61. */
  62. void evaluateAll();
  63. /**
  64. * If cell contains a formula, the formula is evaluated and returned,
  65. * else the CellValue simply copies the appropriate cell value from
  66. * the cell and also its cell type. This method should be preferred over
  67. * evaluateInCell() when the call should not modify the contents of the
  68. * original cell.
  69. * @param cell The {@link Cell} to evaluate
  70. */
  71. CellValue evaluate(Cell cell);
  72. /**
  73. * If cell contains formula, it evaluates the formula,
  74. * and saves the result of the formula. The cell
  75. * remains as a formula cell.
  76. * Else if cell does not contain formula, this method leaves
  77. * the cell unchanged.
  78. * Note that the type of the formula result is returned,
  79. * so you know what kind of value is also stored with
  80. * the formula.
  81. * <pre>
  82. * int evaluatedCellType = evaluator.evaluateFormulaCell(cell);
  83. * </pre>
  84. * Be aware that your cell will hold both the formula,
  85. * and the result. If you want the cell replaced with
  86. * the result of the formula, use {@link #evaluateInCell(Cell)}
  87. * @param cell The cell to evaluate
  88. * @return The type of the formula result, i.e. -1 if the cell is not a formula,
  89. * or one of {@link CellType#NUMERIC}, {@link CellType#STRING},
  90. * {@link CellType#BOOLEAN}, {@link CellType#ERROR}
  91. * Note: the cell's type remains as CellType.FORMULA however.
  92. */
  93. CellType evaluateFormulaCell(Cell cell);
  94. /**
  95. * If cell contains formula, it evaluates the formula, and
  96. * puts the formula result back into the cell, in place
  97. * of the old formula.
  98. * Else if cell does not contain formula, this method leaves
  99. * the cell unchanged.
  100. * Note that the same instance of Cell is returned to
  101. * allow chained calls like:
  102. * <pre>
  103. * int evaluatedCellType = evaluator.evaluateInCell(cell).getCellType();
  104. * </pre>
  105. * Be aware that your cell value will be changed to hold the
  106. * result of the formula. If you simply want the formula
  107. * value computed for you, use {@link #evaluateFormulaCell(Cell)}
  108. * @param cell The {@link Cell} to evaluate and modify.
  109. */
  110. Cell evaluateInCell(Cell cell);
  111. /**
  112. * Sets up the Formula Evaluator to be able to reference and resolve
  113. * links to other workbooks, eg [Test.xls]Sheet1!A1.
  114. * <p>For a workbook referenced as [Test.xls]Sheet1!A1, you should
  115. * supply a map containing the key Test.xls (no square brackets),
  116. * and an open FormulaEvaluator onto that Workbook.
  117. * @param workbooks Map of workbook names (no square brackets) to an evaluator on that workbook
  118. */
  119. void setupReferencedWorkbooks(Map<String,FormulaEvaluator> workbooks);
  120. /**
  121. * Whether to ignore missing references to external workbooks and
  122. * use cached formula results in the main workbook instead.
  123. * <p>
  124. * In some cases external workbooks referenced by formulas in the main workbook are not available.
  125. * With this method you can control how POI handles such missing references:
  126. * <ul>
  127. * <li>by default ignoreMissingWorkbooks=false and POI throws
  128. * {@link org.apache.poi.ss.formula.CollaboratingWorkbooksEnvironment.WorkbookNotFoundException}
  129. * if an external reference cannot be resolved</li>
  130. * <li>if ignoreMissingWorkbooks=true then POI uses cached formula result
  131. * that already exists in the main workbook</li>
  132. * </ul>
  133. *
  134. * @param ignore whether to ignore missing references to external workbooks
  135. */
  136. void setIgnoreMissingWorkbooks(boolean ignore);
  137. /**
  138. * Perform detailed output of formula evaluation for next evaluation only?
  139. * Is for developer use only (also developers using POI for their XLS files).
  140. * Log-Level WARN is for basic info, INFO for detailed information. These quite
  141. * high levels are used because you have to explicitly enable this specific logging.
  142. * @param value whether to perform detailed output
  143. */
  144. void setDebugEvaluationOutputForNextEval(boolean value);
  145. }