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.

XSSFEvaluationWorkbook.java 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 java.util.HashMap;
  17. import java.util.Map;
  18. import org.apache.poi.ss.formula.EvaluationCell;
  19. import org.apache.poi.ss.formula.EvaluationSheet;
  20. import org.apache.poi.ss.formula.FormulaParser;
  21. import org.apache.poi.ss.formula.FormulaType;
  22. import org.apache.poi.ss.formula.ptg.Ptg;
  23. import org.apache.poi.util.Internal;
  24. /**
  25. * Internal POI use only
  26. */
  27. @Internal
  28. public final class XSSFEvaluationWorkbook extends BaseXSSFEvaluationWorkbook {
  29. private final Map<XSSFSheet, XSSFEvaluationSheet> _sheetCache = new HashMap<>();
  30. public static XSSFEvaluationWorkbook create(XSSFWorkbook book) {
  31. if (book == null) {
  32. return null;
  33. }
  34. return new XSSFEvaluationWorkbook(book);
  35. }
  36. private XSSFEvaluationWorkbook(XSSFWorkbook book) {
  37. super(book);
  38. }
  39. /* (non-JavaDoc), inherit JavaDoc from EvaluationSheet
  40. * @since POI 3.15 beta 3
  41. */
  42. @Override
  43. public void clearAllCachedResultValues() {
  44. super.clearAllCachedResultValues();
  45. _sheetCache.clear();
  46. }
  47. @Override
  48. public int getSheetIndex(EvaluationSheet evalSheet) {
  49. XSSFSheet sheet = ((XSSFEvaluationSheet)evalSheet).getXSSFSheet();
  50. return _uBook.getSheetIndex(sheet);
  51. }
  52. @Override
  53. public EvaluationSheet getSheet(int sheetIndex) {
  54. // verify index and let the method in _uBook throw the exception so we report
  55. // it the same way as in other places
  56. if (sheetIndex < 0 || sheetIndex >= _uBook.getNumberOfSheets()) {
  57. // this will throw an exception now as the index is out of bounds
  58. _uBook.getSheetAt(sheetIndex);
  59. }
  60. // Performance optimization: build sheet cache for each sheet to avoid re-creating
  61. // the XSSFEvaluationSheet each time a new cell is evaluated
  62. // EvaluationWorkbooks make not guarantee to synchronize changes made to
  63. // the underlying workbook after the EvaluationWorkbook is created.
  64. final XSSFSheet sheet = _uBook.getSheetAt(sheetIndex);
  65. return _sheetCache.computeIfAbsent(sheet, rows -> new XSSFEvaluationSheet(sheet));
  66. }
  67. @Override
  68. public Ptg[] getFormulaTokens(EvaluationCell evalCell) {
  69. final XSSFCell cell = ((XSSFEvaluationCell)evalCell).getXSSFCell();
  70. final int sheetIndex = _uBook.getSheetIndex(cell.getSheet());
  71. final int rowIndex = cell.getRowIndex();
  72. return FormulaParser.parse(cell.getCellFormula(this), this, FormulaType.CELL, sheetIndex, rowIndex);
  73. }
  74. }