]> source.dussan.org Git - poi.git/commitdiff
[bug-69418] Issue when evaluating WORKDAY function that has a cell ref as 2nd param
authorPJ Fanning <fanningpj@apache.org>
Tue, 29 Oct 2024 18:18:12 +0000 (18:18 +0000)
committerPJ Fanning <fanningpj@apache.org>
Tue, 29 Oct 2024 18:18:12 +0000 (18:18 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1921651 13f79535-47bb-0310-9956-ffa450edef68

poi/src/main/java/org/apache/poi/ss/formula/LazyRefEval.java
poi/src/main/java/org/apache/poi/ss/formula/eval/OperandResolver.java
poi/src/test/java/org/apache/poi/ss/formula/functions/TestWorkdayFunc.java

index 709756da66a8c3a790886505f69fe966638e56ba..9e81aa9b0ec3c923a3dd4b6a077fe8a058b8e627 100644 (file)
@@ -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(),
index 710c9f771377488380a7e3bf3d5cc854295137cf..545db9fa369cbb86deeb7c3fd27332e2f9d22872 100644 (file)
@@ -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() + ")");
     }
 
index 96260443f63d2f3800c13ae958e580dc6e5ce29f..63014ed7342f37f0ed2a4cd40e384a0fca6cc451 100644 (file)
@@ -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")));
         }
     }
 }