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 15KB

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