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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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.EvaluationWorkbook;
  19. import org.apache.poi.ss.formula.WorkbookEvaluator;
  20. import org.apache.poi.ss.formula.eval.BoolEval;
  21. import org.apache.poi.ss.formula.eval.ErrorEval;
  22. import org.apache.poi.ss.formula.eval.NumberEval;
  23. import org.apache.poi.ss.formula.eval.StringEval;
  24. import org.apache.poi.ss.formula.eval.ValueEval;
  25. import org.apache.poi.ss.formula.ptg.Area3DPxg;
  26. import org.apache.poi.ss.formula.ptg.Ptg;
  27. import org.apache.poi.ss.usermodel.Cell;
  28. import org.apache.poi.ss.usermodel.CellType;
  29. import org.apache.poi.ss.usermodel.CellValue;
  30. import org.apache.poi.ss.usermodel.RichTextString;
  31. import org.apache.poi.ss.util.CellReference;
  32. import org.apache.poi.xssf.model.ExternalLinksTable;
  33. /**
  34. * Internal POI use only - parent of XSSF and SXSSF formula evaluators
  35. */
  36. public abstract class BaseXSSFFormulaEvaluator extends BaseFormulaEvaluator {
  37. protected BaseXSSFFormulaEvaluator(WorkbookEvaluator bookEvaluator) {
  38. super(bookEvaluator);
  39. }
  40. @Override
  41. protected RichTextString createRichTextString(String str) {
  42. return new XSSFRichTextString(str);
  43. }
  44. /**
  45. * Turns a XSSFCell / SXSSFCell into a XSSFEvaluationCell
  46. */
  47. protected abstract EvaluationCell toEvaluationCell(Cell cell);
  48. /**
  49. * Returns a CellValue wrapper around the supplied ValueEval instance.
  50. */
  51. @Override
  52. protected CellValue evaluateFormulaCellValue(Cell cell) {
  53. EvaluationCell evalCell = toEvaluationCell(cell);
  54. ValueEval eval = _bookEvaluator.evaluate(evalCell);
  55. cacheExternalWorkbookCells(evalCell);
  56. if (eval instanceof NumberEval) {
  57. NumberEval ne = (NumberEval) eval;
  58. return new CellValue(ne.getNumberValue());
  59. }
  60. if (eval instanceof BoolEval) {
  61. BoolEval be = (BoolEval) eval;
  62. return CellValue.valueOf(be.getBooleanValue());
  63. }
  64. if (eval instanceof StringEval) {
  65. StringEval ne = (StringEval) eval;
  66. return new CellValue(ne.getStringValue());
  67. }
  68. if (eval instanceof ErrorEval) {
  69. return CellValue.getError(((ErrorEval)eval).getErrorCode());
  70. }
  71. throw new IllegalStateException("Unexpected eval class (" + eval.getClass().getName() + ")");
  72. }
  73. /**
  74. * cache cell value of external workbook
  75. *
  76. * @param evalCell sourceCell
  77. */
  78. private void cacheExternalWorkbookCells(EvaluationCell evalCell) {
  79. //
  80. Ptg[] formulaTokens = getEvaluationWorkbook().getFormulaTokens(evalCell);
  81. for (Ptg ptg : formulaTokens) {
  82. if (ptg instanceof Area3DPxg) {
  83. Area3DPxg area3DPxg = (Area3DPxg) ptg;
  84. if (area3DPxg.getExternalWorkbookNumber() > 0) {
  85. EvaluationWorkbook.ExternalSheet externalSheet = getEvaluationWorkbook().getExternalSheet(area3DPxg.getSheetName(), area3DPxg.getLastSheetName(), area3DPxg.getExternalWorkbookNumber());
  86. XSSFCell xssfCell = ((XSSFEvaluationCell) evalCell).getXSSFCell();
  87. XSSFWorkbook externalWorkbook = (XSSFWorkbook) xssfCell.getSheet().getWorkbook().getCreationHelper().getReferencedWorkbooks().get(externalSheet.getWorkbookName());
  88. ExternalLinksTable externalLinksTable = xssfCell.getSheet().getWorkbook().getExternalLinksTable().get(area3DPxg.getExternalWorkbookNumber() - 1);
  89. int firstSheet = externalWorkbook.getSheetIndex(area3DPxg.getSheetName());
  90. int lastSheet = firstSheet;
  91. if (area3DPxg.getLastSheetName() != null) {
  92. lastSheet = externalWorkbook.getSheetIndex(area3DPxg.getLastSheetName());
  93. }
  94. for (int sheetIndex = firstSheet; sheetIndex <= lastSheet; sheetIndex++) {
  95. XSSFSheet sheet = externalWorkbook.getSheetAt(sheetIndex);
  96. int firstRow = area3DPxg.getFirstRow();
  97. int lastRow = area3DPxg.getLastRow();
  98. for (int rowIndex = firstRow; rowIndex <= lastRow; rowIndex++) {
  99. XSSFRow row = sheet.getRow(rowIndex);
  100. int firstColumn = area3DPxg.getFirstColumn();
  101. int lastColumn = area3DPxg.getLastColumn();
  102. for (int cellIndex = firstColumn; cellIndex <= lastColumn; cellIndex++) {
  103. XSSFCell cell = row.getCell(cellIndex);
  104. String cellValue = cell.getRawValue();
  105. String cellR = new CellReference(cell).formatAsString(false);
  106. externalLinksTable.cacheData(sheet.getSheetName(), (long)rowIndex + 1, cellR, cellValue);
  107. }
  108. }
  109. }
  110. }
  111. }
  112. }
  113. }
  114. @Override
  115. protected void setCellType(Cell cell, CellType cellType) {
  116. if (cell instanceof XSSFCell) {
  117. EvaluationWorkbook evaluationWorkbook = getEvaluationWorkbook();
  118. BaseXSSFEvaluationWorkbook xewb = BaseXSSFEvaluationWorkbook.class.isAssignableFrom(evaluationWorkbook.getClass()) ? (BaseXSSFEvaluationWorkbook) evaluationWorkbook : null;
  119. ((XSSFCell) cell).setCellType(cellType, xewb);
  120. } else {
  121. // could be an SXSSFCell
  122. cell.setCellType(cellType);
  123. }
  124. }
  125. }