Add Workbook.setCellFormulaValidation to control whether formulas are validated during Cell.setCellFormula or not

This commit is contained in:
Travis Burtrum 2017-04-24 11:34:32 -04:00
parent f10fa7ac69
commit dbb6f07f29
5 changed files with 89 additions and 4 deletions

View File

@ -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:

View File

@ -686,4 +686,19 @@ public interface Workbook extends Closeable, Iterable<Sheet> {
* @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();
}

View File

@ -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
}

View File

@ -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);

View File

@ -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;
}
}