Author: centic
Date: Wed Sep 20 19:08:57 2017
New Revision: 1809071

URL: http://svn.apache.org/viewvc?rev=1809071&view=rev
Log:
Apply patch from bug 61033 to add a 
XSSFWorkbook.setCellFormulaValidation(false) to speed up some operations

Modified:
    poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java
    poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java
    
poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCell.java

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java?rev=1809071&r1=1809070&r2=1809071&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java 
(original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java Wed 
Sep 20 19:08:57 2017
@@ -564,9 +564,11 @@ public final class XSSFCell implements C
             return;
         }
 
-        XSSFEvaluationWorkbook fpb = XSSFEvaluationWorkbook.create(wb);
-        //validate through the FormulaParser
-        FormulaParser.parse(formula, fpb, formulaType, 
wb.getSheetIndex(getSheet()), getRowIndex());
+        if(wb.getCellFormulaValidation()) {
+            XSSFEvaluationWorkbook fpb = XSSFEvaluationWorkbook.create(wb);
+            //validate through the FormulaParser
+            FormulaParser.parse(formula, fpb, formulaType, 
wb.getSheetIndex(getSheet()), getRowIndex());
+        }
 
         CTCellFormula f = CTCellFormula.Factory.newInstance();
         f.setStringValue(formula);

Modified: 
poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java?rev=1809071&r1=1809070&r2=1809071&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java 
(original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java 
Wed Sep 20 19:08:57 2017
@@ -213,6 +213,11 @@ public class XSSFWorkbook extends POIXML
     private MissingCellPolicy _missingCellPolicy = 
MissingCellPolicy.RETURN_NULL_AND_BLANK;
 
     /**
+     * Whether a call to {@link XSSFCell#setCellFormula(String)} will validate 
the formula or not.
+     */
+    private boolean cellFormulaValidation = true;
+
+    /**
      * array of pictures for this workbook
      */
     private List<XSSFPictureData> pictures;
@@ -2390,4 +2395,23 @@ public class XSSFWorkbook extends POIXML
 
         return oleId;
     }
+
+    /**
+     * Whether a call to {@link XSSFCell#setCellFormula(String)} will validate 
the formula or not.
+     *
+     * @param value true if the application will validate the formula is 
correct
+     * @since 3.17
+     */
+    public void setCellFormulaValidation(final boolean value) {
+        this.cellFormulaValidation = value;
+    }
+
+    /**
+     * Whether a call to {@link XSSFCell#setCellFormula(String)} will validate 
the formula or not.
+     *
+     * @since 3.17
+     */
+    public boolean getCellFormulaValidation() {
+        return this.cellFormulaValidation;
+    }
 }

Modified: 
poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCell.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCell.java?rev=1809071&r1=1809070&r2=1809071&view=diff
==============================================================================
--- 
poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCell.java 
(original)
+++ 
poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCell.java 
Wed Sep 20 19:08:57 2017
@@ -23,6 +23,7 @@ import static org.junit.Assert.assertNot
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertSame;
 import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertFalse;
 
 import java.io.IOException;
 import java.util.List;
@@ -30,6 +31,7 @@ import java.util.List;
 import org.apache.poi.common.usermodel.HyperlinkType;
 import org.apache.poi.hssf.HSSFITestDataProvider;
 import org.apache.poi.ss.SpreadsheetVersion;
+import org.apache.poi.ss.formula.FormulaParseException;
 import org.apache.poi.ss.usermodel.BaseTestXCell;
 import org.apache.poi.ss.usermodel.BorderStyle;
 import org.apache.poi.ss.usermodel.Cell;
@@ -179,6 +181,42 @@ public final class TestXSSFCell extends
             assertEquals(CellType.BLANK, cell.getCellType());
             assertEquals(STCellType.N, ctCell.getT());
             assertEquals("", cell.getStringCellValue());
+
+            // check behavior with setCellFormulaValidation
+            final String invalidFormula = "A", validFormula = "A2";
+            FormulaParseException fpe = null;
+            // check that default is true
+            assertTrue(wb.getCellFormulaValidation());
+
+            // check that valid formula does not throw exception
+            try {
+                cell.setCellFormula(validFormula);
+            } catch(FormulaParseException e) {
+                fpe = e;
+            }
+            assertNull(fpe);
+
+            // check that invalid formula does throw exception
+            try {
+                cell.setCellFormula(invalidFormula);
+            } catch(FormulaParseException e) {
+                fpe = e;
+            }
+            assertNotNull(fpe);
+            fpe = null;
+
+            // set cell formula validation to false
+            wb.setCellFormulaValidation(false);
+            assertFalse(wb.getCellFormulaValidation());
+
+            // check that neither valid nor invalid formula throw an exception
+            try {
+                cell.setCellFormula(validFormula);
+                cell.setCellFormula(invalidFormula);
+            } catch(FormulaParseException e) {
+                fpe = e;
+            }
+            assertNull(fpe);
         } finally {
             wb.close();
         }



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to