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.

SheetRefEvaluator.java 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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.ss.formula;
  16. import org.apache.poi.ss.formula.eval.ValueEval;
  17. import org.apache.poi.ss.formula.ptg.FuncVarPtg;
  18. import org.apache.poi.ss.formula.ptg.Ptg;
  19. import org.apache.poi.ss.usermodel.CellType;
  20. /**
  21. * Evaluator for cells within a specific Sheet
  22. */
  23. final class SheetRefEvaluator {
  24. private final WorkbookEvaluator _bookEvaluator;
  25. private final EvaluationTracker _tracker;
  26. private final int _sheetIndex;
  27. private EvaluationSheet _sheet;
  28. public SheetRefEvaluator(WorkbookEvaluator bookEvaluator, EvaluationTracker tracker, int sheetIndex) {
  29. if (sheetIndex < 0) {
  30. throw new IllegalArgumentException("Invalid sheetIndex: " + sheetIndex + ".");
  31. }
  32. _bookEvaluator = bookEvaluator;
  33. _tracker = tracker;
  34. _sheetIndex = sheetIndex;
  35. }
  36. public String getSheetName() {
  37. return _bookEvaluator.getSheetName(_sheetIndex);
  38. }
  39. public ValueEval getEvalForCell(int rowIndex, int columnIndex) {
  40. return _bookEvaluator.evaluateReference(getSheet(), _sheetIndex, rowIndex, columnIndex, _tracker);
  41. }
  42. private EvaluationSheet getSheet() {
  43. if (_sheet == null) {
  44. _sheet = _bookEvaluator.getSheet(_sheetIndex);
  45. }
  46. return _sheet;
  47. }
  48. /**
  49. * @return whether cell at rowIndex and columnIndex is a subtotal
  50. * @see org.apache.poi.ss.formula.functions.Subtotal
  51. */
  52. public boolean isSubTotal(int rowIndex, int columnIndex){
  53. boolean subtotal = false;
  54. EvaluationCell cell = getSheet().getCell(rowIndex, columnIndex);
  55. if(cell != null && cell.getCellType() == CellType.FORMULA){
  56. EvaluationWorkbook wb = _bookEvaluator.getWorkbook();
  57. for(Ptg ptg : wb.getFormulaTokens(cell)){
  58. if(ptg instanceof FuncVarPtg){
  59. FuncVarPtg f = (FuncVarPtg)ptg;
  60. if("SUBTOTAL".equals(f.getName())) {
  61. subtotal = true;
  62. break;
  63. }
  64. }
  65. }
  66. }
  67. return subtotal;
  68. }
  69. }