Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

Rank.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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.*;
  21. /**
  22. * 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.
  23. * Syntax:
  24. * RANK(number,ref,order)
  25. * Number is the number whose rank you want to find.
  26. * Ref is an array of, or a reference to, a list of numbers. Nonnumeric values in ref are ignored.
  27. * Order is a number specifying how to rank number.
  28. * If order is 0 (zero) or omitted, Microsoft Excel ranks number as if ref were a list sorted in descending order.
  29. * If order is any nonzero value, Microsoft Excel ranks number as if ref were a list sorted in ascending order.
  30. *
  31. * @author Rubin Wang
  32. */
  33. public class Rank extends Var2or3ArgFunction {
  34. public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0, ValueEval arg1) {
  35. try {
  36. ValueEval ve = OperandResolver.getSingleValue(arg0, srcRowIndex, srcColumnIndex);
  37. double result = OperandResolver.coerceValueToDouble(ve);
  38. if (Double.isNaN(result) || Double.isInfinite(result)) {
  39. throw new EvaluationException(ErrorEval.NUM_ERROR);
  40. }
  41. if(arg1 instanceof RefListEval) {
  42. return eval(result, ((RefListEval)arg1), true);
  43. }
  44. final AreaEval aeRange = convertRangeArg(arg1);
  45. return eval(result, aeRange, true);
  46. } catch (EvaluationException e) {
  47. return e.getErrorEval();
  48. }
  49. }
  50. public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0, ValueEval arg1, ValueEval arg2) {
  51. try {
  52. ValueEval ve = OperandResolver.getSingleValue(arg0, srcRowIndex, srcColumnIndex);
  53. final double result = OperandResolver.coerceValueToDouble(ve);
  54. if (Double.isNaN(result) || Double.isInfinite(result)) {
  55. throw new EvaluationException(ErrorEval.NUM_ERROR);
  56. }
  57. ve = OperandResolver.getSingleValue(arg2, srcRowIndex, srcColumnIndex);
  58. int order_value = OperandResolver.coerceValueToInt(ve);
  59. final boolean order;
  60. if(order_value==0) {
  61. order = true;
  62. } else if(order_value==1) {
  63. order = false;
  64. } else {
  65. throw new EvaluationException(ErrorEval.NUM_ERROR);
  66. }
  67. if(arg1 instanceof RefListEval) {
  68. return eval(result, ((RefListEval)arg1), order);
  69. }
  70. final AreaEval aeRange = convertRangeArg(arg1);
  71. return eval(result, aeRange, order);
  72. } catch (EvaluationException e) {
  73. return e.getErrorEval();
  74. }
  75. }
  76. private static ValueEval eval(double arg0, AreaEval aeRange, boolean descending_order) {
  77. int rank = 1;
  78. int height=aeRange.getHeight();
  79. int width= aeRange.getWidth();
  80. for (int r=0; r<height; r++) {
  81. for (int c=0; c<width; c++) {
  82. Double value = getValue(aeRange, r, c);
  83. if(value==null)continue;
  84. if(descending_order && value>arg0 || !descending_order && value<arg0){
  85. rank++;
  86. }
  87. }
  88. }
  89. return new NumberEval(rank);
  90. }
  91. private static ValueEval eval(double arg0, RefListEval aeRange, boolean descending_order) {
  92. int rank = 1;
  93. for(ValueEval ve : aeRange.getList()) {
  94. if (ve instanceof RefEval) {
  95. ve = ((RefEval) ve).getInnerValueEval(((RefEval) ve).getFirstSheetIndex());
  96. }
  97. final Double value;
  98. if (ve instanceof NumberEval) {
  99. value = ((NumberEval)ve).getNumberValue();
  100. } else {
  101. continue;
  102. }
  103. if(descending_order && value>arg0 || !descending_order && value<arg0){
  104. rank++;
  105. }
  106. }
  107. return new NumberEval(rank);
  108. }
  109. private static Double getValue(AreaEval aeRange, int relRowIndex, int relColIndex) {
  110. ValueEval addend = aeRange.getRelativeValue(relRowIndex, relColIndex);
  111. if (addend instanceof NumberEval) {
  112. return ((NumberEval)addend).getNumberValue();
  113. }
  114. // everything else (including string and boolean values) counts as zero
  115. return null;
  116. }
  117. private static AreaEval convertRangeArg(ValueEval eval) throws EvaluationException {
  118. if (eval instanceof AreaEval) {
  119. return (AreaEval) eval;
  120. }
  121. if (eval instanceof RefEval) {
  122. return ((RefEval)eval).offset(0, 0, 0, 0);
  123. }
  124. throw new EvaluationException(ErrorEval.VALUE_INVALID);
  125. }
  126. }