From f7ed387745d7c9fe324f0be285a172277aa12a0c Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Thu, 21 Apr 2011 11:52:52 +0000 Subject: [PATCH] Add get/setForceFormulaRecalculation for XSSF, and promote the methods to the common usermodel Sheet, using sheetCalcPr fullCalcOnLoad="true" git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1095667 13f79535-47bb-0310-9956-ffa450edef68 --- src/documentation/content/xdocs/status.xml | 1 + .../org/apache/poi/ss/usermodel/Sheet.java | 15 ++++++++ .../apache/poi/xssf/usermodel/XSSFSheet.java | 37 +++++++++++++++++++ 3 files changed, 53 insertions(+) diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index 5a90ab94e0..d09d49371f 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,6 +34,7 @@ + Add get/setForceFormulaRecalculation for XSSF, and promote the methods to the common usermodel Sheet Tweak the logic for sizing the HSSFCells array on a HSSFRow to reduce memory over allocation in many use cases 49765 - Support for adding a picture to a XSSFRun Rename/Move xssf.model.Table to xssf.usermodel.XSSFTable as it now has usermodel-like features diff --git a/src/java/org/apache/poi/ss/usermodel/Sheet.java b/src/java/org/apache/poi/ss/usermodel/Sheet.java index b3eb1ee2a8..349c42cbdf 100644 --- a/src/java/org/apache/poi/ss/usermodel/Sheet.java +++ b/src/java/org/apache/poi/ss/usermodel/Sheet.java @@ -258,6 +258,21 @@ public interface Sheet extends Iterable { */ Iterator rowIterator(); + /** + * Control if Excel should be asked to recalculate all formulas when the + * workbook is opened. + * Calculating the formula values with {@link FormulaEvaluator} is the + * recommended solution, but this may be used for certain cases where + * evaluation in POI is not possible. + */ + void setForceFormulaRecalculation(boolean value); + + /** + * Whether Excel will be asked to recalculate all formulas when the + * workbook is opened. + */ + boolean getForceFormulaRecalculation(); + /** * Flag indicating whether the sheet displays Automatic Page Breaks. * diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java index 3d72c82f8f..244a9e063f 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java @@ -46,6 +46,7 @@ import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.DataValidation; import org.apache.poi.ss.usermodel.DataValidationHelper; import org.apache.poi.ss.usermodel.Footer; +import org.apache.poi.ss.usermodel.FormulaEvaluator; import org.apache.poi.ss.usermodel.Header; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; @@ -89,6 +90,7 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPrintOptions; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRow; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSelection; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheet; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheetCalcPr; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheetData; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheetFormatPr; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheetPr; @@ -1454,6 +1456,41 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { } } + /** + * Control if Excel should be asked to recalculate all formulas when the + * workbook is opened, via the "sheetCalcPr fullCalcOnLoad" option. + * Calculating the formula values with {@link FormulaEvaluator} is the + * recommended solution, but this may be used for certain cases where + * evaluation in POI is not possible. + */ + public void setForceFormulaRecalculation(boolean value) { + if(worksheet.isSetSheetCalcPr()) { + // Change the current setting + CTSheetCalcPr calc = worksheet.getSheetCalcPr(); + calc.setFullCalcOnLoad(value); + } + else if(value) { + // Add the Calc block and set it + CTSheetCalcPr calc = worksheet.addNewSheetCalcPr(); + calc.setFullCalcOnLoad(value); + } + else { + // Not set, requested not, nothing to do + } + } + + /** + * Whether Excel will be asked to recalculate all formulas when the + * workbook is opened. + */ + public boolean getForceFormulaRecalculation() { + if(worksheet.isSetSheetCalcPr()) { + CTSheetCalcPr calc = worksheet.getSheetCalcPr(); + return calc.getFullCalcOnLoad(); + } + return false; + } + /** * @return an iterator of the PHYSICAL rows. Meaning the 3rd element may not * be the third row if say for instance the second row is undefined. -- 2.39.5