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.

Index.java 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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.functions;
  16. import org.apache.poi.ss.formula.eval.BlankEval;
  17. import org.apache.poi.ss.formula.eval.ErrorEval;
  18. import org.apache.poi.ss.formula.eval.EvaluationException;
  19. import org.apache.poi.ss.formula.eval.MissingArgEval;
  20. import org.apache.poi.ss.formula.eval.OperandResolver;
  21. import org.apache.poi.ss.formula.eval.RefEval;
  22. import org.apache.poi.ss.formula.eval.ValueEval;
  23. import org.apache.poi.ss.formula.TwoDEval;
  24. /**
  25. * Implementation for the Excel function INDEX
  26. * <p>
  27. *
  28. * Syntax : <p>
  29. * INDEX ( reference, row_num[, column_num [, area_num]])<p>
  30. * INDEX ( array, row_num[, column_num])
  31. * <table>
  32. * <caption>Parameter descriptions</caption>
  33. * <tr><th>reference</th><td>typically an area reference, possibly a union of areas</td></tr>
  34. * <tr><th>array</th><td>a literal array value (currently not supported)</td></tr>
  35. * <tr><th>row_num</th><td>selects the row within the array or area reference</td></tr>
  36. * <tr><th>column_num</th><td>selects column within the array or area reference. default is 1</td></tr>
  37. * <tr><th>area_num</th><td>used when reference is a union of areas</td></tr>
  38. * </table>
  39. */
  40. public final class Index implements Function2Arg, Function3Arg, Function4Arg, ArrayMode {
  41. @Override
  42. public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0, ValueEval arg1) {
  43. TwoDEval reference = convertFirstArg(arg0);
  44. int columnIx = 0;
  45. try {
  46. int rowIx = resolveIndexArg(arg1, srcRowIndex, srcColumnIndex);
  47. if (!reference.isColumn()) {
  48. if (!reference.isRow()) {
  49. // always an error with 2-D area refs
  50. // Note - the type of error changes if the pRowArg is negative
  51. return ErrorEval.REF_INVALID;
  52. }
  53. // When the two-arg version of INDEX() has been invoked and the reference
  54. // is a single column ref, the row arg seems to get used as the column index
  55. columnIx = rowIx;
  56. rowIx = 0;
  57. }
  58. return getValueFromArea(reference, rowIx, columnIx);
  59. } catch (EvaluationException e) {
  60. return e.getErrorEval();
  61. }
  62. }
  63. @Override
  64. public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0, ValueEval arg1,
  65. ValueEval arg2) {
  66. TwoDEval reference = convertFirstArg(arg0);
  67. try {
  68. int columnIx = resolveIndexArg(arg2, srcRowIndex, srcColumnIndex);
  69. int rowIx = resolveIndexArg(arg1, srcRowIndex, srcColumnIndex);
  70. return getValueFromArea(reference, rowIx, columnIx);
  71. } catch (EvaluationException e) {
  72. return e.getErrorEval();
  73. }
  74. }
  75. @Override
  76. public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0, ValueEval arg1,
  77. ValueEval arg2, ValueEval arg3) {
  78. throw new RuntimeException("Incomplete code"
  79. + " - don't know how to support the 'area_num' parameter yet)");
  80. // Excel expression might look like this "INDEX( (A1:B4, C3:D6, D2:E5 ), 1, 2, 3)
  81. // In this example, the 3rd area would be used i.e. D2:E5, and the overall result would be E2
  82. // Token array might be encoded like this: MemAreaPtg, AreaPtg, AreaPtg, UnionPtg, UnionPtg, ParenthesesPtg
  83. // The formula parser doesn't seem to support this yet. Not sure if the evaluator does either
  84. }
  85. private static TwoDEval convertFirstArg(ValueEval arg0) {
  86. if (arg0 instanceof RefEval) {
  87. // convert to area ref for simpler code in getValueFromArea()
  88. return ((RefEval) arg0).offset(0, 0, 0, 0);
  89. }
  90. if((arg0 instanceof TwoDEval)) {
  91. return (TwoDEval) arg0;
  92. }
  93. // else the other variation of this function takes an array as the first argument
  94. // it seems like interface 'ArrayEval' does not even exist yet
  95. throw new RuntimeException("Incomplete code - cannot handle first arg of type ("
  96. + arg0.getClass().getName() + ")");
  97. }
  98. @Override
  99. public ValueEval evaluate(ValueEval[] args, int srcRowIndex, int srcColumnIndex) {
  100. switch (args.length) {
  101. case 2:
  102. return evaluate(srcRowIndex, srcColumnIndex, args[0], args[1]);
  103. case 3:
  104. return evaluate(srcRowIndex, srcColumnIndex, args[0], args[1], args[2]);
  105. case 4:
  106. return evaluate(srcRowIndex, srcColumnIndex, args[0], args[1], args[2], args[3]);
  107. }
  108. return ErrorEval.VALUE_INVALID;
  109. }
  110. private static ValueEval getValueFromArea(TwoDEval ae, int pRowIx, int pColumnIx)
  111. throws EvaluationException {
  112. assert pRowIx >= 0;
  113. assert pColumnIx >= 0;
  114. TwoDEval result = ae;
  115. if (pRowIx != 0) {
  116. // Slightly irregular logic for bounds checking errors
  117. if (pRowIx > ae.getHeight()) {
  118. // high bounds check fail gives #REF! if arg was explicitly passed
  119. throw new EvaluationException(ErrorEval.REF_INVALID);
  120. }
  121. result = result.getRow(pRowIx-1);
  122. }
  123. if (pColumnIx != 0) {
  124. // Slightly irregular logic for bounds checking errors
  125. if (pColumnIx > ae.getWidth()) {
  126. // high bounds check fail gives #REF! if arg was explicitly passed
  127. throw new EvaluationException(ErrorEval.REF_INVALID);
  128. }
  129. result = result.getColumn(pColumnIx-1);
  130. }
  131. return result;
  132. }
  133. /**
  134. * @param arg a 1-based index.
  135. * @return the resolved 1-based index. Zero if the arg was missing or blank
  136. * @throws EvaluationException if the arg is an error value evaluates to a negative numeric value
  137. */
  138. private static int resolveIndexArg(ValueEval arg, int srcCellRow, int srcCellCol) throws EvaluationException {
  139. ValueEval ev = OperandResolver.getSingleValue(arg, srcCellRow, srcCellCol);
  140. if (ev == MissingArgEval.instance) {
  141. return 0;
  142. }
  143. if (ev == BlankEval.instance) {
  144. return 0;
  145. }
  146. int result = OperandResolver.coerceValueToInt(ev);
  147. if (result < 0) {
  148. throw new EvaluationException(ErrorEval.VALUE_INVALID);
  149. }
  150. return result;
  151. }
  152. }