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.

Subtotal.java 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 static org.apache.poi.ss.formula.functions.AggregateFunction.subtotalInstance;
  17. import java.util.ArrayList;
  18. import java.util.Arrays;
  19. import java.util.Iterator;
  20. import java.util.List;
  21. import org.apache.poi.ss.formula.LazyRefEval;
  22. import org.apache.poi.ss.formula.eval.ErrorEval;
  23. import org.apache.poi.ss.formula.eval.EvaluationException;
  24. import org.apache.poi.ss.formula.eval.NotImplementedFunctionException;
  25. import org.apache.poi.ss.formula.eval.OperandResolver;
  26. import org.apache.poi.ss.formula.eval.ValueEval;
  27. /**
  28. * Implementation for the Excel function SUBTOTAL<p>
  29. *
  30. * <b>Syntax :</b> <br>
  31. * SUBTOTAL ( <b>functionCode</b>, <b>ref1</b>, ref2 ... ) <br>
  32. * <table>
  33. * <caption>Parameter descriptions</caption>
  34. * <tr><td><b>functionCode</b></td><td>(1-11) Selects the underlying aggregate function to be used (see table below)</td></tr>
  35. * <tr><td><b>ref1</b>, ref2 ...</td><td>Arguments to be passed to the underlying aggregate function</td></tr>
  36. * </table><br>
  37. *
  38. * <table>
  39. * <caption>Parameter descriptions</caption>
  40. * <tr><th>functionCode</th><th>Aggregate Function</th></tr>
  41. * <tr><td>1</td><td>AVERAGE</td></tr>
  42. * <tr><td>2</td><td>COUNT</td></tr>
  43. * <tr><td>3</td><td>COUNTA</td></tr>
  44. * <tr><td>4</td><td>MAX</td></tr>
  45. * <tr><td>5</td><td>MIN</td></tr>
  46. * <tr><td>6</td><td>PRODUCT</td></tr>
  47. * <tr><td>7</td><td>STDEV</td></tr>
  48. * <tr><td>8</td><td>STDEVP *</td></tr>
  49. * <tr><td>9</td><td>SUM</td></tr>
  50. * <tr><td>10</td><td>VAR *</td></tr>
  51. * <tr><td>11</td><td>VARP *</td></tr>
  52. * <tr><td>101</td><td>AVERAGE</td></tr>
  53. * <tr><td>102</td><td>COUNT</td></tr>
  54. * <tr><td>103</td><td>COUNTA</td></tr>
  55. * <tr><td>104</td><td>MAX</td></tr>
  56. * <tr><td>105</td><td>MIN</td></tr>
  57. * <tr><td>106</td><td>PRODUCT</td></tr>
  58. * <tr><td>107</td><td>STDEV</td></tr>
  59. * <tr><td>108</td><td>STDEVP *</td></tr>
  60. * <tr><td>109</td><td>SUM</td></tr>
  61. * <tr><td>110</td><td>VAR *</td></tr>
  62. * <tr><td>111</td><td>VARP *</td></tr>
  63. * </table><br>
  64. * * Not implemented in POI yet. Functions 101-111 are the same as functions 1-11 but with
  65. * the option 'ignore hidden values'.
  66. */
  67. public class Subtotal implements Function {
  68. private static Function findFunction(int functionCode) throws EvaluationException {
  69. switch (functionCode) {
  70. case 1: return subtotalInstance(AggregateFunction.AVERAGE, true);
  71. case 2: return Count.subtotalInstance(true);
  72. case 3: return Counta.subtotalInstance(true);
  73. case 4: return subtotalInstance(AggregateFunction.MAX, true);
  74. case 5: return subtotalInstance(AggregateFunction.MIN, true);
  75. case 6: return subtotalInstance(AggregateFunction.PRODUCT, true);
  76. case 7: return subtotalInstance(AggregateFunction.STDEV, true);
  77. case 8: throw new NotImplementedFunctionException("STDEVP");
  78. case 9: return subtotalInstance(AggregateFunction.SUM, true);
  79. case 10: throw new NotImplementedFunctionException("VAR");
  80. case 11: throw new NotImplementedFunctionException("VARP");
  81. case 101: return subtotalInstance(AggregateFunction.AVERAGE, false);
  82. case 102: return Count.subtotalInstance(false);
  83. case 103: return Counta.subtotalInstance(false);
  84. case 104: return subtotalInstance(AggregateFunction.MAX, false);
  85. case 105: return subtotalInstance(AggregateFunction.MIN, false);
  86. case 106: return subtotalInstance(AggregateFunction.PRODUCT, false);
  87. case 107: return subtotalInstance(AggregateFunction.STDEV, false);
  88. case 108: throw new NotImplementedFunctionException("STDEVP SUBTOTAL with 'exclude hidden values' option");
  89. case 109: return subtotalInstance(AggregateFunction.SUM, false);
  90. case 110: throw new NotImplementedFunctionException("VAR SUBTOTAL with 'exclude hidden values' option");
  91. case 111: throw new NotImplementedFunctionException("VARP SUBTOTAL with 'exclude hidden values' option");
  92. }
  93. throw EvaluationException.invalidValue();
  94. }
  95. @Override
  96. public ValueEval evaluate(ValueEval[] args, int srcRowIndex, int srcColumnIndex) {
  97. int nInnerArgs = args.length-1; // -1: first arg is used to select from a basic aggregate function
  98. if (nInnerArgs < 1) {
  99. return ErrorEval.VALUE_INVALID;
  100. }
  101. final Function innerFunc;
  102. int functionCode;
  103. try {
  104. ValueEval ve = OperandResolver.getSingleValue(args[0], srcRowIndex, srcColumnIndex);
  105. functionCode = OperandResolver.coerceValueToInt(ve);
  106. innerFunc = findFunction(functionCode);
  107. } catch (EvaluationException e) {
  108. return e.getErrorEval();
  109. }
  110. // ignore the first arg, this is the function-type, we check for the length above
  111. final List<ValueEval> list = new ArrayList<>(Arrays.asList(args).subList(1, args.length));
  112. Iterator<ValueEval> it = list.iterator();
  113. // See https://support.office.com/en-us/article/SUBTOTAL-function-7b027003-f060-4ade-9040-e478765b9939
  114. // "If there are other subtotals within ref1, ref2,... (or nested subtotals), these nested subtotals are ignored to avoid double counting."
  115. // For array references it is handled in other evaluation steps, but we need to handle this here for references to subtotal-functions
  116. while(it.hasNext()) {
  117. ValueEval eval = it.next();
  118. if(eval instanceof LazyRefEval) {
  119. LazyRefEval lazyRefEval = (LazyRefEval) eval;
  120. if(lazyRefEval.isSubTotal()) {
  121. it.remove();
  122. }
  123. if (functionCode > 100 && lazyRefEval.isRowHidden()) {
  124. it.remove();
  125. }
  126. }
  127. }
  128. return innerFunc.evaluate(list.toArray(new ValueEval[0]), srcRowIndex, srcColumnIndex);
  129. }
  130. }