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.

LazyAreaEval.java 3.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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.AreaEval;
  17. import org.apache.poi.ss.formula.eval.AreaEvalBase;
  18. import org.apache.poi.ss.formula.eval.ValueEval;
  19. import org.apache.poi.ss.formula.ptg.AreaI;
  20. import org.apache.poi.ss.formula.ptg.AreaI.OffsetArea;
  21. import org.apache.poi.ss.util.CellReference;
  22. /**
  23. * Provides Lazy Evaluation to 3D Ranges
  24. */
  25. final class LazyAreaEval extends AreaEvalBase {
  26. private final SheetRangeEvaluator _evaluator;
  27. LazyAreaEval(AreaI ptg, SheetRangeEvaluator evaluator) {
  28. super(ptg, evaluator);
  29. _evaluator = evaluator;
  30. }
  31. public LazyAreaEval(int firstRowIndex, int firstColumnIndex, int lastRowIndex,
  32. int lastColumnIndex, SheetRangeEvaluator evaluator) {
  33. super(evaluator, firstRowIndex, firstColumnIndex, lastRowIndex, lastColumnIndex);
  34. _evaluator = evaluator;
  35. }
  36. public ValueEval getRelativeValue(int relativeRowIndex, int relativeColumnIndex) {
  37. return getRelativeValue(getFirstSheetIndex(), relativeRowIndex, relativeColumnIndex);
  38. }
  39. public ValueEval getRelativeValue(int sheetIndex, int relativeRowIndex, int relativeColumnIndex) {
  40. int rowIx = (relativeRowIndex + getFirstRow() ) ;
  41. int colIx = (relativeColumnIndex + getFirstColumn() ) ;
  42. return _evaluator.getEvalForCell(sheetIndex, rowIx, colIx);
  43. }
  44. public AreaEval offset(int relFirstRowIx, int relLastRowIx, int relFirstColIx, int relLastColIx) {
  45. AreaI area = new OffsetArea(getFirstRow(), getFirstColumn(),
  46. relFirstRowIx, relLastRowIx, relFirstColIx, relLastColIx);
  47. return new LazyAreaEval(area, _evaluator);
  48. }
  49. public LazyAreaEval getRow(int rowIndex) {
  50. if (rowIndex >= getHeight()) {
  51. throw new IllegalArgumentException("Invalid rowIndex " + rowIndex
  52. + ". Allowable range is (0.." + getHeight() + ").");
  53. }
  54. int absRowIx = getFirstRow() + rowIndex;
  55. return new LazyAreaEval(absRowIx, getFirstColumn(), absRowIx, getLastColumn(), _evaluator);
  56. }
  57. public LazyAreaEval getColumn(int columnIndex) {
  58. if (columnIndex >= getWidth()) {
  59. throw new IllegalArgumentException("Invalid columnIndex " + columnIndex
  60. + ". Allowable range is (0.." + getWidth() + ").");
  61. }
  62. int absColIx = getFirstColumn() + columnIndex;
  63. return new LazyAreaEval(getFirstRow(), absColIx, getLastRow(), absColIx, _evaluator);
  64. }
  65. public String toString() {
  66. CellReference crA = new CellReference(getFirstRow(), getFirstColumn());
  67. CellReference crB = new CellReference(getLastRow(), getLastColumn());
  68. return getClass().getName() + "[" +
  69. _evaluator.getSheetNameRange() +
  70. '!' +
  71. crA.formatAsString() +
  72. ':' +
  73. crB.formatAsString() +
  74. "]";
  75. }
  76. /**
  77. * @return whether cell at rowIndex and columnIndex is a subtotal
  78. */
  79. public boolean isSubTotal(int rowIndex, int columnIndex){
  80. // delegate the query to the sheet evaluator which has access to internal ptgs
  81. SheetRefEvaluator _sre = _evaluator.getSheetEvaluator(_evaluator.getFirstSheetIndex());
  82. return _sre.isSubTotal(getFirstRow() + rowIndex, getFirstColumn() + columnIndex);
  83. }
  84. }