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.

XSSFFormulaEvaluator.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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.xssf.usermodel;
  16. import org.apache.poi.ss.formula.BaseFormulaEvaluator;
  17. import org.apache.poi.ss.formula.EvaluationCell;
  18. import org.apache.poi.ss.formula.IStabilityClassifier;
  19. import org.apache.poi.ss.formula.WorkbookEvaluator;
  20. import org.apache.poi.ss.formula.udf.UDFFinder;
  21. import org.apache.poi.ss.usermodel.Cell;
  22. /**
  23. * Evaluates formula cells.<p>
  24. *
  25. * For performance reasons, this class keeps a cache of all previously calculated intermediate
  26. * cell values. Be sure to call {@link #clearAllCachedResultValues()} if any workbook cells are changed between
  27. * calls to evaluate~ methods on this class.
  28. */
  29. public final class XSSFFormulaEvaluator extends BaseXSSFFormulaEvaluator {
  30. private XSSFWorkbook _book;
  31. public XSSFFormulaEvaluator(XSSFWorkbook workbook) {
  32. this(workbook, null, null);
  33. }
  34. private XSSFFormulaEvaluator(XSSFWorkbook workbook, IStabilityClassifier stabilityClassifier, UDFFinder udfFinder) {
  35. this(workbook, new WorkbookEvaluator(XSSFEvaluationWorkbook.create(workbook), stabilityClassifier, udfFinder));
  36. }
  37. protected XSSFFormulaEvaluator(XSSFWorkbook workbook, WorkbookEvaluator bookEvaluator) {
  38. super(bookEvaluator);
  39. _book = workbook;
  40. }
  41. /**
  42. * @param stabilityClassifier used to optimise caching performance. Pass <code>null</code>
  43. * for the (conservative) assumption that any cell may have its definition changed after
  44. * evaluation begins.
  45. * @param udfFinder pass <code>null</code> for default (AnalysisToolPak only)
  46. */
  47. public static XSSFFormulaEvaluator create(XSSFWorkbook workbook, IStabilityClassifier stabilityClassifier, UDFFinder udfFinder) {
  48. return new XSSFFormulaEvaluator(workbook, stabilityClassifier, udfFinder);
  49. }
  50. public void notifySetFormula(Cell cell) {
  51. _bookEvaluator.notifyUpdateCell(new XSSFEvaluationCell((XSSFCell)cell));
  52. }
  53. public void notifyDeleteCell(Cell cell) {
  54. _bookEvaluator.notifyDeleteCell(new XSSFEvaluationCell((XSSFCell)cell));
  55. }
  56. public void notifyUpdateCell(Cell cell) {
  57. _bookEvaluator.notifyUpdateCell(new XSSFEvaluationCell((XSSFCell)cell));
  58. }
  59. /**
  60. * Loops over all cells in all sheets of the supplied
  61. * workbook.
  62. * For cells that contain formulas, their formulas are
  63. * evaluated, and the results are saved. These cells
  64. * remain as formula cells.
  65. * For cells that do not contain formulas, no changes
  66. * are made.
  67. * This is a helpful wrapper around looping over all
  68. * cells, and calling evaluateFormulaCell on each one.
  69. */
  70. public static void evaluateAllFormulaCells(XSSFWorkbook wb) {
  71. BaseFormulaEvaluator.evaluateAllFormulaCells(wb);
  72. }
  73. @Override
  74. public XSSFCell evaluateInCell(Cell cell) {
  75. return (XSSFCell) super.evaluateInCell(cell);
  76. }
  77. /**
  78. * Loops over all cells in all sheets of the supplied
  79. * workbook.
  80. * For cells that contain formulas, their formulas are
  81. * evaluated, and the results are saved. These cells
  82. * remain as formula cells.
  83. * For cells that do not contain formulas, no changes
  84. * are made.
  85. * This is a helpful wrapper around looping over all
  86. * cells, and calling evaluateFormulaCell on each one.
  87. */
  88. public void evaluateAll() {
  89. evaluateAllFormulaCells(_book, this);
  90. }
  91. /**
  92. * Turns a XSSFCell into a XSSFEvaluationCell
  93. */
  94. protected EvaluationCell toEvaluationCell(Cell cell) {
  95. if (!(cell instanceof XSSFCell)){
  96. throw new IllegalArgumentException("Unexpected type of cell: " + cell.getClass() + "." +
  97. " Only XSSFCells can be evaluated.");
  98. }
  99. return new XSSFEvaluationCell((XSSFCell)cell);
  100. }
  101. }