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.

WorkbookEvaluator.java 43KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998
  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;
  16. import java.util.Arrays;
  17. import java.util.Collection;
  18. import java.util.Collections;
  19. import java.util.IdentityHashMap;
  20. import java.util.Map;
  21. import java.util.Stack;
  22. import java.util.TreeSet;
  23. import org.apache.logging.log4j.LogManager;
  24. import org.apache.logging.log4j.Logger;
  25. import org.apache.logging.log4j.message.SimpleMessage;
  26. import org.apache.poi.ss.SpreadsheetVersion;
  27. import org.apache.poi.ss.formula.CollaboratingWorkbooksEnvironment.WorkbookNotFoundException;
  28. import org.apache.poi.ss.formula.atp.AnalysisToolPak;
  29. import org.apache.poi.ss.formula.eval.*;
  30. import org.apache.poi.ss.formula.function.FunctionMetadataRegistry;
  31. import org.apache.poi.ss.formula.functions.*;
  32. import org.apache.poi.ss.formula.ptg.*;
  33. import org.apache.poi.ss.formula.udf.AggregatingUDFFinder;
  34. import org.apache.poi.ss.formula.udf.UDFFinder;
  35. import org.apache.poi.ss.usermodel.CellType;
  36. import org.apache.poi.ss.util.CellRangeAddressBase;
  37. import org.apache.poi.ss.util.CellReference;
  38. import org.apache.poi.util.Internal;
  39. import static org.apache.logging.log4j.util.Unbox.box;
  40. /**
  41. * Evaluates formula cells.<p/>
  42. *
  43. * For performance reasons, this class keeps a cache of all previously calculated intermediate
  44. * cell values. Be sure to call {@link #clearAllCachedResultValues()} if any workbook cells are changed between
  45. * calls to evaluate~ methods on this class.<br/>
  46. *
  47. * For POI internal use only
  48. *
  49. * @author Josh Micich
  50. * @author Thies Wellpott (debug output enhancements)
  51. */
  52. @Internal
  53. public final class WorkbookEvaluator {
  54. private static final Logger LOG = LogManager.getLogger(WorkbookEvaluator.class);
  55. private final EvaluationWorkbook _workbook;
  56. private EvaluationCache _cache;
  57. /** part of cache entry key (useful when evaluating multiple workbooks) */
  58. private int _workbookIx;
  59. private final IEvaluationListener _evaluationListener;
  60. private final Map<EvaluationSheet, Integer> _sheetIndexesBySheet;
  61. private final Map<String, Integer> _sheetIndexesByName;
  62. private CollaboratingWorkbooksEnvironment _collaboratingWorkbookEnvironment;
  63. private final IStabilityClassifier _stabilityClassifier;
  64. private final AggregatingUDFFinder _udfFinder;
  65. private boolean _ignoreMissingWorkbooks;
  66. /**
  67. * whether print detailed messages about the next formula evaluation
  68. */
  69. private boolean dbgEvaluationOutputForNextEval;
  70. // special logger for formula evaluation output (because of possibly very large output)
  71. private final Logger EVAL_LOG = LogManager.getLogger("POI.FormulaEval");
  72. // current indent level for evalution; negative value for no output
  73. private int dbgEvaluationOutputIndent = -1;
  74. /**
  75. * @param udfFinder pass <code>null</code> for default (AnalysisToolPak only)
  76. */
  77. public WorkbookEvaluator(EvaluationWorkbook workbook, IStabilityClassifier stabilityClassifier, UDFFinder udfFinder) {
  78. this (workbook, null, stabilityClassifier, udfFinder);
  79. }
  80. /* package */ WorkbookEvaluator(EvaluationWorkbook workbook, IEvaluationListener evaluationListener,
  81. IStabilityClassifier stabilityClassifier, UDFFinder udfFinder) {
  82. _workbook = workbook;
  83. _evaluationListener = evaluationListener;
  84. _cache = new EvaluationCache(evaluationListener);
  85. _sheetIndexesBySheet = new IdentityHashMap<>();
  86. _sheetIndexesByName = new IdentityHashMap<>();
  87. _collaboratingWorkbookEnvironment = CollaboratingWorkbooksEnvironment.EMPTY;
  88. _workbookIx = 0;
  89. _stabilityClassifier = stabilityClassifier;
  90. AggregatingUDFFinder defaultToolkit = // workbook can be null in unit tests
  91. workbook == null ? null : (AggregatingUDFFinder)workbook.getUDFFinder();
  92. if(defaultToolkit != null && udfFinder != null) {
  93. defaultToolkit.add(udfFinder);
  94. }
  95. _udfFinder = defaultToolkit;
  96. }
  97. /**
  98. * also for debug use. Used in toString methods
  99. */
  100. /* package */ String getSheetName(int sheetIndex) {
  101. return _workbook.getSheetName(sheetIndex);
  102. }
  103. /* package */ EvaluationSheet getSheet(int sheetIndex) {
  104. return _workbook.getSheet(sheetIndex);
  105. }
  106. /* package */ EvaluationWorkbook getWorkbook() {
  107. return _workbook;
  108. }
  109. /* package */ EvaluationName getName(String name, int sheetIndex) {
  110. return _workbook.getName(name, sheetIndex);
  111. }
  112. /* package */ void attachToEnvironment(CollaboratingWorkbooksEnvironment collaboratingWorkbooksEnvironment, EvaluationCache cache, int workbookIx) {
  113. _collaboratingWorkbookEnvironment = collaboratingWorkbooksEnvironment;
  114. _cache = cache;
  115. _workbookIx = workbookIx;
  116. }
  117. /* package */ CollaboratingWorkbooksEnvironment getEnvironment() {
  118. return _collaboratingWorkbookEnvironment;
  119. }
  120. /**
  121. * Discards the current workbook environment and attaches to the default 'empty' environment.
  122. * Also resets evaluation cache.
  123. */
  124. /* package */ void detachFromEnvironment() {
  125. _collaboratingWorkbookEnvironment = CollaboratingWorkbooksEnvironment.EMPTY;
  126. _cache = new EvaluationCache(_evaluationListener);
  127. _workbookIx = 0;
  128. }
  129. /**
  130. * @return the evaluator for another workbook which is part of the same {@link CollaboratingWorkbooksEnvironment}
  131. */
  132. /* package */ WorkbookEvaluator getOtherWorkbookEvaluator(String workbookName) throws WorkbookNotFoundException {
  133. return _collaboratingWorkbookEnvironment.getWorkbookEvaluator(workbookName);
  134. }
  135. /* package */ IEvaluationListener getEvaluationListener() {
  136. return _evaluationListener;
  137. }
  138. /**
  139. * Should be called whenever there are changes to input cells in the evaluated workbook.
  140. * Failure to call this method after changing cell values will cause incorrect behaviour
  141. * of the evaluate~ methods of this class
  142. */
  143. public void clearAllCachedResultValues() {
  144. _cache.clear();
  145. _sheetIndexesBySheet.clear();
  146. _workbook.clearAllCachedResultValues();
  147. }
  148. /**
  149. * Should be called to tell the cell value cache that the specified (value or formula) cell
  150. * has changed.
  151. */
  152. public void notifyUpdateCell(EvaluationCell cell) {
  153. int sheetIndex = getSheetIndex(cell.getSheet());
  154. _cache.notifyUpdateCell(_workbookIx, sheetIndex, cell);
  155. }
  156. /**
  157. * Should be called to tell the cell value cache that the specified cell has just been
  158. * deleted.
  159. */
  160. public void notifyDeleteCell(EvaluationCell cell) {
  161. int sheetIndex = getSheetIndex(cell.getSheet());
  162. _cache.notifyDeleteCell(_workbookIx, sheetIndex, cell);
  163. }
  164. private int getSheetIndex(EvaluationSheet sheet) {
  165. Integer result = _sheetIndexesBySheet.get(sheet);
  166. if (result == null) {
  167. int sheetIndex = _workbook.getSheetIndex(sheet);
  168. if (sheetIndex < 0) {
  169. throw new RuntimeException("Specified sheet from a different book");
  170. }
  171. result = Integer.valueOf(sheetIndex);
  172. _sheetIndexesBySheet.put(sheet, result);
  173. }
  174. return result.intValue();
  175. }
  176. public ValueEval evaluate(EvaluationCell srcCell) {
  177. int sheetIndex = getSheetIndex(srcCell.getSheet());
  178. return evaluateAny(srcCell, sheetIndex, srcCell.getRowIndex(), srcCell.getColumnIndex(), new EvaluationTracker(_cache));
  179. }
  180. /**
  181. * Case-insensitive.
  182. * @return -1 if sheet with specified name does not exist
  183. */
  184. /* package */ int getSheetIndex(String sheetName) {
  185. Integer result = _sheetIndexesByName.get(sheetName);
  186. if (result == null) {
  187. int sheetIndex = _workbook.getSheetIndex(sheetName);
  188. if (sheetIndex < 0) {
  189. return -1;
  190. }
  191. result = Integer.valueOf(sheetIndex);
  192. _sheetIndexesByName.put(sheetName, result);
  193. }
  194. return result.intValue();
  195. }
  196. /* package */ int getSheetIndexByExternIndex(int externSheetIndex) {
  197. return _workbook.convertFromExternSheetIndex(externSheetIndex);
  198. }
  199. /**
  200. * @return never <code>null</code>, never {@link BlankEval}
  201. */
  202. private ValueEval evaluateAny(EvaluationCell srcCell, int sheetIndex,
  203. int rowIndex, int columnIndex, EvaluationTracker tracker) {
  204. // avoid tracking dependencies to cells that have constant definition
  205. boolean shouldCellDependencyBeRecorded = _stabilityClassifier == null ? true
  206. : !_stabilityClassifier.isCellFinal(sheetIndex, rowIndex, columnIndex);
  207. if (srcCell == null || srcCell.getCellType() != CellType.FORMULA) {
  208. ValueEval result = getValueFromNonFormulaCell(srcCell);
  209. if (shouldCellDependencyBeRecorded) {
  210. tracker.acceptPlainValueDependency(_workbook, _workbookIx, sheetIndex, rowIndex, columnIndex, result);
  211. }
  212. return result;
  213. }
  214. FormulaCellCacheEntry cce = _cache.getOrCreateFormulaCellEntry(srcCell);
  215. if (shouldCellDependencyBeRecorded || cce.isInputSensitive()) {
  216. tracker.acceptFormulaDependency(cce);
  217. }
  218. IEvaluationListener evalListener = _evaluationListener;
  219. ValueEval result;
  220. if (cce.getValue() == null) {
  221. if (!tracker.startEvaluate(cce)) {
  222. return ErrorEval.CIRCULAR_REF_ERROR;
  223. }
  224. try {
  225. Ptg[] ptgs = _workbook.getFormulaTokens(srcCell);
  226. OperationEvaluationContext ec = new OperationEvaluationContext
  227. (this, _workbook, sheetIndex, rowIndex, columnIndex, tracker);
  228. if (evalListener == null) {
  229. result = evaluateFormula(ec, ptgs);
  230. } else {
  231. evalListener.onStartEvaluate(srcCell, cce);
  232. result = evaluateFormula(ec, ptgs);
  233. evalListener.onEndEvaluate(cce, result);
  234. }
  235. tracker.updateCacheResult(result);
  236. }
  237. catch (NotImplementedException e) {
  238. throw addExceptionInfo(e, sheetIndex, rowIndex, columnIndex);
  239. } catch (RuntimeException re) {
  240. if (re.getCause() instanceof WorkbookNotFoundException && _ignoreMissingWorkbooks) {
  241. LOG.atInfo().log("{} - Continuing with cached value!", re.getCause().getMessage());
  242. switch(srcCell.getCachedFormulaResultType()) {
  243. case NUMERIC:
  244. result = new NumberEval(srcCell.getNumericCellValue());
  245. break;
  246. case STRING:
  247. result = new StringEval(srcCell.getStringCellValue());
  248. break;
  249. case BLANK:
  250. result = BlankEval.instance;
  251. break;
  252. case BOOLEAN:
  253. result = BoolEval.valueOf(srcCell.getBooleanCellValue());
  254. break;
  255. case ERROR:
  256. result = ErrorEval.valueOf(srcCell.getErrorCellValue());
  257. break;
  258. case FORMULA:
  259. default:
  260. throw new RuntimeException("Unexpected cell type '" + srcCell.getCellType()+"' found!");
  261. }
  262. } else {
  263. throw re;
  264. }
  265. } finally {
  266. tracker.endEvaluate(cce);
  267. }
  268. } else {
  269. if(evalListener != null) {
  270. evalListener.onCacheHit(sheetIndex, rowIndex, columnIndex, cce.getValue());
  271. }
  272. return cce.getValue();
  273. }
  274. final ValueEval resultForLogging = result;
  275. LOG.atDebug().log(()->{
  276. String sheetName = getSheetName(sheetIndex);
  277. CellReference cr = new CellReference(rowIndex, columnIndex);
  278. return new SimpleMessage("Evaluated " + sheetName + "!" + cr.formatAsString() + " to " + resultForLogging);
  279. });
  280. // Usually (result === cce.getValue())
  281. // But sometimes: (result==ErrorEval.CIRCULAR_REF_ERROR, cce.getValue()==null)
  282. // When circular references are detected, the cache entry is only updated for
  283. // the top evaluation frame
  284. return result;
  285. }
  286. /**
  287. * Adds the current cell reference to the exception for easier debugging.
  288. * Would be nice to get the formula text as well, but that seems to require
  289. * too much digging around and casting to get the FormulaRenderingWorkbook.
  290. */
  291. private NotImplementedException addExceptionInfo(NotImplementedException inner, int sheetIndex, int rowIndex, int columnIndex) {
  292. try {
  293. String sheetName = _workbook.getSheetName(sheetIndex);
  294. CellReference cr = new CellReference(sheetName, rowIndex, columnIndex, false, false);
  295. String msg = "Error evaluating cell " + cr.formatAsString();
  296. return new NotImplementedException(msg, inner);
  297. } catch (Exception e) {
  298. // avoid bombing out during exception handling
  299. LOG.atError().withThrowable(e).log("Can't add exception info");
  300. return inner; // preserve original exception
  301. }
  302. }
  303. /**
  304. * Gets the value from a non-formula cell.
  305. * @param cell may be <code>null</code>
  306. * @return {@link BlankEval} if cell is <code>null</code> or blank, never <code>null</code>
  307. */
  308. /* package */ static ValueEval getValueFromNonFormulaCell(EvaluationCell cell) {
  309. if (cell == null) {
  310. return BlankEval.instance;
  311. }
  312. CellType cellType = cell.getCellType();
  313. switch (cellType) {
  314. case NUMERIC:
  315. return new NumberEval(cell.getNumericCellValue());
  316. case STRING:
  317. return new StringEval(cell.getStringCellValue());
  318. case BOOLEAN:
  319. return BoolEval.valueOf(cell.getBooleanCellValue());
  320. case BLANK:
  321. return BlankEval.instance;
  322. case ERROR:
  323. return ErrorEval.valueOf(cell.getErrorCellValue());
  324. default:
  325. throw new RuntimeException("Unexpected cell type (" + cellType + ")");
  326. }
  327. }
  328. // visibility raised for testing
  329. @Internal
  330. /* package */ ValueEval evaluateFormula(OperationEvaluationContext ec, Ptg[] ptgs) {
  331. String dbgIndentStr = ""; // always init. to non-null just for defensive avoiding NPE
  332. if (dbgEvaluationOutputForNextEval) {
  333. // first evaluation call when ouput is desired, so iit. this evaluator instance
  334. dbgEvaluationOutputIndent = 1;
  335. dbgEvaluationOutputForNextEval = false;
  336. }
  337. if (dbgEvaluationOutputIndent > 0) {
  338. // init. indent string to needed spaces (create as substring from very long space-only string;
  339. // limit indentation for deep recursions)
  340. dbgIndentStr = " ";
  341. dbgIndentStr = dbgIndentStr.substring(0, Math.min(dbgIndentStr.length(), dbgEvaluationOutputIndent*2));
  342. String finalDbgIndentStr = dbgIndentStr;
  343. EVAL_LOG.atWarn().log(() -> {
  344. String message = finalDbgIndentStr
  345. + "- evaluateFormula('" + ec.getRefEvaluatorForCurrentSheet().getSheetNameRange()
  346. + "'/" + new CellReference(ec.getRowIndex(), ec.getColumnIndex()).formatAsString()
  347. + "): " + Arrays.toString(ptgs).replaceAll("\\Qorg.apache.poi.ss.formula.ptg.\\E", "");
  348. return new SimpleMessage(message);
  349. });
  350. dbgEvaluationOutputIndent++;
  351. }
  352. EvaluationSheet evalSheet = ec.getWorkbook().getSheet(ec.getSheetIndex());
  353. EvaluationCell evalCell = evalSheet.getCell(ec.getRowIndex(), ec.getColumnIndex());
  354. Stack<ValueEval> stack = new Stack<>();
  355. for (int i = 0, iSize = ptgs.length; i < iSize; i++) {
  356. // since we don't know how to handle these yet :(
  357. Ptg ptg = ptgs[i];
  358. if (dbgEvaluationOutputIndent > 0) {
  359. EVAL_LOG.atInfo().log("{} * ptg {}: {}, stack: {}", dbgIndentStr, box(i),ptg, stack);
  360. }
  361. if (ptg instanceof AttrPtg) {
  362. AttrPtg attrPtg = (AttrPtg) ptg;
  363. if (attrPtg.isSum()) {
  364. // Excel prefers to encode 'SUM()' as a tAttr token, but this evaluator
  365. // expects the equivalent function token
  366. ptg = FuncVarPtg.SUM;
  367. }
  368. if (attrPtg.isOptimizedChoose()) {
  369. ValueEval arg0 = stack.pop();
  370. int[] jumpTable = attrPtg.getJumpTable();
  371. int dist;
  372. int nChoices = jumpTable.length;
  373. try {
  374. int switchIndex = Choose.evaluateFirstArg(arg0, ec.getRowIndex(), ec.getColumnIndex());
  375. if (switchIndex<1 || switchIndex > nChoices) {
  376. stack.push(ErrorEval.VALUE_INVALID);
  377. dist = attrPtg.getChooseFuncOffset() + 4; // +4 for tFuncFar(CHOOSE)
  378. } else {
  379. dist = jumpTable[switchIndex-1];
  380. }
  381. } catch (EvaluationException e) {
  382. stack.push(e.getErrorEval());
  383. dist = attrPtg.getChooseFuncOffset() + 4; // +4 for tFuncFar(CHOOSE)
  384. }
  385. // Encoded dist for tAttrChoose includes size of jump table, but
  386. // countTokensToBeSkipped() does not (it counts whole tokens).
  387. dist -= nChoices*2+2; // subtract jump table size
  388. i+= countTokensToBeSkipped(ptgs, i, dist);
  389. continue;
  390. }
  391. if (attrPtg.isOptimizedIf()) {
  392. if(!evalCell.isPartOfArrayFormulaGroup()) {
  393. ValueEval arg0 = stack.pop();
  394. boolean evaluatedPredicate;
  395. try {
  396. evaluatedPredicate = IfFunc.evaluateFirstArg(arg0, ec.getRowIndex(), ec.getColumnIndex());
  397. } catch (EvaluationException e) {
  398. stack.push(e.getErrorEval());
  399. int dist = attrPtg.getData();
  400. i += countTokensToBeSkipped(ptgs, i, dist);
  401. attrPtg = (AttrPtg) ptgs[i];
  402. dist = attrPtg.getData() + 1;
  403. i += countTokensToBeSkipped(ptgs, i, dist);
  404. continue;
  405. }
  406. if (evaluatedPredicate) {
  407. // nothing to skip - true param follows
  408. } else {
  409. int dist = attrPtg.getData();
  410. i += countTokensToBeSkipped(ptgs, i, dist);
  411. Ptg nextPtg = ptgs[i + 1];
  412. if (ptgs[i] instanceof AttrPtg && nextPtg instanceof FuncVarPtg &&
  413. // in order to verify that there is no third param, we need to check
  414. // if we really have the IF next or some other FuncVarPtg as third param, e.g. ROW()/COLUMN()!
  415. ((FuncVarPtg) nextPtg).getFunctionIndex() == FunctionMetadataRegistry.FUNCTION_INDEX_IF) {
  416. // this is an if statement without a false param (as opposed to MissingArgPtg as the false param)
  417. //i++;
  418. stack.push(arg0);
  419. stack.push(BoolEval.FALSE);
  420. }
  421. }
  422. }
  423. continue;
  424. }
  425. if (attrPtg.isSkip() && !evalCell.isPartOfArrayFormulaGroup()) {
  426. int dist = attrPtg.getData()+1;
  427. i+= countTokensToBeSkipped(ptgs, i, dist);
  428. if (stack.peek() == MissingArgEval.instance) {
  429. stack.pop();
  430. stack.push(BlankEval.instance);
  431. }
  432. continue;
  433. }
  434. }
  435. if (ptg instanceof ControlPtg) {
  436. // skip Parentheses, Attr, etc
  437. continue;
  438. }
  439. if (ptg instanceof MemFuncPtg || ptg instanceof MemAreaPtg) {
  440. // can ignore, rest of tokens for this expression are in OK RPN order
  441. continue;
  442. }
  443. if (ptg instanceof MemErrPtg) {
  444. continue;
  445. }
  446. if (ptg instanceof UnionPtg) {
  447. ValueEval v2 = stack.pop();
  448. ValueEval v1 = stack.pop();
  449. stack.push(new RefListEval(v1, v2));
  450. continue;
  451. }
  452. ValueEval opResult;
  453. if (ptg instanceof OperationPtg) {
  454. OperationPtg optg = (OperationPtg) ptg;
  455. int numops = optg.getNumberOfOperands();
  456. ValueEval[] ops = new ValueEval[numops];
  457. // storing the ops in reverse order since they are popping
  458. boolean areaArg = false; // whether one of the operands is an area
  459. for (int j = numops - 1; j >= 0; j--) {
  460. ValueEval p = stack.pop();
  461. ops[j] = p;
  462. if(p instanceof AreaEval){
  463. areaArg = true;
  464. }
  465. }
  466. boolean arrayMode = false;
  467. if(areaArg) for (int ii = i; ii < iSize; ii++) {
  468. if(ptgs[ii] instanceof FuncVarPtg){
  469. FuncVarPtg f = (FuncVarPtg)ptgs[ii];
  470. try {
  471. Function func = FunctionEval.getBasicFunction(f.getFunctionIndex());
  472. if (func != null && func instanceof ArrayMode) {
  473. arrayMode = true;
  474. }
  475. } catch (NotImplementedException ne){
  476. //FunctionEval.getBasicFunction can throw NotImplementedException
  477. // if the fucntion is not yet supported.
  478. }
  479. break;
  480. }
  481. }
  482. ec.setArrayMode(arrayMode);
  483. // logDebug("invoke " + operation + " (nAgs=" + numops + ")");
  484. opResult = OperationEvaluatorFactory.evaluate(optg, ops, ec);
  485. ec.setArrayMode(false);
  486. } else {
  487. opResult = getEvalForPtg(ptg, ec);
  488. }
  489. if (opResult == null) {
  490. throw new RuntimeException("Evaluation result must not be null");
  491. }
  492. // logDebug("push " + opResult);
  493. stack.push(opResult);
  494. if (dbgEvaluationOutputIndent > 0) {
  495. EVAL_LOG.atInfo().log("{} = {}", dbgIndentStr, opResult);
  496. }
  497. }
  498. ValueEval value = stack.pop();
  499. if (!stack.isEmpty()) {
  500. throw new IllegalStateException("evaluation stack not empty");
  501. }
  502. ValueEval result;
  503. if (ec.isSingleValue()) {
  504. result = dereferenceResult(value, ec);
  505. }
  506. else {
  507. result = value;
  508. }
  509. if (dbgEvaluationOutputIndent > 0) {
  510. EVAL_LOG.atInfo().log("{}finished eval of {}: {}", dbgIndentStr, new CellReference(ec.getRowIndex(), ec.getColumnIndex()).formatAsString(), result);
  511. dbgEvaluationOutputIndent--;
  512. if (dbgEvaluationOutputIndent == 1) {
  513. // this evaluation is done, reset indent to stop logging
  514. dbgEvaluationOutputIndent = -1;
  515. }
  516. } // if
  517. return result;
  518. }
  519. /**
  520. * Calculates the number of tokens that the evaluator should skip upon reaching a tAttrSkip.
  521. *
  522. * @return the number of tokens (starting from <tt>startIndex+1</tt>) that need to be skipped
  523. * to achieve the specified <tt>distInBytes</tt> skip distance.
  524. */
  525. private static int countTokensToBeSkipped(Ptg[] ptgs, int startIndex, int distInBytes) {
  526. int remBytes = distInBytes;
  527. int index = startIndex;
  528. while (remBytes != 0) {
  529. index++;
  530. remBytes -= ptgs[index].getSize();
  531. if (remBytes < 0) {
  532. throw new RuntimeException("Bad skip distance (wrong token size calculation).");
  533. }
  534. if (index >= ptgs.length) {
  535. throw new RuntimeException("Skip distance too far (ran out of formula tokens).");
  536. }
  537. }
  538. return index-startIndex;
  539. }
  540. /**
  541. * Dereferences a single value from any AreaEval or RefEval evaluation
  542. * result. If the supplied evaluationResult is just a plain value, it is
  543. * returned as-is.
  544. *
  545. * @return a {@link NumberEval}, {@link StringEval}, {@link BoolEval}, or
  546. * {@link ErrorEval}. Never <code>null</code>. {@link BlankEval} is
  547. * converted to {@link NumberEval#ZERO}
  548. */
  549. private static ValueEval dereferenceResult(ValueEval evaluationResult, OperationEvaluationContext ec) {
  550. ValueEval value;
  551. if (ec == null) {
  552. throw new IllegalArgumentException("OperationEvaluationContext ec is null");
  553. }
  554. if (ec.getWorkbook() == null) {
  555. throw new IllegalArgumentException("OperationEvaluationContext ec.getWorkbook() is null");
  556. }
  557. EvaluationSheet evalSheet = ec.getWorkbook().getSheet(ec.getSheetIndex());
  558. EvaluationCell evalCell = evalSheet.getCell(ec.getRowIndex(), ec.getColumnIndex());
  559. if (evalCell != null && evalCell.isPartOfArrayFormulaGroup() && evaluationResult instanceof AreaEval) {
  560. value = OperandResolver.getElementFromArray((AreaEval) evaluationResult, evalCell);
  561. }
  562. else {
  563. value = dereferenceResult(evaluationResult, ec.getRowIndex(), ec.getColumnIndex());
  564. }
  565. if (value == BlankEval.instance) {
  566. // Note Excel behaviour here. A blank final final value is converted to zero.
  567. return NumberEval.ZERO;
  568. // Formulas _never_ evaluate to blank. If a formula appears to have evaluated to
  569. // blank, the actual value is empty string. This can be verified with ISBLANK().
  570. }
  571. return value;
  572. }
  573. /**
  574. * Dereferences a single value from any AreaEval or RefEval evaluation
  575. * result. If the supplied evaluationResult is just a plain value, it is
  576. * returned as-is.
  577. *
  578. * @return a {@link NumberEval}, {@link StringEval}, {@link BoolEval}, or
  579. * {@link ErrorEval}. Never <code>null</code>. {@link BlankEval} is
  580. * converted to {@link NumberEval#ZERO}
  581. */
  582. public static ValueEval dereferenceResult(ValueEval evaluationResult, int srcRowNum, int srcColNum) {
  583. ValueEval value;
  584. try {
  585. value = OperandResolver.getSingleValue(evaluationResult, srcRowNum, srcColNum);
  586. } catch (EvaluationException e) {
  587. return e.getErrorEval();
  588. }
  589. if (value == BlankEval.instance) {
  590. // Note Excel behaviour here. A blank final final value is converted to zero.
  591. return NumberEval.ZERO;
  592. // Formulas _never_ evaluate to blank. If a formula appears to have evaluated to
  593. // blank, the actual value is empty string. This can be verified with ISBLANK().
  594. }
  595. return value;
  596. }
  597. /**
  598. * returns an appropriate Eval impl instance for the Ptg. The Ptg must be
  599. * one of: Area3DPtg, AreaPtg, ReferencePtg, Ref3DPtg, IntPtg, NumberPtg,
  600. * StringPtg, BoolPtg <br/>special Note: OperationPtg subtypes cannot be
  601. * passed here!
  602. */
  603. private ValueEval getEvalForPtg(Ptg ptg, OperationEvaluationContext ec) {
  604. // consider converting all these (ptg instanceof XxxPtg) expressions to (ptg.getClass() == XxxPtg.class)
  605. if (ptg instanceof NamePtg) {
  606. // Named ranges, macro functions
  607. NamePtg namePtg = (NamePtg) ptg;
  608. EvaluationName nameRecord = _workbook.getName(namePtg);
  609. return getEvalForNameRecord(nameRecord, ec);
  610. }
  611. if (ptg instanceof NameXPtg) {
  612. // Externally defined named ranges or macro functions
  613. return processNameEval(ec.getNameXEval((NameXPtg)ptg), ec);
  614. }
  615. if (ptg instanceof NameXPxg) {
  616. // Externally defined named ranges or macro functions
  617. return processNameEval(ec.getNameXEval((NameXPxg)ptg), ec);
  618. }
  619. if (ptg instanceof IntPtg) {
  620. return new NumberEval(((IntPtg)ptg).getValue());
  621. }
  622. if (ptg instanceof NumberPtg) {
  623. return new NumberEval(((NumberPtg)ptg).getValue());
  624. }
  625. if (ptg instanceof StringPtg) {
  626. return new StringEval(((StringPtg) ptg).getValue());
  627. }
  628. if (ptg instanceof BoolPtg) {
  629. return BoolEval.valueOf(((BoolPtg) ptg).getValue());
  630. }
  631. if (ptg instanceof ErrPtg) {
  632. return ErrorEval.valueOf(((ErrPtg) ptg).getErrorCode());
  633. }
  634. if (ptg instanceof MissingArgPtg) {
  635. return MissingArgEval.instance;
  636. }
  637. if (ptg instanceof AreaErrPtg ||ptg instanceof RefErrorPtg
  638. || ptg instanceof DeletedArea3DPtg || ptg instanceof DeletedRef3DPtg) {
  639. return ErrorEval.REF_INVALID;
  640. }
  641. if (ptg instanceof Ref3DPtg) {
  642. return ec.getRef3DEval((Ref3DPtg)ptg);
  643. }
  644. if (ptg instanceof Ref3DPxg) {
  645. return ec.getRef3DEval((Ref3DPxg)ptg);
  646. }
  647. if (ptg instanceof Area3DPtg) {
  648. return ec.getArea3DEval((Area3DPtg)ptg);
  649. }
  650. if (ptg instanceof Area3DPxg) {
  651. return ec.getArea3DEval((Area3DPxg)ptg);
  652. }
  653. if (ptg instanceof RefPtg) {
  654. RefPtg rptg = (RefPtg) ptg;
  655. return ec.getRefEval(rptg.getRow(), rptg.getColumn());
  656. }
  657. if (ptg instanceof AreaPtg) {
  658. AreaPtg aptg = (AreaPtg) ptg;
  659. return ec.getAreaEval(aptg.getFirstRow(), aptg.getFirstColumn(), aptg.getLastRow(), aptg.getLastColumn());
  660. }
  661. if (ptg instanceof ArrayPtg) {
  662. ArrayPtg aptg = (ArrayPtg) ptg;
  663. return ec.getAreaValueEval(0, 0, aptg.getRowCount() - 1, aptg.getColumnCount() - 1, aptg.getTokenArrayValues());
  664. }
  665. if (ptg instanceof UnknownPtg) {
  666. // POI uses UnknownPtg when the encoded Ptg array seems to be corrupted.
  667. // This seems to occur in very rare cases (e.g. unused name formulas in bug 44774, attachment 21790)
  668. // In any case, formulas are re-parsed before execution, so UnknownPtg should not get here
  669. throw new RuntimeException("UnknownPtg not allowed");
  670. }
  671. if (ptg instanceof ExpPtg) {
  672. // ExpPtg is used for array formulas and shared formulas.
  673. // it is currently unsupported, and may not even get implemented here
  674. throw new RuntimeException("ExpPtg currently not supported");
  675. }
  676. throw new RuntimeException("Unexpected ptg class (" + ptg.getClass().getName() + ")");
  677. }
  678. private ValueEval processNameEval(ValueEval eval, OperationEvaluationContext ec) {
  679. if (eval instanceof ExternalNameEval) {
  680. EvaluationName name = ((ExternalNameEval)eval).getName();
  681. return getEvalForNameRecord(name, ec);
  682. }
  683. return eval;
  684. }
  685. private ValueEval getEvalForNameRecord(EvaluationName nameRecord, OperationEvaluationContext ec) {
  686. if (nameRecord.isFunctionName()) {
  687. return new FunctionNameEval(nameRecord.getNameText());
  688. }
  689. if (nameRecord.hasFormula()) {
  690. return evaluateNameFormula(nameRecord.getNameDefinition(), ec);
  691. }
  692. throw new RuntimeException("Don't know how to evaluate name '" + nameRecord.getNameText() + "'");
  693. }
  694. /**
  695. * YK: Used by OperationEvaluationContext to resolve indirect names.
  696. */
  697. /*package*/ ValueEval evaluateNameFormula(Ptg[] ptgs, OperationEvaluationContext ec) {
  698. if (ptgs.length == 1 && !(ptgs[0] instanceof FuncVarPtg)) {
  699. return getEvalForPtg(ptgs[0], ec);
  700. }
  701. OperationEvaluationContext anyValueContext = new OperationEvaluationContext(this, ec.getWorkbook(), ec.getSheetIndex(), ec.getRowIndex(), ec.getColumnIndex(), new EvaluationTracker(_cache), false);
  702. return evaluateFormula(anyValueContext, ptgs);
  703. }
  704. /**
  705. * Used by the lazy ref evals whenever they need to get the value of a contained cell.
  706. */
  707. /* package */ ValueEval evaluateReference(
  708. EvaluationSheet sheet, int sheetIndex, int rowIndex,
  709. int columnIndex, EvaluationTracker tracker) {
  710. EvaluationCell cell = sheet.getCell(rowIndex, columnIndex);
  711. return evaluateAny(cell, sheetIndex, rowIndex, columnIndex, tracker);
  712. }
  713. public FreeRefFunction findUserDefinedFunction(String functionName) {
  714. return _udfFinder.findFunction(functionName);
  715. }
  716. /**
  717. * Evaluate a formula outside a cell value, e.g. conditional format rules or data validation expressions
  718. *
  719. * @param formula to evaluate
  720. * @param ref defines the optional sheet and row/column base for the formula, if it is relative
  721. * @return value
  722. */
  723. public ValueEval evaluate(String formula, CellReference ref) {
  724. final String sheetName = ref == null ? null : ref.getSheetName();
  725. int sheetIndex;
  726. if (sheetName == null) {
  727. sheetIndex = -1; // workbook scope only
  728. } else {
  729. sheetIndex = getWorkbook().getSheetIndex(sheetName);
  730. }
  731. int rowIndex = ref == null ? -1 : ref.getRow();
  732. short colIndex = ref == null ? -1 : ref.getCol();
  733. final OperationEvaluationContext ec = new OperationEvaluationContext(
  734. this,
  735. getWorkbook(),
  736. sheetIndex,
  737. rowIndex,
  738. colIndex,
  739. new EvaluationTracker(_cache)
  740. );
  741. Ptg[] ptgs = FormulaParser.parse(formula, (FormulaParsingWorkbook) getWorkbook(), FormulaType.CELL, sheetIndex, rowIndex);
  742. return evaluateNameFormula(ptgs, ec);
  743. }
  744. /**
  745. * Some expressions need to be evaluated in terms of an offset from the top left corner of a region,
  746. * such as some data validation and conditional format expressions, when those constraints apply
  747. * to contiguous cells. When a relative formula is used, it must be evaluated by shifting by the target
  748. * offset position relative to the top left of the range.
  749. * <p>
  750. * Returns a single value e.g. a cell formula result or boolean value for conditional formatting.
  751. *
  752. * @param formula The formula to evaluate
  753. * @param target cell context for the operation
  754. * @param region containing the cell
  755. * @return value
  756. * @throws IllegalArgumentException if target does not define a sheet name to evaluate the formula on.
  757. */
  758. public ValueEval evaluate(String formula, CellReference target, CellRangeAddressBase region) {
  759. return evaluate(formula, target, region, FormulaType.CELL);
  760. }
  761. /**
  762. * Some expressions need to be evaluated in terms of an offset from the top left corner of a region,
  763. * such as some data validation and conditional format expressions, when those constraints apply
  764. * to contiguous cells. When a relative formula is used, it must be evaluated by shifting by the target
  765. * offset position relative to the top left of the range.
  766. * <p>
  767. * Returns a ValueEval that may be one or more values, such as the allowed values for a data validation constraint.
  768. *
  769. * @param formula The formula to evaluate
  770. * @param target cell context for the operation
  771. * @param region containing the cell
  772. * @return ValueEval for one or more values
  773. * @throws IllegalArgumentException if target does not define a sheet name to evaluate the formula on.
  774. */
  775. public ValueEval evaluateList(String formula, CellReference target, CellRangeAddressBase region) {
  776. return evaluate(formula, target, region, FormulaType.DATAVALIDATION_LIST);
  777. }
  778. private ValueEval evaluate(String formula, CellReference target, CellRangeAddressBase region, FormulaType formulaType) {
  779. final String sheetName = target == null ? null : target.getSheetName();
  780. if (sheetName == null) throw new IllegalArgumentException("Sheet name is required");
  781. final int sheetIndex = getWorkbook().getSheetIndex(sheetName);
  782. Ptg[] ptgs = FormulaParser.parse(formula, (FormulaParsingWorkbook) getWorkbook(), formulaType, sheetIndex, target.getRow());
  783. adjustRegionRelativeReference(ptgs, target, region);
  784. final OperationEvaluationContext ec = new OperationEvaluationContext(this, getWorkbook(), sheetIndex, target.getRow(), target.getCol(), new EvaluationTracker(_cache), formulaType.isSingleValue());
  785. return evaluateNameFormula(ptgs, ec);
  786. }
  787. /**
  788. * Adjust formula relative references by the offset between the start of the given region and the given target cell.
  789. * That is, treat the region top-left cell as "A1" for the purposes of evaluating relative reference components (row and/or column),
  790. * and further move references by the position of the target within the region.
  791. * <p><pre>formula ref + range top-left + current cell range offset </pre></p>
  792. * which simplifies to
  793. * <p><pre>formula ref + current cell ref</pre></p>
  794. * @param ptgs
  795. * @param target cell within the region to use.
  796. * @param region containing the cell, OR, for conditional format rules with multiple ranges, the region with the top-left-most cell
  797. * @return true if any Ptg references were shifted
  798. * @throws IndexOutOfBoundsException if the resulting shifted row/column indexes are over the document format limits
  799. * @throws IllegalArgumentException if target is not within region.
  800. */
  801. protected boolean adjustRegionRelativeReference(Ptg[] ptgs, CellReference target, CellRangeAddressBase region) {
  802. // region may not be the one that contains the target, if a conditional formatting rule applies to multiple regions
  803. int deltaRow = target.getRow() - region.getFirstRow();
  804. int deltaColumn = target.getCol() - region.getFirstColumn();
  805. boolean shifted = false;
  806. for (Ptg ptg : ptgs) {
  807. // base class for cell reference "things"
  808. if (ptg instanceof RefPtgBase) {
  809. RefPtgBase ref = (RefPtgBase) ptg;
  810. // re-calculate cell references
  811. final SpreadsheetVersion version = _workbook.getSpreadsheetVersion();
  812. if (ref.isRowRelative() && deltaRow > 0) {
  813. final int rowIndex = ref.getRow() + deltaRow;
  814. if (rowIndex > version.getMaxRows()) {
  815. throw new IndexOutOfBoundsException(version.name() + " files can only have " + version.getMaxRows() + " rows, but row " + rowIndex + " was requested.");
  816. }
  817. ref.setRow(rowIndex);
  818. shifted = true;
  819. }
  820. if (ref.isColRelative() && deltaColumn > 0) {
  821. final int colIndex = ref.getColumn() + deltaColumn;
  822. if (colIndex > version.getMaxColumns()) {
  823. throw new IndexOutOfBoundsException(version.name() + " files can only have " + version.getMaxColumns() + " columns, but column " + colIndex + " was requested.");
  824. }
  825. ref.setColumn(colIndex);
  826. shifted = true;
  827. }
  828. }
  829. }
  830. return shifted;
  831. }
  832. /**
  833. * Whether to ignore missing references to external workbooks and
  834. * use cached formula results in the main workbook instead.
  835. * <p>
  836. * In some cases exetrnal workbooks referenced by formulas in the main workbook are not avaiable.
  837. * With this method you can control how POI handles such missing references:
  838. * <ul>
  839. * <li>by default ignoreMissingWorkbooks=false and POI throws {@link WorkbookNotFoundException}
  840. * if an external reference cannot be resolved</li>
  841. * <li>if ignoreMissingWorkbooks=true then POI uses cached formula result
  842. * that already exists in the main workbook</li>
  843. * </ul>
  844. *
  845. * @param ignore whether to ignore missing references to external workbooks
  846. * @see <a href="https://issues.apache.org/bugzilla/show_bug.cgi?id=52575">Bug 52575 for details</a>
  847. */
  848. public void setIgnoreMissingWorkbooks(boolean ignore){
  849. _ignoreMissingWorkbooks = ignore;
  850. }
  851. public boolean isIgnoreMissingWorkbooks(){
  852. return _ignoreMissingWorkbooks;
  853. }
  854. /**
  855. * Return a collection of functions that POI can evaluate
  856. *
  857. * @return names of functions supported by POI
  858. */
  859. public static Collection<String> getSupportedFunctionNames(){
  860. Collection<String> lst = new TreeSet<>();
  861. lst.addAll(FunctionEval.getSupportedFunctionNames());
  862. lst.addAll(AnalysisToolPak.getSupportedFunctionNames());
  863. return Collections.unmodifiableCollection(lst);
  864. }
  865. /**
  866. * Return a collection of functions that POI does not support
  867. *
  868. * @return names of functions NOT supported by POI
  869. */
  870. public static Collection<String> getNotSupportedFunctionNames(){
  871. Collection<String> lst = new TreeSet<>();
  872. lst.addAll(FunctionEval.getNotSupportedFunctionNames());
  873. lst.addAll(AnalysisToolPak.getNotSupportedFunctionNames());
  874. return Collections.unmodifiableCollection(lst);
  875. }
  876. /**
  877. * Register a ATP function in runtime.
  878. *
  879. * @param name the function name
  880. * @param func the functoin to register
  881. * @throws IllegalArgumentException if the function is unknown or already registered.
  882. * @since 3.8 beta6
  883. */
  884. public static void registerFunction(String name, FreeRefFunction func){
  885. AnalysisToolPak.registerFunction(name, func);
  886. }
  887. /**
  888. * Register a function in runtime.
  889. *
  890. * @param name the function name
  891. * @param func the functoin to register
  892. * @throws IllegalArgumentException if the function is unknown or already registered.
  893. * @since 3.8 beta6
  894. */
  895. public static void registerFunction(String name, Function func){
  896. FunctionEval.registerFunction(name, func);
  897. }
  898. public void setDebugEvaluationOutputForNextEval(boolean value){
  899. dbgEvaluationOutputForNextEval = value;
  900. }
  901. public boolean isDebugEvaluationOutputForNextEval(){
  902. return dbgEvaluationOutputForNextEval;
  903. }
  904. }