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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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.*;
  23. /**
  24. * Mappings from the Excel functions to our evaluation implementations
  25. * (where available)
  26. */
  27. public final class FunctionEval {
  28. private FunctionEval() {
  29. // no instances of this class
  30. }
  31. /**
  32. * Some function IDs that require special treatment
  33. */
  34. private static final class FunctionID {
  35. /** 1 */
  36. public static final int IF = FunctionMetadataRegistry.FUNCTION_INDEX_IF;
  37. /** 4 */
  38. public static final int SUM = FunctionMetadataRegistry.FUNCTION_INDEX_SUM;
  39. /** 78 */
  40. public static final int OFFSET = 78;
  41. /** 100 */
  42. public static final int CHOOSE = FunctionMetadataRegistry.FUNCTION_INDEX_CHOOSE;
  43. /** 148 */
  44. public static final int INDIRECT = FunctionMetadataRegistry.FUNCTION_INDEX_INDIRECT;
  45. /** 255 */
  46. public static final int EXTERNAL_FUNC = FunctionMetadataRegistry.FUNCTION_INDEX_EXTERNAL;
  47. }
  48. /**
  49. * Array elements corresponding to unimplemented functions are <code>null</code>
  50. */
  51. protected static final Function[] functions = produceFunctions();
  52. /**
  53. * See <a href="https://www.openoffice.org/sc/excelfileformat.pdf">Apache Open Office Excel File Format,
  54. * Section 3.11 Built-In Sheet Functions</a>
  55. */
  56. private static Function[] produceFunctions() {
  57. Function[] retval = new Function[368];
  58. retval[0] = new Count();
  59. retval[FunctionID.IF] = new IfFunc(); //nominally 1
  60. retval[2] = LogicalFunction.ISNA;
  61. retval[3] = LogicalFunction.ISERROR;
  62. retval[FunctionID.SUM] = AggregateFunction.SUM; //nominally 4
  63. retval[5] = AggregateFunction.AVERAGE;
  64. retval[6] = AggregateFunction.MIN;
  65. retval[7] = AggregateFunction.MAX;
  66. retval[8] = new RowFunc(); // ROW
  67. retval[9] = new Column();
  68. retval[10] = new Na();
  69. retval[11] = new Npv();
  70. retval[12] = AggregateFunction.STDEV;
  71. retval[13] = NumericFunction.DOLLAR;
  72. retval[14] = new Fixed();
  73. retval[15] = NumericFunction.SIN;
  74. retval[16] = NumericFunction.COS;
  75. retval[17] = NumericFunction.TAN;
  76. retval[18] = NumericFunction.ATAN;
  77. retval[19] = NumericFunction.PI;
  78. retval[20] = NumericFunction.SQRT;
  79. retval[21] = NumericFunction.EXP;
  80. retval[22] = NumericFunction.LN;
  81. retval[23] = NumericFunction.LOG10;
  82. retval[24] = NumericFunction.ABS;
  83. retval[25] = NumericFunction.INT;
  84. retval[26] = NumericFunction.SIGN;
  85. retval[27] = NumericFunction.ROUND;
  86. retval[28] = new Lookup();
  87. retval[29] = new Index();
  88. retval[30] = new Rept();
  89. retval[31] = TextFunction.MID;
  90. retval[32] = TextFunction.LEN;
  91. retval[33] = new Value();
  92. retval[34] = BooleanFunction.TRUE;
  93. retval[35] = BooleanFunction.FALSE;
  94. retval[36] = BooleanFunction.AND;
  95. retval[37] = BooleanFunction.OR;
  96. retval[38] = BooleanFunction.NOT;
  97. retval[39] = NumericFunction.MOD;
  98. // 40: DCOUNT
  99. // 41: DSUM
  100. // 42: DAVERAGE
  101. retval[43] = new DStarRunner(DStarRunner.DStarAlgorithmEnum.DMIN);
  102. // 44: DMAX
  103. // 45: DSTDEV
  104. retval[46] = AggregateFunction.VAR;
  105. // 47: DVAR
  106. retval[48] = TextFunction.TEXT;
  107. // 49: LINEST
  108. // 50: TREND
  109. // 51: LOGEST
  110. // 52: GROWTH
  111. retval[56] = FinanceFunction.PV;
  112. retval[57] = FinanceFunction.FV;
  113. retval[58] = FinanceFunction.NPER;
  114. retval[59] = FinanceFunction.PMT;
  115. retval[60] = new Rate();
  116. retval[61] = new Mirr();
  117. retval[62] = new Irr();
  118. retval[63] = NumericFunction.RAND;
  119. retval[64] = new Match();
  120. retval[65] = DateFunc.instance;
  121. retval[66] = new TimeFunc();
  122. retval[67] = CalendarFieldFunction.DAY;
  123. retval[68] = CalendarFieldFunction.MONTH;
  124. retval[69] = CalendarFieldFunction.YEAR;
  125. retval[70] = WeekdayFunc.instance;
  126. retval[71] = CalendarFieldFunction.HOUR;
  127. retval[72] = CalendarFieldFunction.MINUTE;
  128. retval[73] = CalendarFieldFunction.SECOND;
  129. retval[74] = new Now();
  130. // 75: AREAS
  131. retval[76] = new Rows();
  132. retval[77] = new Columns();
  133. retval[FunctionID.OFFSET] = new Offset(); //nominally 78
  134. retval[82] = TextFunction.SEARCH;
  135. // 83: TRANSPOSE
  136. retval[83] = MatrixFunction.TRANSPOSE;
  137. // 86: TYPE
  138. retval[97] = NumericFunction.ATAN2;
  139. retval[98] = NumericFunction.ASIN;
  140. retval[99] = NumericFunction.ACOS;
  141. retval[FunctionID.CHOOSE] = new Choose(); //nominally 100
  142. retval[101] = new Hlookup();
  143. retval[102] = new Vlookup();
  144. retval[105] = LogicalFunction.ISREF;
  145. retval[109] = NumericFunction.LOG;
  146. retval[111] = TextFunction.CHAR;
  147. retval[112] = TextFunction.LOWER;
  148. retval[113] = TextFunction.UPPER;
  149. retval[114] = TextFunction.PROPER;
  150. retval[115] = TextFunction.LEFT;
  151. retval[116] = TextFunction.RIGHT;
  152. retval[117] = TextFunction.EXACT;
  153. retval[118] = TextFunction.TRIM;
  154. retval[119] = new Replace();
  155. retval[120] = new Substitute();
  156. retval[121] = new Code();
  157. retval[124] = TextFunction.FIND;
  158. retval[126] = LogicalFunction.ISERR;
  159. retval[127] = LogicalFunction.ISTEXT;
  160. retval[128] = LogicalFunction.ISNUMBER;
  161. retval[129] = LogicalFunction.ISBLANK;
  162. retval[130] = new T();
  163. retval[FunctionID.INDIRECT] = null; // Indirect.evaluate has different signature
  164. retval[162] = TextFunction.CLEAN;
  165. retval[163] = MatrixFunction.MDETERM;
  166. retval[164] = MatrixFunction.MINVERSE;
  167. retval[165] = MatrixFunction.MMULT;
  168. retval[167] = new IPMT();
  169. retval[168] = new PPMT();
  170. retval[169] = new Counta();
  171. retval[183] = AggregateFunction.PRODUCT;
  172. retval[184] = NumericFunction.FACT;
  173. retval[190] = LogicalFunction.ISNONTEXT;
  174. retval[194] = AggregateFunction.VARP;
  175. retval[197] = NumericFunction.TRUNC;
  176. retval[198] = LogicalFunction.ISLOGICAL;
  177. //204: USDOLLAR (YEN in BIFF3)
  178. //205: FINDB
  179. //206: SEARCHB
  180. //207: REPLACEB
  181. //208: LEFTB
  182. //209: RIGHTB
  183. //210: MIDB
  184. //211: LENB
  185. retval[212] = NumericFunction.ROUNDUP;
  186. retval[213] = NumericFunction.ROUNDDOWN;
  187. //214: ASC
  188. //215: DBCS (JIS in BIFF3)
  189. retval[216] = new Rank();
  190. retval[219] = new Address();
  191. retval[220] = new Days360();
  192. retval[221] = new Today();
  193. //222: VBD
  194. retval[227] = AggregateFunction.MEDIAN;
  195. retval[228] = new Sumproduct();
  196. retval[229] = NumericFunction.SINH;
  197. retval[230] = NumericFunction.COSH;
  198. retval[231] = NumericFunction.TANH;
  199. retval[232] = NumericFunction.ASINH;
  200. retval[233] = NumericFunction.ACOSH;
  201. retval[234] = NumericFunction.ATANH;
  202. retval[235] = new DStarRunner(DStarRunner.DStarAlgorithmEnum.DGET);
  203. // 244: INFO
  204. // 247: DB
  205. retval[FunctionID.EXTERNAL_FUNC] = null; // ExternalFunction is a FreeRefFunction, nominally 255
  206. retval[261] = new Errortype();
  207. retval[269] = AggregateFunction.AVEDEV;
  208. // 270: BETADIST
  209. // 271: GAMMALN
  210. // 272: BETAINV
  211. // 273: BINOMDIST
  212. // 274: CHIDIST
  213. // 275: CHIINV
  214. retval[276] = NumericFunction.COMBIN;
  215. // 277: CONFIDENCE
  216. // 278:CRITBINOM
  217. retval[279] = new Even();
  218. // 280: EXPONDIST
  219. // 281: FDIST
  220. // 282: FINV
  221. // 283: FISHER
  222. // 284: FISHERINV
  223. retval[285] = NumericFunction.FLOOR;
  224. // 286: GAMMADIST
  225. // 287: GAMMAINV
  226. retval[288] = NumericFunction.CEILING;
  227. // 289: HYPGEOMDIST
  228. // 290: LOGNORMDIST
  229. // 291: LOGINV
  230. // 292: NEGBINOMDIST
  231. // 293: NORMDIST
  232. // 294: NORMSDIST
  233. // 295: NORMINV
  234. // 296: NORMSINV
  235. // 297: STANDARDIZE
  236. retval[298] = new Odd();
  237. // 299: PERMUT
  238. retval[300] = NumericFunction.POISSON;
  239. // 301: TDIST
  240. // 302: WEIBULL
  241. retval[303] = new Sumxmy2();
  242. retval[304] = new Sumx2my2();
  243. retval[305] = new Sumx2py2();
  244. // 306: CHITEST
  245. // 307: CORREL
  246. // 308: COVAR
  247. // 309: FORECAST
  248. // 310: FTEST
  249. retval[311] = new Intercept();
  250. // 312: PEARSON
  251. // 313: RSQ
  252. // 314: STEYX
  253. retval[315] = new Slope();
  254. // 316: TTEST
  255. // 317: PROB
  256. retval[318] = AggregateFunction.DEVSQ;
  257. // 319: GEOMEAN
  258. // 320: HARMEAN
  259. retval[321] = AggregateFunction.SUMSQ;
  260. // 322: KURT
  261. // 323: SKEW
  262. // 324: ZTEST
  263. retval[325] = AggregateFunction.LARGE;
  264. retval[326] = AggregateFunction.SMALL;
  265. // 327: QUARTILE
  266. retval[328] = AggregateFunction.PERCENTILE;
  267. // 329: PERCENTRANK
  268. retval[330] = new Mode();
  269. // 331: TRIMMEAN
  270. // 332: TINV
  271. retval[336] = TextFunction.CONCATENATE;
  272. retval[337] = NumericFunction.POWER;
  273. retval[342] = NumericFunction.RADIANS;
  274. retval[343] = NumericFunction.DEGREES;
  275. retval[344] = new Subtotal();
  276. retval[345] = new Sumif();
  277. retval[346] = new Countif();
  278. retval[347] = new Countblank();
  279. // 350: ISPMT
  280. // 351: DATEDIF
  281. // 352: DATESTRING
  282. // 353: NUMBERSTRING
  283. retval[354] = new Roman();
  284. // 358: GETPIVOTDATA
  285. retval[359] = new Hyperlink();
  286. // 360: PHONETIC
  287. // 361: AVERAGEA
  288. retval[362] = MinaMaxa.MAXA;
  289. retval[363] = MinaMaxa.MINA;
  290. // 364: STDEVPA
  291. // 365: VARPA
  292. // 366: STDEVA
  293. // 367: VARA
  294. for (int i = 0; i < retval.length; i++) {
  295. Function f = retval[i];
  296. if (f == null) {
  297. FunctionMetadata fm = FunctionMetadataRegistry.getFunctionByIndex(i);
  298. if (fm == null) {
  299. continue;
  300. }
  301. retval[i] = new NotImplementedFunction(fm.getName());
  302. }
  303. }
  304. return retval;
  305. }
  306. /**
  307. * @return <code>null</code> if the specified functionIndex is for INDIRECT() or any external (add-in) function.
  308. */
  309. public static Function getBasicFunction(int functionIndex) {
  310. // check for 'free ref' functions first
  311. switch (functionIndex) {
  312. case FunctionID.INDIRECT:
  313. case FunctionID.EXTERNAL_FUNC:
  314. return null;
  315. }
  316. // else - must be plain function
  317. Function result = functions[functionIndex];
  318. if (result == null) {
  319. throw new NotImplementedException("FuncIx=" + functionIndex);
  320. }
  321. return result;
  322. }
  323. /**
  324. * Register a new function in runtime.
  325. *
  326. * @param name the function name
  327. * @param func the functoin to register
  328. * @throws IllegalArgumentException if the function is unknown or already registered.
  329. * @since 3.8 beta6
  330. */
  331. public static void registerFunction(String name, Function func){
  332. FunctionMetadata metaData = FunctionMetadataRegistry.getFunctionByName(name);
  333. if(metaData == null) {
  334. if(AnalysisToolPak.isATPFunction(name)) {
  335. throw new IllegalArgumentException(name + " is a function from the Excel Analysis Toolpack. " +
  336. "Use AnalysisToolpack.registerFunction(String name, FreeRefFunction func) instead.");
  337. }
  338. throw new IllegalArgumentException("Unknown function: " + name);
  339. }
  340. int idx = metaData.getIndex();
  341. if(functions[idx] instanceof NotImplementedFunction) {
  342. functions[idx] = func;
  343. } else {
  344. throw new IllegalArgumentException("POI already implememts " + name +
  345. ". You cannot override POI's implementations of Excel functions");
  346. }
  347. }
  348. /**
  349. * Returns a collection of function names implemented by POI.
  350. *
  351. * @return an array of supported functions
  352. * @since 3.8 beta6
  353. */
  354. public static Collection<String> getSupportedFunctionNames() {
  355. Collection<String> lst = new TreeSet<String>();
  356. for (int i = 0; i < functions.length; i++) {
  357. Function func = functions[i];
  358. FunctionMetadata metaData = FunctionMetadataRegistry.getFunctionByIndex(i);
  359. if (func != null && !(func instanceof NotImplementedFunction)) {
  360. lst.add(metaData.getName());
  361. }
  362. }
  363. lst.add("INDIRECT"); // INDIRECT is a special case
  364. return Collections.unmodifiableCollection(lst);
  365. }
  366. /**
  367. * Returns an array of function names NOT implemented by POI.
  368. *
  369. * @return an array of not supported functions
  370. * @since 3.8 beta6
  371. */
  372. public static Collection<String> getNotSupportedFunctionNames() {
  373. Collection<String> lst = new TreeSet<String>();
  374. for (int i = 0; i < functions.length; i++) {
  375. Function func = functions[i];
  376. if (func != null && (func instanceof NotImplementedFunction)) {
  377. FunctionMetadata metaData = FunctionMetadataRegistry.getFunctionByIndex(i);
  378. lst.add(metaData.getName());
  379. }
  380. }
  381. lst.remove("INDIRECT"); // INDIRECT is a special case
  382. return Collections.unmodifiableCollection(lst);
  383. }
  384. }