選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

XSSFFormulaEvaluator.java 4.3KB

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