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 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. /**
  17. * Evaluates formula cells.<p/>
  18. *
  19. * For performance reasons, this class keeps a cache of all previously calculated intermediate
  20. * cell values. Be sure to call {@link #clearAllCachedResultValues()} if any workbook cells are changed between
  21. * calls to evaluate~ methods on this class.
  22. *
  23. * @author Amol S. Deshmukh &lt; amolweb at ya hoo dot com &gt;
  24. * @author Josh Micich
  25. */
  26. public interface FormulaEvaluator {
  27. /**
  28. * Should be called whenever there are changes to input cells in the evaluated workbook.
  29. * Failure to call this method after changing cell values will cause incorrect behaviour
  30. * of the evaluate~ methods of this class
  31. */
  32. void clearAllCachedResultValues();
  33. /**
  34. * Should be called to tell the cell value cache that the specified (value or formula) cell
  35. * has changed.
  36. * Failure to call this method after changing cell values will cause incorrect behaviour
  37. * of the evaluate~ methods of this class
  38. */
  39. void notifySetFormula(Cell cell);
  40. /**
  41. * Should be called to tell the cell value cache that the specified cell has just become a
  42. * formula cell, or the formula text has changed
  43. */
  44. void notifyDeleteCell(Cell cell);
  45. /**
  46. * Should be called to tell the cell value cache that the specified (value or formula) cell
  47. * has changed.
  48. * Failure to call this method after changing cell values will cause incorrect behaviour
  49. * of the evaluate~ methods of this class
  50. */
  51. void notifyUpdateCell(Cell cell);
  52. /**
  53. * If cell contains a formula, the formula is evaluated and returned,
  54. * else the CellValue simply copies the appropriate cell value from
  55. * the cell and also its cell type. This method should be preferred over
  56. * evaluateInCell() when the call should not modify the contents of the
  57. * original cell.
  58. * @param cell
  59. */
  60. CellValue evaluate(Cell cell);
  61. /**
  62. * If cell contains formula, it evaluates the formula,
  63. * and saves the result of the formula. The cell
  64. * remains as a formula cell.
  65. * Else if cell does not contain formula, this method leaves
  66. * the cell unchanged.
  67. * Note that the type of the formula result is returned,
  68. * so you know what kind of value is also stored with
  69. * the formula.
  70. * <pre>
  71. * int evaluatedCellType = evaluator.evaluateFormulaCell(cell);
  72. * </pre>
  73. * Be aware that your cell will hold both the formula,
  74. * and the result. If you want the cell replaced with
  75. * the result of the formula, use {@link #evaluateInCell(Cell)}
  76. * @param cell The cell to evaluate
  77. * @return The type of the formula result (the cell's type remains as Cell.CELL_TYPE_FORMULA however)
  78. */
  79. int evaluateFormulaCell(Cell cell);
  80. /**
  81. * If cell contains formula, it evaluates the formula, and
  82. * puts the formula result back into the cell, in place
  83. * of the old formula.
  84. * Else if cell does not contain formula, this method leaves
  85. * the cell unchanged.
  86. * Note that the same instance of Cell is returned to
  87. * allow chained calls like:
  88. * <pre>
  89. * int evaluatedCellType = evaluator.evaluateInCell(cell).getCellType();
  90. * </pre>
  91. * Be aware that your cell value will be changed to hold the
  92. * result of the formula. If you simply want the formula
  93. * value computed for you, use {@link #evaluateFormulaCell(Cell)}
  94. * @param cell
  95. */
  96. Cell evaluateInCell(Cell cell);
  97. }