]> source.dussan.org Git - poi.git/commitdiff
When evaluating a name in another workbook, need to use an OperationEvaluationContext...
authorNick Burch <nick@apache.org>
Thu, 24 Jul 2014 14:56:23 +0000 (14:56 +0000)
committerNick Burch <nick@apache.org>
Thu, 24 Jul 2014 14:56:23 +0000 (14:56 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1613154 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/ss/formula/OperationEvaluationContext.java
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFFormulaEvaluation.java

index ab8720822753818cb9b2d4d6ebf610ac610bb5e7..96eb89c9b397be392d98bd0b75d6201d0b1bffd0 100644 (file)
@@ -377,25 +377,31 @@ public final class OperationEvaluationContext {
        
     private ValueEval getExternalNameXEval(ExternalName externName, String workbookName) {
         try {
+            // Fetch the workbook this refers to, and the name as defined with that
             WorkbookEvaluator refWorkbookEvaluator = _bookEvaluator.getOtherWorkbookEvaluator(workbookName);
             EvaluationName evaluationName = refWorkbookEvaluator.getName(externName.getName(),externName.getIx()-1);
             if (evaluationName != null && evaluationName.hasFormula()){
                 if (evaluationName.getNameDefinition().length > 1) {
                     throw new RuntimeException("Complex name formulas not supported yet");
                 }
+                
+                // Need to evaluate the reference in the context of the other book
+                OperationEvaluationContext refWorkbookContext = new OperationEvaluationContext(
+                        refWorkbookEvaluator, refWorkbookEvaluator.getWorkbook(), -1, -1, -1, _tracker);
+                
                 Ptg ptg = evaluationName.getNameDefinition()[0];
                 if (ptg instanceof Ref3DPtg){
                     Ref3DPtg ref3D = (Ref3DPtg)ptg;
-                    int sheetIndex = refWorkbookEvaluator.getSheetIndexByExternIndex(ref3D.getExternSheetIndex());
-                    String sheetName = refWorkbookEvaluator.getSheetName(sheetIndex);
-                    SheetRefEvaluator sre = createExternSheetRefEvaluator(workbookName, sheetName);
-                    return new LazyRefEval(ref3D.getRow(), ref3D.getColumn(), sre);
+                    return refWorkbookContext.getRef3DEval(ref3D);
+                } else if (ptg instanceof Ref3DPxg){
+                    Ref3DPxg ref3D = (Ref3DPxg)ptg;
+                    return refWorkbookContext.getRef3DEval(ref3D);
                 } else if(ptg instanceof Area3DPtg){
                     Area3DPtg area3D = (Area3DPtg)ptg;
-                    int sheetIndex = refWorkbookEvaluator.getSheetIndexByExternIndex(area3D.getExternSheetIndex());
-                    String sheetName = refWorkbookEvaluator.getSheetName(sheetIndex);
-                    SheetRefEvaluator sre = createExternSheetRefEvaluator(workbookName, sheetName);
-                    return new LazyAreaEval(area3D.getFirstRow(), area3D.getFirstColumn(), area3D.getLastRow(), area3D.getLastColumn(), sre);
+                    return refWorkbookContext.getArea3DEval(area3D);
+                } else if(ptg instanceof Area3DPxg){
+                    Area3DPxg area3D = (Area3DPxg)ptg;
+                    return refWorkbookContext.getArea3DEval(area3D);
                 }
             }
             return ErrorEval.REF_INVALID;
index 3857b0d52cfffa645d2848e41aab34c7af65b53b..4100b5f9c2437a94fdf413939d487b39acb8ef4e 100644 (file)
@@ -156,9 +156,8 @@ public final class TestXSSFFormulaEvaluation extends BaseTestFormulaEvaluator {
         
         // Evaluate and check results
         assertEquals("\"Hello!\"",  evaluator.evaluate(cXSLX_cell).formatAsString());
-        // TODO Fix XSSF reference evaluations to work
-//        assertEquals("\"Test A1\"", evaluator.evaluate(cXSLX_sNR).formatAsString());
-//        assertEquals("142.0",   evaluator.evaluate(cXSLX_gNR).formatAsString());
+        assertEquals("\"Test A1\"", evaluator.evaluate(cXSLX_sNR).formatAsString());
+        assertEquals("142.0",   evaluator.evaluate(cXSLX_gNR).formatAsString());
 
         assertEquals("\"Hello!\"",  evaluator.evaluate(cXSL_cell).formatAsString());
         assertEquals("\"Test A1\"", evaluator.evaluate(cXSL_sNR).formatAsString());