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.

FunctionEval.java 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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.eval;
  16. import org.apache.poi.ss.formula.function.FunctionMetadata;
  17. import org.apache.poi.ss.formula.function.FunctionMetadataRegistry;
  18. import org.apache.poi.ss.formula.functions.*;
  19. /**
  20. * @author Amol S. Deshmukh < amolweb at ya hoo dot com >
  21. */
  22. public final class FunctionEval {
  23. /**
  24. * Some function IDs that require special treatment
  25. */
  26. private static final class FunctionID {
  27. /** 1 */
  28. public static final int IF = FunctionMetadataRegistry.FUNCTION_INDEX_IF;
  29. /** 4 */
  30. public static final int SUM = FunctionMetadataRegistry.FUNCTION_INDEX_SUM;
  31. /** 78 */
  32. public static final int OFFSET = 78;
  33. /** 100 */
  34. public static final int CHOOSE = FunctionMetadataRegistry.FUNCTION_INDEX_CHOOSE;
  35. /** 148 */
  36. public static final int INDIRECT = FunctionMetadataRegistry.FUNCTION_INDEX_INDIRECT;
  37. /** 255 */
  38. public static final int EXTERNAL_FUNC = FunctionMetadataRegistry.FUNCTION_INDEX_EXTERNAL;
  39. }
  40. // convenient access to namespace
  41. private static final FunctionID ID = null;
  42. /**
  43. * Array elements corresponding to unimplemented functions are <code>null</code>
  44. */
  45. protected static final Function[] functions = produceFunctions();
  46. private static Function[] produceFunctions() {
  47. Function[] retval = new Function[368];
  48. retval[0] = new Count();
  49. retval[ID.IF] = new IfFunc();
  50. retval[2] = LogicalFunction.ISNA;
  51. retval[3] = LogicalFunction.ISERROR;
  52. retval[ID.SUM] = AggregateFunction.SUM;
  53. retval[5] = AggregateFunction.AVERAGE;
  54. retval[6] = AggregateFunction.MIN;
  55. retval[7] = AggregateFunction.MAX;
  56. retval[8] = new RowFunc(); // ROW
  57. retval[9] = new Column();
  58. retval[10] = new Na();
  59. retval[11] = new Npv();
  60. retval[12] = AggregateFunction.STDEV;
  61. retval[13] = NumericFunction.DOLLAR;
  62. retval[15] = NumericFunction.SIN;
  63. retval[16] = NumericFunction.COS;
  64. retval[17] = NumericFunction.TAN;
  65. retval[18] = NumericFunction.ATAN;
  66. retval[19] = NumericFunction.PI;
  67. retval[20] = NumericFunction.SQRT;
  68. retval[21] = NumericFunction.EXP;
  69. retval[22] = NumericFunction.LN;
  70. retval[23] = NumericFunction.LOG10;
  71. retval[24] = NumericFunction.ABS;
  72. retval[25] = NumericFunction.INT;
  73. retval[26] = NumericFunction.SIGN;
  74. retval[27] = NumericFunction.ROUND;
  75. retval[28] = new Lookup();
  76. retval[29] = new Index();
  77. retval[31] = TextFunction.MID;
  78. retval[32] = TextFunction.LEN;
  79. retval[33] = new Value();
  80. retval[34] = BooleanFunction.TRUE;
  81. retval[35] = BooleanFunction.FALSE;
  82. retval[36] = BooleanFunction.AND;
  83. retval[37] = BooleanFunction.OR;
  84. retval[38] = BooleanFunction.NOT;
  85. retval[39] = NumericFunction.MOD;
  86. retval[48] = TextFunction.TEXT;
  87. retval[56] = FinanceFunction.PV;
  88. retval[57] = FinanceFunction.FV;
  89. retval[58] = FinanceFunction.NPER;
  90. retval[59] = FinanceFunction.PMT;
  91. retval[62] = new Irr();
  92. retval[63] = NumericFunction.RAND;
  93. retval[64] = new Match();
  94. retval[65] = DateFunc.instance;
  95. retval[66] = new TimeFunc();
  96. retval[67] = CalendarFieldFunction.DAY;
  97. retval[68] = CalendarFieldFunction.MONTH;
  98. retval[69] = CalendarFieldFunction.YEAR;
  99. retval[74] = new Now();
  100. retval[76] = new Rows();
  101. retval[77] = new Columns();
  102. retval[82] = TextFunction.SEARCH;
  103. retval[ID.OFFSET] = new Offset();
  104. retval[82] = TextFunction.SEARCH;
  105. retval[97] = NumericFunction.ATAN2;
  106. retval[98] = NumericFunction.ASIN;
  107. retval[99] = NumericFunction.ACOS;
  108. retval[ID.CHOOSE] = new Choose();
  109. retval[101] = new Hlookup();
  110. retval[102] = new Vlookup();
  111. retval[105] = LogicalFunction.ISREF;
  112. retval[109] = NumericFunction.LOG;
  113. retval[112] = TextFunction.LOWER;
  114. retval[113] = TextFunction.UPPER;
  115. retval[115] = TextFunction.LEFT;
  116. retval[116] = TextFunction.RIGHT;
  117. retval[117] = TextFunction.EXACT;
  118. retval[118] = TextFunction.TRIM;
  119. retval[119] = new Replace();
  120. retval[120] = new Substitute();
  121. retval[124] = TextFunction.FIND;
  122. retval[127] = LogicalFunction.ISTEXT;
  123. retval[128] = LogicalFunction.ISNUMBER;
  124. retval[129] = LogicalFunction.ISBLANK;
  125. retval[130] = new T();
  126. retval[ID.INDIRECT] = null; // Indirect.evaluate has different signature
  127. retval[169] = new Counta();
  128. retval[183] = AggregateFunction.PRODUCT;
  129. retval[184] = NumericFunction.FACT;
  130. retval[190] = LogicalFunction.ISNONTEXT;
  131. retval[197] = NumericFunction.TRUNC;
  132. retval[198] = LogicalFunction.ISLOGICAL;
  133. retval[212] = NumericFunction.ROUNDUP;
  134. retval[213] = NumericFunction.ROUNDDOWN;
  135. retval[220] = new Days360();
  136. retval[221] = new Today();
  137. retval[227] = AggregateFunction.MEDIAN;
  138. retval[228] = new Sumproduct();
  139. retval[229] = NumericFunction.SINH;
  140. retval[230] = NumericFunction.COSH;
  141. retval[231] = NumericFunction.TANH;
  142. retval[232] = NumericFunction.ASINH;
  143. retval[233] = NumericFunction.ACOSH;
  144. retval[234] = NumericFunction.ATANH;
  145. retval[ID.EXTERNAL_FUNC] = null; // ExternalFunction is a FreeREfFunction
  146. retval[261] = new Errortype();
  147. retval[269] = AggregateFunction.AVEDEV;
  148. retval[276] = NumericFunction.COMBIN;
  149. retval[279] = new Even();
  150. retval[285] = NumericFunction.FLOOR;
  151. retval[288] = NumericFunction.CEILING;
  152. retval[298] = new Odd();
  153. retval[300] = NumericFunction.POISSON;
  154. retval[303] = new Sumxmy2();
  155. retval[304] = new Sumx2my2();
  156. retval[305] = new Sumx2py2();
  157. retval[318] = AggregateFunction.DEVSQ;
  158. retval[321] = AggregateFunction.SUMSQ;
  159. retval[325] = AggregateFunction.LARGE;
  160. retval[326] = AggregateFunction.SMALL;
  161. retval[330] = new Mode();
  162. retval[336] = TextFunction.CONCATENATE;
  163. retval[337] = NumericFunction.POWER;
  164. retval[342] = NumericFunction.RADIANS;
  165. retval[343] = NumericFunction.DEGREES;
  166. retval[344] = new Subtotal();
  167. retval[345] = new Sumif();
  168. retval[346] = new Countif();
  169. retval[347] = new Countblank();
  170. retval[359] = new Hyperlink();
  171. retval[362] = MinaMaxa.MAXA;
  172. retval[363] = MinaMaxa.MINA;
  173. for (int i = 0; i < retval.length; i++) {
  174. Function f = retval[i];
  175. if (f == null) {
  176. FunctionMetadata fm = FunctionMetadataRegistry.getFunctionByIndex(i);
  177. if (fm == null) {
  178. continue;
  179. }
  180. retval[i] = new NotImplementedFunction(fm.getName());
  181. }
  182. }
  183. return retval;
  184. }
  185. /**
  186. * @return <code>null</code> if the specified functionIndex is for INDIRECT() or any external (add-in) function.
  187. */
  188. public static Function getBasicFunction(int functionIndex) {
  189. // check for 'free ref' functions first
  190. switch (functionIndex) {
  191. case FunctionID.INDIRECT:
  192. case FunctionID.EXTERNAL_FUNC:
  193. return null;
  194. }
  195. // else - must be plain function
  196. Function result = functions[functionIndex];
  197. if (result == null) {
  198. throw new NotImplementedException("FuncIx=" + functionIndex);
  199. }
  200. return result;
  201. }
  202. }