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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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] = RowFunc::evaluate; // ROW
  67. retval[9] = Column::evaluate;
  68. retval[10] = Na::evaluate;
  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. retval[41] = new DStarRunner(DStarRunner.DStarAlgorithmEnum.DSUM);
  100. // 42: DAVERAGE
  101. retval[43] = new DStarRunner(DStarRunner.DStarAlgorithmEnum.DMIN);
  102. retval[44] = new DStarRunner(DStarRunner.DStarAlgorithmEnum.DMAX);
  103. // 45: DSTDEV
  104. retval[46] = AggregateFunction.VAR;
  105. // 47: DVAR
  106. retval[48] = TextFunction.TEXT;
  107. // 49: LINEST
  108. retval[50] = new 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] = Now::evaluate;
  130. retval[75] = new 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. retval[83] = MatrixFunction.TRANSPOSE;
  136. // 86: TYPE
  137. retval[97] = NumericFunction.ATAN2;
  138. retval[98] = NumericFunction.ASIN;
  139. retval[99] = NumericFunction.ACOS;
  140. retval[FunctionID.CHOOSE] = new Choose(); //nominally 100
  141. retval[101] = new Hlookup();
  142. retval[102] = new Vlookup();
  143. retval[105] = LogicalFunction.ISREF;
  144. retval[109] = NumericFunction.LOG;
  145. retval[111] = TextFunction.CHAR;
  146. retval[112] = TextFunction.LOWER;
  147. retval[113] = TextFunction.UPPER;
  148. retval[114] = TextFunction.PROPER;
  149. retval[115] = TextFunction.LEFT;
  150. retval[116] = TextFunction.RIGHT;
  151. retval[117] = TextFunction.EXACT;
  152. retval[118] = TextFunction.TRIM;
  153. retval[119] = new Replace();
  154. retval[120] = new Substitute();
  155. retval[121] = new Code();
  156. retval[124] = TextFunction.FIND;
  157. // 125: CELL
  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. // 131: N
  164. retval[140] = new DateValue();
  165. // 141: TIMEVALUE
  166. // 142: SLN
  167. // 143: SYD
  168. // 144: DDB
  169. retval[FunctionID.INDIRECT] = null; // Indirect.evaluate has different signature
  170. retval[162] = TextFunction.CLEAN;
  171. retval[163] = MatrixFunction.MDETERM;
  172. retval[164] = MatrixFunction.MINVERSE;
  173. retval[165] = MatrixFunction.MMULT;
  174. retval[167] = new IPMT();
  175. retval[168] = new PPMT();
  176. retval[169] = new Counta();
  177. retval[183] = AggregateFunction.PRODUCT;
  178. retval[184] = NumericFunction.FACT;
  179. // 189: DPRODUCT
  180. retval[190] = LogicalFunction.ISNONTEXT;
  181. retval[194] = AggregateFunction.VARP;
  182. // 195: DSTDEVP
  183. // 196: DVARP
  184. retval[197] = NumericFunction.TRUNC;
  185. retval[198] = LogicalFunction.ISLOGICAL;
  186. // 199: DCOUNTA
  187. //204: USDOLLAR (YEN in BIFF3)
  188. //205: FINDB
  189. //206: SEARCHB
  190. //207: REPLACEB
  191. //208: LEFTB
  192. //209: RIGHTB
  193. //210: MIDB
  194. //211: LENB
  195. retval[212] = NumericFunction.ROUNDUP;
  196. retval[213] = NumericFunction.ROUNDDOWN;
  197. //214: ASC
  198. //215: DBCS (JIS in BIFF3)
  199. retval[216] = new Rank();
  200. retval[219] = new Address();
  201. retval[220] = new Days360();
  202. retval[221] = Today::evaluate;
  203. //222: VBD
  204. retval[227] = AggregateFunction.MEDIAN;
  205. retval[228] = new Sumproduct();
  206. retval[229] = NumericFunction.SINH;
  207. retval[230] = NumericFunction.COSH;
  208. retval[231] = NumericFunction.TANH;
  209. retval[232] = NumericFunction.ASINH;
  210. retval[233] = NumericFunction.ACOSH;
  211. retval[234] = NumericFunction.ATANH;
  212. retval[235] = new DStarRunner(DStarRunner.DStarAlgorithmEnum.DGET);
  213. // 244: INFO
  214. // 247: DB
  215. // 252: FEQUENCY
  216. retval[252] = Frequency.instance;
  217. retval[FunctionID.EXTERNAL_FUNC] = null; // ExternalFunction is a FreeRefFunction, nominally 255
  218. retval[261] = new Errortype();
  219. retval[269] = AggregateFunction.AVEDEV;
  220. // 270: BETADIST
  221. // 271: GAMMALN
  222. // 272: BETAINV
  223. // 273: BINOMDIST
  224. // 274: CHIDIST
  225. // 275: CHIINV
  226. retval[276] = NumericFunction.COMBIN;
  227. // 277: CONFIDENCE
  228. // 278:CRITBINOM
  229. retval[279] = NumericFunction.EVEN;
  230. // 280: EXPONDIST
  231. // 281: FDIST
  232. // 282: FINV
  233. // 283: FISHER
  234. // 284: FISHERINV
  235. retval[285] = NumericFunction.FLOOR;
  236. // 286: GAMMADIST
  237. // 287: GAMMAINV
  238. retval[288] = NumericFunction.CEILING;
  239. // 289: HYPGEOMDIST
  240. // 290: LOGNORMDIST
  241. // 291: LOGINV
  242. // 292: NEGBINOMDIST
  243. // 293: NORMDIST
  244. // 294: NORMSDIST
  245. // 295: NORMINV
  246. // 296: NORMSINV
  247. // 297: STANDARDIZE
  248. retval[298] = NumericFunction.ODD;
  249. // 299: PERMUT
  250. retval[300] = NumericFunction.POISSON;
  251. // 301: TDIST
  252. // 302: WEIBULL
  253. retval[303] = new Sumxmy2();
  254. retval[304] = new Sumx2my2();
  255. retval[305] = new Sumx2py2();
  256. // 306: CHITEST
  257. // 307: CORREL
  258. // 308: COVAR
  259. // 309: FORECAST
  260. // 310: FTEST
  261. retval[311] = new Intercept();
  262. // 312: PEARSON
  263. // 313: RSQ
  264. // 314: STEYX
  265. retval[315] = new Slope();
  266. // 316: TTEST
  267. // 317: PROB
  268. retval[318] = AggregateFunction.DEVSQ;
  269. retval[319] = AggregateFunction.GEOMEAN;
  270. // 320: HARMEAN
  271. retval[321] = AggregateFunction.SUMSQ;
  272. // 322: KURT
  273. // 323: SKEW
  274. // 324: ZTEST
  275. retval[325] = AggregateFunction.LARGE;
  276. retval[326] = AggregateFunction.SMALL;
  277. // 327: QUARTILE
  278. retval[328] = AggregateFunction.PERCENTILE;
  279. // 329: PERCENTRANK
  280. retval[330] = new Mode();
  281. // 331: TRIMMEAN
  282. // 332: TINV
  283. retval[336] = TextFunction.CONCATENATE;
  284. retval[337] = NumericFunction.POWER;
  285. retval[342] = NumericFunction.RADIANS;
  286. retval[343] = NumericFunction.DEGREES;
  287. retval[344] = new Subtotal();
  288. retval[345] = new Sumif();
  289. retval[346] = new Countif();
  290. retval[347] = new Countblank();
  291. // 350: ISPMT
  292. // 351: DATEDIF
  293. // 352: DATESTRING
  294. // 353: NUMBERSTRING
  295. retval[354] = new Roman();
  296. // 358: GETPIVOTDATA
  297. retval[359] = new Hyperlink();
  298. // 360: PHONETIC
  299. // 361: AVERAGEA
  300. retval[362] = MinaMaxa.MAXA;
  301. retval[363] = MinaMaxa.MINA;
  302. // 364: STDEVPA
  303. // 365: VARPA
  304. // 366: STDEVA
  305. // 367: VARA
  306. for (int i = 0; i < retval.length; i++) {
  307. Function f = retval[i];
  308. if (f == null) {
  309. FunctionMetadata fm = FunctionMetadataRegistry.getFunctionByIndex(i);
  310. if (fm == null) {
  311. continue;
  312. }
  313. retval[i] = new NotImplementedFunction(fm.getName());
  314. }
  315. }
  316. return retval;
  317. }
  318. /**
  319. * @return <code>null</code> if the specified functionIndex is for INDIRECT() or any external (add-in) function.
  320. */
  321. public static Function getBasicFunction(int functionIndex) {
  322. // check for 'free ref' functions first
  323. switch (functionIndex) {
  324. case FunctionID.INDIRECT:
  325. case FunctionID.EXTERNAL_FUNC:
  326. return null;
  327. }
  328. // else - must be plain function
  329. Function result = functions[functionIndex];
  330. if (result == null) {
  331. throw new NotImplementedException("FuncIx=" + functionIndex);
  332. }
  333. return result;
  334. }
  335. /**
  336. * Register a new function in runtime.
  337. *
  338. * @param name the function name
  339. * @param func the functoin to register
  340. * @throws IllegalArgumentException if the function is unknown or already registered.
  341. * @since 3.8 beta6
  342. */
  343. public static void registerFunction(String name, Function func){
  344. FunctionMetadata metaData = FunctionMetadataRegistry.getFunctionByName(name);
  345. if(metaData == null) {
  346. if(AnalysisToolPak.isATPFunction(name)) {
  347. throw new IllegalArgumentException(name + " is a function from the Excel Analysis Toolpack. " +
  348. "Use AnalysisToolpack.registerFunction(String name, FreeRefFunction func) instead.");
  349. }
  350. throw new IllegalArgumentException("Unknown function: " + name);
  351. }
  352. int idx = metaData.getIndex();
  353. if(functions[idx] instanceof NotImplementedFunction) {
  354. functions[idx] = func;
  355. } else {
  356. throw new IllegalArgumentException("POI already implements " + name +
  357. ". You cannot override POI's implementations of Excel functions");
  358. }
  359. }
  360. /**
  361. * Returns a collection of function names implemented by POI.
  362. *
  363. * @return an array of supported functions
  364. * @since 3.8 beta6
  365. */
  366. public static Collection<String> getSupportedFunctionNames() {
  367. Collection<String> lst = new TreeSet<>();
  368. for (int i = 0; i < functions.length; i++) {
  369. Function func = functions[i];
  370. FunctionMetadata metaData = FunctionMetadataRegistry.getFunctionByIndex(i);
  371. if (func != null && !(func instanceof NotImplementedFunction)) {
  372. lst.add(metaData.getName());
  373. }
  374. }
  375. lst.add("INDIRECT"); // INDIRECT is a special case
  376. return Collections.unmodifiableCollection(lst);
  377. }
  378. /**
  379. * Returns an array of function names NOT implemented by POI.
  380. *
  381. * @return an array of not supported functions
  382. * @since 3.8 beta6
  383. */
  384. public static Collection<String> getNotSupportedFunctionNames() {
  385. Collection<String> lst = new TreeSet<>();
  386. for (int i = 0; i < functions.length; i++) {
  387. Function func = functions[i];
  388. if (func != null && (func instanceof NotImplementedFunction)) {
  389. FunctionMetadata metaData = FunctionMetadataRegistry.getFunctionByIndex(i);
  390. lst.add(metaData.getName());
  391. }
  392. }
  393. lst.remove("INDIRECT"); // INDIRECT is a special case
  394. return Collections.unmodifiableCollection(lst);
  395. }
  396. }