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.

FunctionEval.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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.atp.AnalysisToolPak;
  17. import org.apache.poi.ss.formula.function.FunctionMetadata;
  18. import org.apache.poi.ss.formula.function.FunctionMetadataRegistry;
  19. import org.apache.poi.ss.formula.functions.*;
  20. import java.util.Collection;
  21. import java.util.Collections;
  22. import java.util.TreeSet;
  23. /**
  24. * @author Amol S. Deshmukh < amolweb at ya hoo dot com >
  25. * @author Johan Karlsteen - added Intercept and Slope
  26. */
  27. public final class FunctionEval {
  28. /**
  29. * Some function IDs that require special treatment
  30. */
  31. private static final class FunctionID {
  32. /** 1 */
  33. public static final int IF = FunctionMetadataRegistry.FUNCTION_INDEX_IF;
  34. /** 4 */
  35. public static final int SUM = FunctionMetadataRegistry.FUNCTION_INDEX_SUM;
  36. /** 78 */
  37. public static final int OFFSET = 78;
  38. /** 100 */
  39. public static final int CHOOSE = FunctionMetadataRegistry.FUNCTION_INDEX_CHOOSE;
  40. /** 148 */
  41. public static final int INDIRECT = FunctionMetadataRegistry.FUNCTION_INDEX_INDIRECT;
  42. /** 255 */
  43. public static final int EXTERNAL_FUNC = FunctionMetadataRegistry.FUNCTION_INDEX_EXTERNAL;
  44. }
  45. // convenient access to namespace
  46. private static final FunctionID ID = null;
  47. /**
  48. * Array elements corresponding to unimplemented functions are <code>null</code>
  49. */
  50. protected static final Function[] functions = produceFunctions();
  51. private static Function[] produceFunctions() {
  52. Function[] retval = new Function[368];
  53. retval[0] = new Count();
  54. retval[ID.IF] = new IfFunc();
  55. retval[2] = LogicalFunction.ISNA;
  56. retval[3] = LogicalFunction.ISERROR;
  57. retval[ID.SUM] = AggregateFunction.SUM;
  58. retval[5] = AggregateFunction.AVERAGE;
  59. retval[6] = AggregateFunction.MIN;
  60. retval[7] = AggregateFunction.MAX;
  61. retval[8] = new RowFunc(); // ROW
  62. retval[9] = new Column();
  63. retval[10] = new Na();
  64. retval[11] = new Npv();
  65. retval[12] = AggregateFunction.STDEV;
  66. retval[13] = NumericFunction.DOLLAR;
  67. retval[15] = NumericFunction.SIN;
  68. retval[16] = NumericFunction.COS;
  69. retval[17] = NumericFunction.TAN;
  70. retval[18] = NumericFunction.ATAN;
  71. retval[19] = NumericFunction.PI;
  72. retval[20] = NumericFunction.SQRT;
  73. retval[21] = NumericFunction.EXP;
  74. retval[22] = NumericFunction.LN;
  75. retval[23] = NumericFunction.LOG10;
  76. retval[24] = NumericFunction.ABS;
  77. retval[25] = NumericFunction.INT;
  78. retval[26] = NumericFunction.SIGN;
  79. retval[27] = NumericFunction.ROUND;
  80. retval[28] = new Lookup();
  81. retval[29] = new Index();
  82. retval[30] = new Rept();
  83. retval[31] = TextFunction.MID;
  84. retval[32] = TextFunction.LEN;
  85. retval[33] = new Value();
  86. retval[34] = BooleanFunction.TRUE;
  87. retval[35] = BooleanFunction.FALSE;
  88. retval[36] = BooleanFunction.AND;
  89. retval[37] = BooleanFunction.OR;
  90. retval[38] = BooleanFunction.NOT;
  91. retval[39] = NumericFunction.MOD;
  92. retval[46] = AggregateFunction.VAR;
  93. retval[48] = TextFunction.TEXT;
  94. retval[56] = FinanceFunction.PV;
  95. retval[57] = FinanceFunction.FV;
  96. retval[58] = FinanceFunction.NPER;
  97. retval[59] = FinanceFunction.PMT;
  98. retval[60] = new Rate();
  99. retval[61] = new Mirr();
  100. retval[62] = new Irr();
  101. retval[63] = NumericFunction.RAND;
  102. retval[64] = new Match();
  103. retval[65] = DateFunc.instance;
  104. retval[66] = new TimeFunc();
  105. retval[67] = CalendarFieldFunction.DAY;
  106. retval[68] = CalendarFieldFunction.MONTH;
  107. retval[69] = CalendarFieldFunction.YEAR;
  108. retval[70] = WeekdayFunc.instance;
  109. retval[71] = CalendarFieldFunction.HOUR;
  110. retval[72] = CalendarFieldFunction.MINUTE;
  111. retval[73] = CalendarFieldFunction.SECOND;
  112. retval[74] = new Now();
  113. retval[76] = new Rows();
  114. retval[77] = new Columns();
  115. retval[82] = TextFunction.SEARCH;
  116. retval[ID.OFFSET] = new Offset();
  117. retval[82] = TextFunction.SEARCH;
  118. retval[97] = NumericFunction.ATAN2;
  119. retval[98] = NumericFunction.ASIN;
  120. retval[99] = NumericFunction.ACOS;
  121. retval[ID.CHOOSE] = new Choose();
  122. retval[101] = new Hlookup();
  123. retval[102] = new Vlookup();
  124. retval[105] = LogicalFunction.ISREF;
  125. retval[109] = NumericFunction.LOG;
  126. retval[111] = TextFunction.CHAR;
  127. retval[112] = TextFunction.LOWER;
  128. retval[113] = TextFunction.UPPER;
  129. retval[115] = TextFunction.LEFT;
  130. retval[116] = TextFunction.RIGHT;
  131. retval[117] = TextFunction.EXACT;
  132. retval[118] = TextFunction.TRIM;
  133. retval[119] = new Replace();
  134. retval[120] = new Substitute();
  135. retval[121] = new Code();
  136. retval[124] = TextFunction.FIND;
  137. retval[127] = LogicalFunction.ISTEXT;
  138. retval[128] = LogicalFunction.ISNUMBER;
  139. retval[129] = LogicalFunction.ISBLANK;
  140. retval[130] = new T();
  141. retval[ID.INDIRECT] = null; // Indirect.evaluate has different signature
  142. retval[162] = TextFunction.CLEAN; //Aniket Banerjee
  143. retval[167] = new IPMT();
  144. retval[168] = new PPMT();
  145. retval[169] = new Counta();
  146. retval[183] = AggregateFunction.PRODUCT;
  147. retval[184] = NumericFunction.FACT;
  148. retval[190] = LogicalFunction.ISNONTEXT;
  149. retval[194] = AggregateFunction.VARP;
  150. retval[197] = NumericFunction.TRUNC;
  151. retval[198] = LogicalFunction.ISLOGICAL;
  152. retval[212] = NumericFunction.ROUNDUP;
  153. retval[213] = NumericFunction.ROUNDDOWN;
  154. retval[216] = new Rank();
  155. retval[219] = new Address(); //Aniket Banerjee
  156. retval[220] = new Days360();
  157. retval[221] = new Today();
  158. retval[227] = AggregateFunction.MEDIAN;
  159. retval[228] = new Sumproduct();
  160. retval[229] = NumericFunction.SINH;
  161. retval[230] = NumericFunction.COSH;
  162. retval[231] = NumericFunction.TANH;
  163. retval[232] = NumericFunction.ASINH;
  164. retval[233] = NumericFunction.ACOSH;
  165. retval[234] = NumericFunction.ATANH;
  166. retval[ID.EXTERNAL_FUNC] = null; // ExternalFunction is a FreeREfFunction
  167. retval[261] = new Errortype();
  168. retval[269] = AggregateFunction.AVEDEV;
  169. retval[276] = NumericFunction.COMBIN;
  170. retval[279] = new Even();
  171. retval[285] = NumericFunction.FLOOR;
  172. retval[288] = NumericFunction.CEILING;
  173. retval[298] = new Odd();
  174. retval[300] = NumericFunction.POISSON;
  175. retval[303] = new Sumxmy2();
  176. retval[304] = new Sumx2my2();
  177. retval[305] = new Sumx2py2();
  178. retval[311] = new Intercept();
  179. retval[315] = new Slope();
  180. retval[318] = AggregateFunction.DEVSQ;
  181. retval[321] = AggregateFunction.SUMSQ;
  182. retval[325] = AggregateFunction.LARGE;
  183. retval[326] = AggregateFunction.SMALL;
  184. retval[330] = new Mode();
  185. retval[336] = TextFunction.CONCATENATE;
  186. retval[337] = NumericFunction.POWER;
  187. retval[342] = NumericFunction.RADIANS;
  188. retval[343] = NumericFunction.DEGREES;
  189. retval[344] = new Subtotal();
  190. retval[345] = new Sumif();
  191. retval[346] = new Countif();
  192. retval[347] = new Countblank();
  193. retval[354] = new Roman();
  194. retval[359] = new Hyperlink();
  195. retval[362] = MinaMaxa.MAXA;
  196. retval[363] = MinaMaxa.MINA;
  197. for (int i = 0; i < retval.length; i++) {
  198. Function f = retval[i];
  199. if (f == null) {
  200. FunctionMetadata fm = FunctionMetadataRegistry.getFunctionByIndex(i);
  201. if (fm == null) {
  202. continue;
  203. }
  204. retval[i] = new NotImplementedFunction(fm.getName());
  205. }
  206. }
  207. return retval;
  208. }
  209. /**
  210. * @return <code>null</code> if the specified functionIndex is for INDIRECT() or any external (add-in) function.
  211. */
  212. public static Function getBasicFunction(int functionIndex) {
  213. // check for 'free ref' functions first
  214. switch (functionIndex) {
  215. case FunctionID.INDIRECT:
  216. case FunctionID.EXTERNAL_FUNC:
  217. return null;
  218. }
  219. // else - must be plain function
  220. Function result = functions[functionIndex];
  221. if (result == null) {
  222. throw new NotImplementedException("FuncIx=" + functionIndex);
  223. }
  224. return result;
  225. }
  226. /**
  227. * Register a new function in runtime.
  228. *
  229. * @param name the function name
  230. * @param func the functoin to register
  231. * @throws IllegalArgumentException if the function is unknown or already registered.
  232. * @since 3.8 beta6
  233. */
  234. public static void registerFunction(String name, Function func){
  235. FunctionMetadata metaData = FunctionMetadataRegistry.getFunctionByName(name);
  236. if(metaData == null) {
  237. if(AnalysisToolPak.isATPFunction(name)) {
  238. throw new IllegalArgumentException(name + " is a function from the Excel Analysis Toolpack. " +
  239. "Use AnalysisToolpack.registerFunction(String name, FreeRefFunction func) instead.");
  240. } else {
  241. throw new IllegalArgumentException("Unknown function: " + name);
  242. }
  243. }
  244. int idx = metaData.getIndex();
  245. if(functions[idx] instanceof NotImplementedFunction) {
  246. functions[idx] = func;
  247. } else {
  248. throw new IllegalArgumentException("POI already implememts " + name +
  249. ". You cannot override POI's implementations of Excel functions");
  250. }
  251. }
  252. /**
  253. * Returns a collection of function names implemented by POI.
  254. *
  255. * @return an array of supported functions
  256. * @since 3.8 beta6
  257. */
  258. public static Collection<String> getSupportedFunctionNames(){
  259. Collection<String> lst = new TreeSet<String>();
  260. for(int i = 0; i < functions.length; i++){
  261. Function func = functions[i];
  262. FunctionMetadata metaData = FunctionMetadataRegistry.getFunctionByIndex(i);
  263. if(func != null && !(func instanceof NotImplementedFunction)){
  264. lst.add(metaData.getName());
  265. }
  266. }
  267. lst.add("INDIRECT"); // INDIRECT is a special case
  268. return Collections.unmodifiableCollection(lst);
  269. }
  270. /**
  271. * Returns an array of function names NOT implemented by POI.
  272. *
  273. * @return an array of not supported functions
  274. * @since 3.8 beta6
  275. */
  276. public static Collection<String> getNotSupportedFunctionNames(){
  277. Collection<String> lst = new TreeSet<String>();
  278. for(int i = 0; i < functions.length; i++){
  279. Function func = functions[i];
  280. if(func != null && (func instanceof NotImplementedFunction)){
  281. FunctionMetadata metaData = FunctionMetadataRegistry.getFunctionByIndex(i);
  282. lst.add(metaData.getName());
  283. }
  284. }
  285. lst.remove("INDIRECT"); // INDIRECT is a special case
  286. return Collections.unmodifiableCollection(lst);
  287. }
  288. }