From 930f67f99133c1ff8507f36252a4851bc0d0d23b Mon Sep 17 00:00:00 2001 From: moparisthebest Date: Mon, 24 Apr 2017 11:34:32 -0400 Subject: [PATCH] Add Workbook.setCellFormulaValidation to control whether formulas are validated during Cell.setCellFormula or not --- .../poi/hssf/usermodel/HSSFWorkbook.java | 21 +++++++++++++++ .../org/apache/poi/ss/usermodel/Workbook.java | 15 +++++++++++ .../poi/xssf/streaming/SXSSFWorkbook.java | 23 +++++++++++++++- .../apache/poi/xssf/usermodel/XSSFCell.java | 8 +++--- .../poi/xssf/usermodel/XSSFWorkbook.java | 26 +++++++++++++++++++ 5 files changed, 89 insertions(+), 4 deletions(-) diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java b/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java index 9acd3e468..a24bab7f0 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java @@ -2269,6 +2269,27 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss return recalc != null && recalc.getEngineId() != 0; } + /** + * Whether a call to {@link HSSFCell#setCellFormula(String)} will validate the formula or not. + * + * @param value true if the application will validate the formula is correct + * @since 3.17 + */ + @Override + public void setCellFormulaValidation(final boolean value) { + // currently {@link HSSFCell#setCellFormula(String)} does no validation anyway, ignore + } + + /** + * Whether a call to {@link HSSFCell#setCellFormula(String)} will validate the formula or not. + * + * @since 3.17 + */ + @Override + public boolean getCellFormulaValidation() { + return false; + } + /** * Changes an external referenced file to another file. * A formula in Excel which references a cell in another file is saved in two parts: diff --git a/src/java/org/apache/poi/ss/usermodel/Workbook.java b/src/java/org/apache/poi/ss/usermodel/Workbook.java index 49082536f..c456f64e1 100644 --- a/src/java/org/apache/poi/ss/usermodel/Workbook.java +++ b/src/java/org/apache/poi/ss/usermodel/Workbook.java @@ -686,4 +686,19 @@ public interface Workbook extends Closeable, Iterable { * @throws IOException if the object can't be embedded */ int addOlePackage(byte[] oleData, String label, String fileName, String command) throws IOException; + + /** + * Whether a call to {@link Cell#setCellFormula(String)} will validate the formula or not. + * + * @param value true if the application will validate the formula is correct + * @since 3.17 + */ + void setCellFormulaValidation(boolean value); + + /** + * Whether a call to {@link Cell#setCellFormula(String)} will validate the formula or not. + * + * @since 3.17 + */ + boolean getCellFormulaValidation(); } diff --git a/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFWorkbook.java b/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFWorkbook.java index 551bf405b..a5935e60f 100644 --- a/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFWorkbook.java +++ b/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFWorkbook.java @@ -1363,6 +1363,27 @@ public class SXSSFWorkbook implements Workbook { public int addOlePackage(byte[] oleData, String label, String fileName, String command) throws IOException { return _wb.addOlePackage(oleData, label, fileName, command); } - + + /** + * Whether a call to {@link SXSSFCell#setCellFormula(String)} will validate the formula or not. + * + * @param value true if the application will validate the formula is correct + * @since 3.17 + */ + @Override + public void setCellFormulaValidation(final boolean value) { + // currently {@link SXSSFCell#setCellFormula(String)} does no validation anyway, ignore + } + + /** + * Whether a call to {@link SXSSFCell#setCellFormula(String)} will validate the formula or not. + * + * @since 3.17 + */ + @Override + public boolean getCellFormulaValidation() { + return false; + } + //end of interface implementation } diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java index a39fd47bf..e6d423e53 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java @@ -561,9 +561,11 @@ public final class XSSFCell implements Cell { 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); diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java index fecad97bc..4ed9811d0 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java @@ -208,6 +208,11 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook { */ 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 */ @@ -2470,4 +2475,25 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook { 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 + */ + @Override + 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 + */ + @Override + public boolean getCellFormulaValidation() { + return this.cellFormulaValidation; + } }