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.

Rank.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * ====================================================================
  3. * Licensed to the Apache Software Foundation (ASF) under one or more
  4. * contributor license agreements. See the NOTICE file distributed with
  5. * this work for additional information regarding copyright ownership.
  6. * The ASF licenses this file to You under the Apache License, Version 2.0
  7. * (the "License"); you may not use this file except in compliance with
  8. * the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. * ====================================================================
  18. */
  19. package org.apache.poi.ss.formula.functions;
  20. import org.apache.poi.ss.formula.eval.AreaEval;
  21. import org.apache.poi.ss.formula.eval.ErrorEval;
  22. import org.apache.poi.ss.formula.eval.EvaluationException;
  23. import org.apache.poi.ss.formula.eval.NumberEval;
  24. import org.apache.poi.ss.formula.eval.OperandResolver;
  25. import org.apache.poi.ss.formula.eval.RefEval;
  26. import org.apache.poi.ss.formula.eval.ValueEval;
  27. /**
  28. * Returns the rank of a number in a list of numbers. The rank of a number is its size relative to other values in a list.
  29. * Syntax:
  30. * RANK(number,ref,order)
  31. * Number is the number whose rank you want to find.
  32. * Ref is an array of, or a reference to, a list of numbers. Nonnumeric values in ref are ignored.
  33. * Order is a number specifying how to rank number.
  34. * If order is 0 (zero) or omitted, Microsoft Excel ranks number as if ref were a list sorted in descending order.
  35. * If order is any nonzero value, Microsoft Excel ranks number as if ref were a list sorted in ascending order.
  36. *
  37. * @author Rubin Wang
  38. */
  39. public class Rank extends Var2or3ArgFunction {
  40. public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0, ValueEval arg1) {
  41. AreaEval aeRange;
  42. double result;
  43. try {
  44. ValueEval ve = OperandResolver.getSingleValue(arg0, srcRowIndex, srcColumnIndex);
  45. result = OperandResolver.coerceValueToDouble(ve);
  46. if (Double.isNaN(result) || Double.isInfinite(result)) {
  47. throw new EvaluationException(ErrorEval.NUM_ERROR);
  48. }
  49. aeRange = convertRangeArg(arg1);
  50. } catch (EvaluationException e) {
  51. return e.getErrorEval();
  52. }
  53. return eval(srcRowIndex, srcColumnIndex, result, aeRange, true);
  54. }
  55. public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0, ValueEval arg1, ValueEval arg2) {
  56. AreaEval aeRange;
  57. double result;
  58. boolean order=false;
  59. try {
  60. ValueEval ve = OperandResolver.getSingleValue(arg0, srcRowIndex, srcColumnIndex);
  61. result = OperandResolver.coerceValueToDouble(ve);
  62. if (Double.isNaN(result) || Double.isInfinite(result)) {
  63. throw new EvaluationException(ErrorEval.NUM_ERROR);
  64. }
  65. aeRange = convertRangeArg(arg1);
  66. ve = OperandResolver.getSingleValue(arg2, srcRowIndex, srcColumnIndex);
  67. int order_value = OperandResolver.coerceValueToInt(ve);
  68. if(order_value==0){
  69. order=true;
  70. }else if(order_value==1){
  71. order=false;
  72. }else throw new EvaluationException(ErrorEval.NUM_ERROR);
  73. } catch (EvaluationException e) {
  74. return e.getErrorEval();
  75. }
  76. return eval(srcRowIndex, srcColumnIndex, result, aeRange, order);
  77. }
  78. private static ValueEval eval(int srcRowIndex, int srcColumnIndex, double arg0, AreaEval aeRange, boolean descending_order) {
  79. int rank = 1;
  80. int height=aeRange.getHeight();
  81. int width= aeRange.getWidth();
  82. for (int r=0; r<height; r++) {
  83. for (int c=0; c<width; c++) {
  84. Double value = getValue(aeRange, r, c);
  85. if(value==null)continue;
  86. if(descending_order && value>arg0 || !descending_order && value<arg0){
  87. rank++;
  88. }
  89. }
  90. }
  91. return new NumberEval(rank);
  92. }
  93. private static Double getValue(AreaEval aeRange, int relRowIndex, int relColIndex) {
  94. ValueEval addend = aeRange.getRelativeValue(relRowIndex, relColIndex);
  95. if (addend instanceof NumberEval) {
  96. return ((NumberEval)addend).getNumberValue();
  97. }
  98. // everything else (including string and boolean values) counts as zero
  99. return null;
  100. }
  101. private static AreaEval convertRangeArg(ValueEval eval) throws EvaluationException {
  102. if (eval instanceof AreaEval) {
  103. return (AreaEval) eval;
  104. }
  105. if (eval instanceof RefEval) {
  106. return ((RefEval)eval).offset(0, 0, 0, 0);
  107. }
  108. throw new EvaluationException(ErrorEval.VALUE_INVALID);
  109. }
  110. }