From 7ea956189d4de9bf38194c69839c6d17f95ed148 Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Tue, 29 Oct 2024 18:18:12 +0000 Subject: [PATCH] [bug-69418] Issue when evaluating WORKDAY function that has a cell ref as 2nd param git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1921651 13f79535-47bb-0310-9956-ffa450edef68 --- .../main/java/org/apache/poi/ss/formula/LazyRefEval.java | 8 ++++++++ .../org/apache/poi/ss/formula/eval/OperandResolver.java | 9 +++++++++ .../apache/poi/ss/formula/functions/TestWorkdayFunc.java | 2 +- 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/poi/src/main/java/org/apache/poi/ss/formula/LazyRefEval.java b/poi/src/main/java/org/apache/poi/ss/formula/LazyRefEval.java index 709756da66..9e81aa9b0e 100644 --- a/poi/src/main/java/org/apache/poi/ss/formula/LazyRefEval.java +++ b/poi/src/main/java/org/apache/poi/ss/formula/LazyRefEval.java @@ -39,6 +39,14 @@ public final class LazyRefEval extends RefEvalBase { return _evaluator.getEvalForCell(sheetIndex, getRow(), getColumn()); } + /** + * @return the Eval of the first sheet associated with this LazyRefEval + * @since POI 5.3.1 + */ + public ValueEval getInnerValueEvalForFirstSheet() { + return _evaluator.getEvalForCell(_evaluator.getFirstSheetIndex(), getRow(), getColumn()); + } + public AreaEval offset(int relFirstRowIx, int relLastRowIx, int relFirstColIx, int relLastColIx) { AreaI area = new OffsetArea(getRow(), getColumn(), diff --git a/poi/src/main/java/org/apache/poi/ss/formula/eval/OperandResolver.java b/poi/src/main/java/org/apache/poi/ss/formula/eval/OperandResolver.java index 710c9f7713..545db9fa36 100644 --- a/poi/src/main/java/org/apache/poi/ss/formula/eval/OperandResolver.java +++ b/poi/src/main/java/org/apache/poi/ss/formula/eval/OperandResolver.java @@ -18,6 +18,7 @@ package org.apache.poi.ss.formula.eval; import org.apache.poi.ss.formula.EvaluationCell; +import org.apache.poi.ss.formula.LazyRefEval; import org.apache.poi.ss.usermodel.DateUtil; import org.apache.poi.ss.util.CellRangeAddress; @@ -267,6 +268,14 @@ public final class OperandResolver { } return dd; } + if (ev instanceof LazyRefEval) { + final LazyRefEval lre = (LazyRefEval) ev; + final ValueEval innerValueEval = lre.getInnerValueEvalForFirstSheet(); + if (innerValueEval == ev) { + throw new IllegalStateException("Circular lazy reference " + lre); + } + return coerceValueToDouble(innerValueEval); + } throw new IllegalStateException("Unexpected arg eval type (" + ev.getClass().getName() + ")"); } diff --git a/poi/src/test/java/org/apache/poi/ss/formula/functions/TestWorkdayFunc.java b/poi/src/test/java/org/apache/poi/ss/formula/functions/TestWorkdayFunc.java index 96260443f6..63014ed734 100644 --- a/poi/src/test/java/org/apache/poi/ss/formula/functions/TestWorkdayFunc.java +++ b/poi/src/test/java/org/apache/poi/ss/formula/functions/TestWorkdayFunc.java @@ -46,7 +46,7 @@ class TestWorkdayFunc { cellB1.setCellValue(5); Cell cellResult = sheet.createRow(1).createCell(0); HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb); - assertDouble(fe, cellResult, "WORKDAY(A1,5)", DateUtil.getExcelDate(LocalDate.parse("2024-11-05"))); + assertDouble(fe, cellResult, "WORKDAY(A1,B1)", DateUtil.getExcelDate(LocalDate.parse("2024-11-05"))); } } } -- 2.39.5