]> source.dussan.org Git - poi.git/commitdiff
#56132 Allow XSSF formula evaluation to also skip missing external workbook reference...
authorNick Burch <nick@apache.org>
Wed, 30 Jul 2014 18:08:28 +0000 (18:08 +0000)
committerNick Burch <nick@apache.org>
Wed, 30 Jul 2014 18:08:28 +0000 (18:08 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1614729 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/hssf/usermodel/HSSFFormulaEvaluator.java
src/java/org/apache/poi/ss/usermodel/FormulaEvaluator.java
src/java/org/apache/poi/ss/util/SheetUtil.java
src/ooxml/java/org/apache/poi/xssf/model/ExternalLinksTable.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFFormulaEvaluator.java
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestMissingWorkbookOnXSSF.java [new file with mode: 0644]
src/testcases/org/apache/poi/ss/formula/TestMissingWorkbook.java

index 1e8098b9ef3bbfbebb4ee70b2f4bdad2ce449fef..e73903759c20289af2c1049ce97708e02c17bb5a 100644 (file)
@@ -378,21 +378,7 @@ public class HSSFFormulaEvaluator implements FormulaEvaluator, WorkbookEvaluator
                throw new RuntimeException("Unexpected eval class (" + eval.getClass().getName() + ")");
        }
 
-    /**
-     * Whether to ignore missing references to external workbooks and
-     * use cached formula results in the main workbook instead.
-     * <p>
-     * In some cases exetrnal workbooks referenced by formulas in the main workbook are not avaiable.
-     * With this method you can control how POI handles such missing references:
-     * <ul>
-     *     <li>by default ignoreMissingWorkbooks=false and POI throws {@link org.apache.poi.ss.formula.CollaboratingWorkbooksEnvironment.WorkbookNotFoundException}
-     *     if an external reference cannot be resolved</li>
-     *     <li>if ignoreMissingWorkbooks=true then POI uses cached formula result
-     *     that already exists in the main workbook</li>
-     * </ul>
-     *
-     * @param ignore whether to ignore missing references to external workbooks
-     */
+    /** {@inheritDoc} */
     public void setIgnoreMissingWorkbooks(boolean ignore){
         _bookEvaluator.setIgnoreMissingWorkbooks(ignore);
     }
index 911647bb0e681d1d02507bf94fa2bdaa424188c3..289a209873ab014e055b0e7ea5d814ad5bb7cb79 100644 (file)
@@ -127,6 +127,24 @@ public interface FormulaEvaluator {
      */
     void setupReferencedWorkbooks(Map<String,FormulaEvaluator> workbooks);
 
+    /**
+     * Whether to ignore missing references to external workbooks and
+     * use cached formula results in the main workbook instead.
+     * <p>
+     * In some cases external workbooks referenced by formulas in the main workbook are not available.
+     * With this method you can control how POI handles such missing references:
+     * <ul>
+     *     <li>by default ignoreMissingWorkbooks=false and POI throws 
+     *     {@link org.apache.poi.ss.formula.CollaboratingWorkbooksEnvironment.WorkbookNotFoundException}
+     *     if an external reference cannot be resolved</li>
+     *     <li>if ignoreMissingWorkbooks=true then POI uses cached formula result
+     *     that already exists in the main workbook</li>
+     * </ul>
+     *
+     * @param ignore whether to ignore missing references to external workbooks
+     */
+    void setIgnoreMissingWorkbooks(boolean ignore);
+    
     /**
      * Perform detailed output of formula evaluation for next evaluation only?
      * Is for developer use only (also developers using POI for their XLS files).
index 792ef31f69c4c89a995b8ba1c735b1c548634411..71c3c3cb40e4baaf2191971943c71d3eb96078bf 100644 (file)
@@ -73,13 +73,12 @@ public class SheetUtil {
         public Cell evaluateInCell(Cell cell) { return null; }
         public void setupReferencedWorkbooks(Map<String, FormulaEvaluator> workbooks) {}
         public void setDebugEvaluationOutputForNextEval(boolean value) {}
-
+        public void setIgnoreMissingWorkbooks(boolean ignore) {}
+        
         public void evaluateAll() {}
-
         public int evaluateFormulaCell(Cell cell) {
             return cell.getCachedFormulaResultType();
         }
-
     };
 
     /**
index 9d66b6fa48ce7a77b1cab24ee905999b055c3c6d..c11d51ef9d3b25fe05a0a94e7df48fd34a767058 100644 (file)
@@ -44,6 +44,7 @@ public class ExternalLinksTable extends POIXMLDocumentPart {
     public ExternalLinksTable() {
         super();
         link = CTExternalLink.Factory.newInstance();
+        link.addNewExternalBook();
     }
 
     public ExternalLinksTable(PackagePart part, PackageRelationship rel) throws IOException {
index 90091a146613fd99e3bb010f4e203dbfe6861b3d..bfa4c6d6787317fea6daaf39944d5810ea5fdb0e 100644 (file)
@@ -292,9 +292,13 @@ public class XSSFFormulaEvaluator implements FormulaEvaluator, WorkbookEvaluator
         return _bookEvaluator;
     }
 
+    /** {@inheritDoc} */
+    public void setIgnoreMissingWorkbooks(boolean ignore){
+        _bookEvaluator.setIgnoreMissingWorkbooks(ignore);
+    }
+
     /** {@inheritDoc} */
     public void setDebugEvaluationOutputForNextEval(boolean value){
         _bookEvaluator.setDebugEvaluationOutputForNextEval(value);
     }
-
 }
diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestMissingWorkbookOnXSSF.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestMissingWorkbookOnXSSF.java
new file mode 100644 (file)
index 0000000..2427dd1
--- /dev/null
@@ -0,0 +1,40 @@
+/* ====================================================================
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You under the Apache License, Version 2.0
+   (the "License"); you may not use this file except in compliance with
+   the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+==================================================================== */
+
+package org.apache.poi.xssf.usermodel;
+
+import org.apache.poi.hssf.HSSFTestDataSamples;
+import org.apache.poi.ss.formula.TestMissingWorkbook;
+import org.apache.poi.xssf.XSSFTestDataSamples;
+
+/**
+ * XSSF Specific version of the Missing Workbooks test
+ */
+public final class TestMissingWorkbookOnXSSF extends TestMissingWorkbook {
+    public TestMissingWorkbookOnXSSF() {
+        super("52575_main.xlsx", "source_dummy.xlsx", "52575_source.xls");
+    }
+    
+    @Override
+    protected void setUp() throws Exception {
+        mainWorkbook = XSSFTestDataSamples.openSampleWorkbook(MAIN_WORKBOOK_FILENAME);
+        sourceWorkbook = HSSFTestDataSamples.openSampleWorkbook(SOURCE_WORKBOOK_FILENAME);
+        
+        assertNotNull(mainWorkbook);
+        assertNotNull(sourceWorkbook);
+    }
+}
index 50ca5e2988325960e009462f385f3bf57fc48b4d..35bb050b1ae198acee30a7a839eb55485abccde6 100644 (file)
@@ -33,12 +33,22 @@ import org.apache.poi.ss.usermodel.Sheet;
 import org.apache.poi.ss.usermodel.Workbook;\r
 \r
 public class TestMissingWorkbook extends TestCase {\r
-       private static final String MAIN_WORKBOOK_FILENAME = "52575_main.xls";\r
-       private static final String SOURCE_DUMMY_WORKBOOK_FILENAME = "source_dummy.xls";\r
-       private static final String SOURCE_WORKBOOK_FILENAME = "52575_source.xls";\r
-       \r
-       private Workbook mainWorkbook;\r
-       private Workbook sourceWorkbook;\r
+    protected Workbook mainWorkbook;\r
+    protected Workbook sourceWorkbook;\r
+    \r
+    protected final String MAIN_WORKBOOK_FILENAME;\r
+    protected final String SOURCE_DUMMY_WORKBOOK_FILENAME;\r
+    protected final String SOURCE_WORKBOOK_FILENAME;\r
+    \r
+    public TestMissingWorkbook() {\r
+        this("52575_main.xls", "source_dummy.xls", "52575_source.xls");\r
+    }\r
+    protected TestMissingWorkbook(String MAIN_WORKBOOK_FILENAME, \r
+            String SOURCE_DUMMY_WORKBOOK_FILENAME, String SOURCE_WORKBOOK_FILENAME) {\r
+        this.MAIN_WORKBOOK_FILENAME = MAIN_WORKBOOK_FILENAME; \r
+        this.SOURCE_DUMMY_WORKBOOK_FILENAME = SOURCE_DUMMY_WORKBOOK_FILENAME;\r
+        this.SOURCE_WORKBOOK_FILENAME = SOURCE_WORKBOOK_FILENAME;\r
+    }\r
        \r
        @Override\r
        protected void setUp() throws Exception {\r
@@ -74,12 +84,18 @@ public class TestMissingWorkbook extends TestCase {
                assertEquals(Cell.CELL_TYPE_FORMULA, lA1Cell.getCellType());\r
                assertEquals(Cell.CELL_TYPE_FORMULA, lB1Cell.getCellType());\r
                assertEquals(Cell.CELL_TYPE_FORMULA, lC1Cell.getCellType());\r
+\r
+               // Check cached values\r
+        assertEquals(10.0d, lA1Cell.getNumericCellValue(), 0.00001d);\r
+        assertEquals("POI rocks!", lB1Cell.getStringCellValue());\r
+        assertEquals(true, lC1Cell.getBooleanCellValue());\r
                \r
+        // Evaluate\r
                FormulaEvaluator evaluator = mainWorkbook.getCreationHelper().createFormulaEvaluator();\r
         evaluator.setIgnoreMissingWorkbooks(true);\r
 \r
                assertEquals(Cell.CELL_TYPE_NUMERIC, evaluator.evaluateFormulaCell(lA1Cell));\r
-               assertEquals(Cell.CELL_TYPE_STRING, evaluator.evaluateFormulaCell(lB1Cell));\r
+               assertEquals(Cell.CELL_TYPE_STRING,  evaluator.evaluateFormulaCell(lB1Cell));\r
                assertEquals(Cell.CELL_TYPE_BOOLEAN, evaluator.evaluateFormulaCell(lC1Cell));\r
 \r
                assertEquals(10.0d, lA1Cell.getNumericCellValue(), 0.00001d);\r