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.

OperandResolver.java 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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.EvaluationCell;
  17. import org.apache.poi.ss.usermodel.DateUtil;
  18. import org.apache.poi.ss.util.CellRangeAddress;
  19. import java.time.DateTimeException;
  20. import java.util.regex.Pattern;
  21. /**
  22. * Provides functionality for evaluating arguments to functions and operators.
  23. */
  24. public final class OperandResolver {
  25. // Based on regular expression defined in JavaDoc at {@link java.lang.Double#valueOf}
  26. // modified to remove support for NaN, Infinity, Hexadecimal support and floating type suffixes
  27. private static final String Digits = "(\\p{Digit}+)";
  28. private static final String Exp = "[eE][+-]?"+Digits;
  29. private static final String fpRegex =
  30. ("[\\x00-\\x20]*" +
  31. "[+-]?(" +
  32. "("+Digits+"(\\.)?("+Digits+"?)("+Exp+")?)|"+
  33. "(\\."+Digits+"("+Exp+")?))"+
  34. "[\\x00-\\x20]*");
  35. private static final Pattern fpPattern = Pattern.compile(fpRegex);
  36. private OperandResolver() {
  37. // no instances of this class
  38. }
  39. /**
  40. * Retrieves a single value from a variety of different argument types according to standard
  41. * Excel rules. Does not perform any type conversion.
  42. * @param arg the evaluated argument as passed to the function or operator.
  43. * @param srcCellRow used when arg is a single column AreaRef
  44. * @param srcCellCol used when arg is a single row AreaRef
  45. * @return a {@code NumberEval}, {@code StringEval}, {@code BoolEval} or {@code BlankEval}.
  46. * Never {@code null} or {@code ErrorEval}.
  47. * @throws EvaluationException if srcCellRow or srcCellCol do not properly index into
  48. * an AreaEval. If the actual value retrieved is an ErrorEval, a corresponding
  49. * EvaluationException is thrown.
  50. */
  51. public static ValueEval getSingleValue(ValueEval arg, int srcCellRow, int srcCellCol)
  52. throws EvaluationException {
  53. final ValueEval result;
  54. if (arg instanceof RefEval) {
  55. result = chooseSingleElementFromRef((RefEval) arg);
  56. } else if (arg instanceof AreaEval) {
  57. result = chooseSingleElementFromArea((AreaEval) arg, srcCellRow, srcCellCol);
  58. } else {
  59. result = arg;
  60. }
  61. if (result instanceof ErrorEval) {
  62. throw new EvaluationException((ErrorEval) result);
  63. }
  64. return result;
  65. }
  66. /**
  67. * Retrieves a single value from an area evaluation utilizing the 2D indices of the cell
  68. * within its own area reference to index the value in the area evaluation.
  69. *
  70. * @param ae area reference after evaluation
  71. * @param cell the source cell of the formula that contains its 2D indices
  72. * @return a {@code NumberEval}, {@code StringEval}, {@code BoolEval} or {@code BlankEval}. or {@code ErrorEval}
  73. * Never {@code null}.
  74. */
  75. public static ValueEval getElementFromArray(AreaEval ae, EvaluationCell cell) {
  76. CellRangeAddress range = cell.getArrayFormulaRange();
  77. int relativeRowIndex = cell.getRowIndex() - range.getFirstRow();
  78. int relativeColIndex = cell.getColumnIndex() - range.getFirstColumn();
  79. if (ae.isColumn()) {
  80. if (ae.isRow()) {
  81. return ae.getRelativeValue(0, 0);
  82. }
  83. else if(relativeRowIndex < ae.getHeight()) {
  84. return ae.getRelativeValue(relativeRowIndex, 0);
  85. }
  86. }
  87. else if (!ae.isRow() && relativeRowIndex < ae.getHeight() && relativeColIndex < ae.getWidth()) {
  88. return ae.getRelativeValue(relativeRowIndex, relativeColIndex);
  89. }
  90. else if (ae.isRow() && relativeColIndex < ae.getWidth()) {
  91. return ae.getRelativeValue(0, relativeColIndex);
  92. }
  93. return ErrorEval.NA;
  94. }
  95. /**
  96. * Implements (some perhaps not well known) Excel functionality to select a single cell from an
  97. * area depending on the coordinates of the calling cell. Here is an example demonstrating
  98. * both selection from a single row area and a single column area in the same formula.
  99. *
  100. * <table>
  101. * <caption>sample spreadsheet</caption>
  102. * <tr><th>&nbsp;</th><th>&nbsp;A&nbsp;</th><th>&nbsp;B&nbsp;</th><th>&nbsp;C&nbsp;</th><th>&nbsp;D&nbsp;</th></tr>
  103. * <tr><th>1</th><td>15</td><td>20</td><td>25</td><td>&nbsp;</td></tr>
  104. * <tr><th>2</th><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>200</td></tr>
  105. * <tr><th>3</th><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>300</td></tr>
  106. * <tr><th>3</th><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>400</td></tr>
  107. * </table>
  108. *
  109. * If the formula "=1000+A1:B1+D2:D3" is put into the 9 cells from A2 to C4, the spreadsheet
  110. * will look like this:
  111. *
  112. * <table>
  113. * <caption>sample spreadsheet</caption>
  114. * <tr><th>&nbsp;</th><th>&nbsp;A&nbsp;</th><th>&nbsp;B&nbsp;</th><th>&nbsp;C&nbsp;</th><th>&nbsp;D&nbsp;</th></tr>
  115. * <tr><th>1</th><td>15</td><td>20</td><td>25</td><td>&nbsp;</td></tr>
  116. * <tr><th>2</th><td>1215</td><td>1220</td><td>#VALUE!</td><td>200</td></tr>
  117. * <tr><th>3</th><td>1315</td><td>1320</td><td>#VALUE!</td><td>300</td></tr>
  118. * <tr><th>4</th><td>#VALUE!</td><td>#VALUE!</td><td>#VALUE!</td><td>400</td></tr>
  119. * </table>
  120. *
  121. * Note that the row area (A1:B1) does not include column C and the column area (D2:D3) does
  122. * not include row 4, so the values in C1(=25) and D4(=400) are not accessible to the formula
  123. * as written, but in the 4 cells A2:B3, the row and column selection works ok.<p>
  124. *
  125. * The same concept is extended to references across sheets, such that even multi-row,
  126. * multi-column areas can be useful.<p>
  127. *
  128. * Of course with carefully (or carelessly) chosen parameters, cyclic references can occur and
  129. * hence this method <b>can</b> throw a 'circular reference' EvaluationException. Note that
  130. * this method does not attempt to detect cycles. Every cell in the specified Area {@code ae}
  131. * has already been evaluated prior to this method call. Any cell (or cell<b>s</b>) part of
  132. * {@code ae} that would incur a cyclic reference error if selected by this method, will
  133. * already have the value {@code ErrorEval.CIRCULAR_REF_ERROR} upon entry to this method. It
  134. * is assumed logic exists elsewhere to produce this behaviour.
  135. *
  136. * @return whatever the selected cell's evaluated value is. Never {@code null}. Never
  137. * {@code ErrorEval}.
  138. * @throws EvaluationException if there is a problem with indexing into the area, or if the
  139. * evaluated cell has an error.
  140. */
  141. public static ValueEval chooseSingleElementFromArea(AreaEval ae,
  142. int srcCellRow, int srcCellCol) throws EvaluationException {
  143. ValueEval result = chooseSingleElementFromAreaInternal(ae, srcCellRow, srcCellCol);
  144. if (result instanceof ErrorEval) {
  145. throw new EvaluationException((ErrorEval) result);
  146. }
  147. return result;
  148. }
  149. /**
  150. * @return possibly {@code ErrorEval}, and {@code null}
  151. */
  152. private static ValueEval chooseSingleElementFromAreaInternal(AreaEval ae,
  153. int srcCellRow, int srcCellCol) throws EvaluationException {
  154. // if(false) {
  155. // // this is too simplistic
  156. // if(ae.containsRow(srcCellRow) && ae.containsColumn(srcCellCol)) {
  157. // throw new EvaluationException(ErrorEval.CIRCULAR_REF_ERROR);
  158. // }
  159. // /*
  160. // Circular references are not dealt with directly here, but it is worth noting some issues.
  161. //
  162. // ANY one of the return statements in this method could return a cell that is identical
  163. // to the one immediately being evaluated. The evaluating cell is identified by srcCellRow,
  164. // srcCellRow AND sheet. The sheet is not available in any nearby calling method, so that's
  165. // one reason why circular references are not easy to detect here. (The sheet of the returned
  166. // cell can be obtained from ae if it is an Area3DEval.)
  167. //
  168. // Another reason there's little value in attempting to detect circular references here is
  169. // that only direct circular references could be detected. If the cycle involved two or more
  170. // cells this method could not detect it.
  171. //
  172. // Logic to detect evaluation cycles of all kinds has been coded in EvaluationCycleDetector
  173. // (and FormulaEvaluator).
  174. // */
  175. // }
  176. if (ae.isColumn()) {
  177. if(ae.isRow()) {
  178. return ae.getRelativeValue(0, 0);
  179. }
  180. if(!ae.containsRow(srcCellRow)) {
  181. throw EvaluationException.invalidValue();
  182. }
  183. return ae.getAbsoluteValue(srcCellRow, ae.getFirstColumn());
  184. }
  185. if(!ae.isRow()) {
  186. // multi-column, multi-row area
  187. if(ae.containsRow(srcCellRow) && ae.containsColumn(srcCellCol)) {
  188. return ae.getAbsoluteValue(srcCellRow, srcCellCol);
  189. }
  190. throw EvaluationException.invalidValue();
  191. }
  192. if(!ae.containsColumn(srcCellCol)) {
  193. throw EvaluationException.invalidValue();
  194. }
  195. return ae.getAbsoluteValue(ae.getFirstRow(), srcCellCol);
  196. }
  197. private static ValueEval chooseSingleElementFromRef(RefEval ref) {
  198. return ref.getInnerValueEval( ref.getFirstSheetIndex() );
  199. }
  200. /**
  201. * Applies some conversion rules if the supplied value is not already an integer.<br>
  202. * Value is first coerced to a {@code double} ( See {@code coerceValueToDouble()} ).
  203. * Note - {@code BlankEval} is converted to {@code 0}.<p>
  204. *
  205. * Excel typically converts doubles to integers by truncating toward negative infinity.<br>
  206. * The equivalent java code is:<br>
  207. * &nbsp;&nbsp;{@code return (int)Math.floor(d);}<br>
  208. * <b>not</b>:<br>
  209. * &nbsp;&nbsp;{@code return (int)d; // wrong - rounds toward zero}
  210. *
  211. */
  212. public static int coerceValueToInt(ValueEval ev) throws EvaluationException {
  213. if (ev == BlankEval.instance) {
  214. return 0;
  215. }
  216. double d = coerceValueToDouble(ev);
  217. // Note - the standard java type conversion from double to int truncates toward zero.
  218. // but Math.floor() truncates toward negative infinity
  219. return (int)Math.floor(d);
  220. }
  221. /**
  222. * Applies some conversion rules if the supplied value is not already a number.
  223. * Note - {@code BlankEval} is converted to {@link NumberEval#ZERO}.
  224. * @param ev must be a {@link NumberEval}, {@link StringEval}, {@link BoolEval} or
  225. * {@link BlankEval}
  226. * @return actual, parsed or interpreted double value (respectively).
  227. * @throws EvaluationException if a StringEval is supplied and cannot be parsed
  228. * as a double (See {@code parseDouble()} for allowable formats).
  229. * @throws RuntimeException if the supplied parameter is not {@link NumberEval},
  230. * {@link StringEval}, {@link BoolEval} or {@link BlankEval}
  231. */
  232. public static double coerceValueToDouble(ValueEval ev) throws EvaluationException {
  233. if (ev == BlankEval.instance) {
  234. return 0.0;
  235. }
  236. if (ev instanceof NumericValueEval) {
  237. // this also handles booleans
  238. return ((NumericValueEval)ev).getNumberValue();
  239. }
  240. if (ev instanceof StringEval) {
  241. String sval = ((StringEval) ev).getStringValue();
  242. Double dd = parseDouble(sval);
  243. if(dd == null) dd = parseDateTime(sval);
  244. if (dd == null) {
  245. throw EvaluationException.invalidValue();
  246. }
  247. return dd;
  248. }
  249. throw new RuntimeException("Unexpected arg eval type (" + ev.getClass().getName() + ")");
  250. }
  251. /**
  252. * Converts a string to a double using standard rules that Excel would use.<br>
  253. * Tolerates leading and trailing spaces, <p>
  254. *
  255. * Doesn't support currency prefixes, commas, percentage signs or arithmetic operations strings.
  256. *
  257. * Some examples:<br>
  258. * " 123 " -&gt; 123.0<br>
  259. * ".123" -&gt; 0.123<br>
  260. * "1E4" -&gt; 1000<br>
  261. * "-123" -&gt; -123.0<br>
  262. * These not supported yet:<br>
  263. * " $ 1,000.00 " -&gt; 1000.0<br>
  264. * "$1.25E4" -&gt; 12500.0<br>
  265. * "5**2" -&gt; 500<br>
  266. * "250%" -&gt; 2.5<br>
  267. *
  268. * @return {@code null} if the specified text cannot be parsed as a number
  269. */
  270. public static Double parseDouble(String pText) {
  271. if (fpPattern.matcher(pText).matches())
  272. try {
  273. return Double.parseDouble(pText);
  274. } catch (NumberFormatException e) {
  275. return null;
  276. }
  277. else {
  278. return null;
  279. }
  280. }
  281. public static Double parseDateTime(String pText) {
  282. try {
  283. return DateUtil.parseDateTime(pText);
  284. } catch (DateTimeException e) {
  285. return null;
  286. }
  287. }
  288. /**
  289. * @param ve must be a {@code NumberEval}, {@code StringEval}, {@code BoolEval}, or {@code BlankEval}
  290. * @return the converted string value. never {@code null}
  291. */
  292. public static String coerceValueToString(ValueEval ve) {
  293. if (ve instanceof StringValueEval) {
  294. StringValueEval sve = (StringValueEval) ve;
  295. return sve.getStringValue();
  296. }
  297. if (ve == BlankEval.instance) {
  298. return "";
  299. }
  300. throw new IllegalArgumentException("Unexpected eval class (" + ve.getClass().getName() + ")");
  301. }
  302. /**
  303. * @return {@code null} to represent blank values
  304. * @throws EvaluationException if ve is an ErrorEval, or if a string value cannot be converted
  305. */
  306. public static Boolean coerceValueToBoolean(ValueEval ve, boolean stringsAreBlanks) throws EvaluationException {
  307. if (ve == null || ve == BlankEval.instance) {
  308. // TODO - remove 've == null' condition once AreaEval is fixed
  309. return null;
  310. }
  311. if (ve instanceof BoolEval) {
  312. return ((BoolEval) ve).getBooleanValue();
  313. }
  314. if (ve instanceof StringEval) {
  315. if (stringsAreBlanks) {
  316. return null;
  317. }
  318. String str = ((StringEval) ve).getStringValue();
  319. if (str.equalsIgnoreCase("true")) {
  320. return Boolean.TRUE;
  321. }
  322. if (str.equalsIgnoreCase("false")) {
  323. return Boolean.FALSE;
  324. }
  325. // else - string cannot be converted to boolean
  326. throw new EvaluationException(ErrorEval.VALUE_INVALID);
  327. }
  328. if (ve instanceof NumericValueEval) {
  329. NumericValueEval ne = (NumericValueEval) ve;
  330. double d = ne.getNumberValue();
  331. if (Double.isNaN(d)) {
  332. throw new EvaluationException(ErrorEval.VALUE_INVALID);
  333. }
  334. return d != 0;
  335. }
  336. if (ve instanceof ErrorEval) {
  337. throw new EvaluationException((ErrorEval) ve);
  338. }
  339. throw new RuntimeException("Unexpected eval (" + ve.getClass().getName() + ")");
  340. }
  341. }