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.

Vlookup.java 3.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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.BoolEval;
  17. import org.apache.poi.ss.formula.eval.EvaluationException;
  18. import org.apache.poi.ss.formula.eval.OperandResolver;
  19. import org.apache.poi.ss.formula.eval.ValueEval;
  20. import org.apache.poi.ss.formula.functions.LookupUtils.ValueVector;
  21. import org.apache.poi.ss.formula.TwoDEval;
  22. /**
  23. * Implementation of the VLOOKUP() function.<p/>
  24. *
  25. * VLOOKUP finds a row in a lookup table by the first column value and returns the value from another column.<br/>
  26. *
  27. * <b>Syntax</b>:<br/>
  28. * <b>VLOOKUP</b>(<b>lookup_value</b>, <b>table_array</b>, <b>col_index_num</b>, range_lookup)<p/>
  29. *
  30. * <b>lookup_value</b> The value to be found in the first column of the table array.<br/>
  31. * <b>table_array</b> An area reference for the lookup data. <br/>
  32. * <b>col_index_num</b> a 1 based index specifying which column value of the lookup data will be returned.<br/>
  33. * <b>range_lookup</b> If TRUE (default), VLOOKUP finds the largest value less than or equal to
  34. * the lookup_value. If FALSE, only exact matches will be considered<br/>
  35. *
  36. * @author Josh Micich
  37. * @author Cedric Walter at innoveo.com
  38. */
  39. public final class Vlookup extends Var3or4ArgFunction {
  40. private static final ValueEval DEFAULT_ARG3 = BoolEval.TRUE;
  41. public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0, ValueEval arg1,
  42. ValueEval arg2) {
  43. return evaluate(srcRowIndex, srcColumnIndex, arg0, arg1, arg2, DEFAULT_ARG3);
  44. }
  45. public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval lookup_value, ValueEval table_array,
  46. ValueEval col_index, ValueEval range_lookup) {
  47. try {
  48. // Evaluation order:
  49. // lookup_value , table_array, range_lookup, find lookup value, col_index, fetch result
  50. ValueEval lookupValue = OperandResolver.getSingleValue(lookup_value, srcRowIndex, srcColumnIndex);
  51. TwoDEval tableArray = LookupUtils.resolveTableArrayArg(table_array);
  52. boolean isRangeLookup = LookupUtils.resolveRangeLookupArg(range_lookup, srcRowIndex, srcColumnIndex);
  53. int rowIndex = LookupUtils.lookupIndexOfValue(lookupValue, LookupUtils.createColumnVector(tableArray, 0), isRangeLookup);
  54. int colIndex = LookupUtils.resolveRowOrColIndexArg(col_index, srcRowIndex, srcColumnIndex);
  55. ValueVector resultCol = createResultColumnVector(tableArray, colIndex);
  56. return resultCol.getItem(rowIndex);
  57. } catch (EvaluationException e) {
  58. return e.getErrorEval();
  59. }
  60. }
  61. /**
  62. * Returns one column from an <tt>AreaEval</tt>
  63. *
  64. * @param colIndex assumed to be non-negative
  65. *
  66. * @throws EvaluationException (#REF!) if colIndex is too high
  67. */
  68. private ValueVector createResultColumnVector(TwoDEval tableArray, int colIndex) throws EvaluationException {
  69. if(colIndex >= tableArray.getWidth()) {
  70. throw EvaluationException.invalidRef();
  71. }
  72. return LookupUtils.createColumnVector(tableArray, colIndex);
  73. }
  74. }