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.

BaseXSSFFormulaEvaluator.java 3.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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.WorkbookEvaluator;
  19. import org.apache.poi.ss.formula.eval.BoolEval;
  20. import org.apache.poi.ss.formula.eval.ErrorEval;
  21. import org.apache.poi.ss.formula.eval.NumberEval;
  22. import org.apache.poi.ss.formula.eval.StringEval;
  23. import org.apache.poi.ss.formula.eval.ValueEval;
  24. import org.apache.poi.ss.usermodel.Cell;
  25. import org.apache.poi.ss.usermodel.CellType;
  26. import org.apache.poi.ss.usermodel.CellValue;
  27. import org.apache.poi.ss.usermodel.RichTextString;
  28. /**
  29. * Internal POI use only - parent of XSSF and SXSSF formula evaluators
  30. */
  31. public abstract class BaseXSSFFormulaEvaluator extends BaseFormulaEvaluator {
  32. protected BaseXSSFFormulaEvaluator(WorkbookEvaluator bookEvaluator) {
  33. super(bookEvaluator);
  34. }
  35. @Override
  36. protected RichTextString createRichTextString(String str) {
  37. return new XSSFRichTextString(str);
  38. }
  39. public void notifySetFormula(Cell cell) {
  40. _bookEvaluator.notifyUpdateCell(new XSSFEvaluationCell((XSSFCell)cell));
  41. }
  42. public void notifyDeleteCell(Cell cell) {
  43. _bookEvaluator.notifyDeleteCell(new XSSFEvaluationCell((XSSFCell)cell));
  44. }
  45. public void notifyUpdateCell(Cell cell) {
  46. _bookEvaluator.notifyUpdateCell(new XSSFEvaluationCell((XSSFCell)cell));
  47. }
  48. /**
  49. * Turns a XSSFCell / SXSSFCell into a XSSFEvaluationCell
  50. */
  51. protected abstract EvaluationCell toEvaluationCell(Cell cell);
  52. /**
  53. * Returns a CellValue wrapper around the supplied ValueEval instance.
  54. */
  55. protected CellValue evaluateFormulaCellValue(Cell cell) {
  56. EvaluationCell evalCell = toEvaluationCell(cell);
  57. ValueEval eval = _bookEvaluator.evaluate(evalCell);
  58. if (eval instanceof NumberEval) {
  59. NumberEval ne = (NumberEval) eval;
  60. return new CellValue(ne.getNumberValue());
  61. }
  62. if (eval instanceof BoolEval) {
  63. BoolEval be = (BoolEval) eval;
  64. return CellValue.valueOf(be.getBooleanValue());
  65. }
  66. if (eval instanceof StringEval) {
  67. StringEval ne = (StringEval) eval;
  68. return new CellValue(ne.getStringValue());
  69. }
  70. if (eval instanceof ErrorEval) {
  71. return CellValue.getError(((ErrorEval)eval).getErrorCode());
  72. }
  73. throw new RuntimeException("Unexpected eval class (" + eval.getClass().getName() + ")");
  74. }
  75. }