You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by on...@apache.org on 2016/06/10 02:51:45 UTC
svn commit: r1747625 [2/2] - in /poi/branches/xssf_structured_references/src:
java/org/apache/poi/hssf/usermodel/ java/org/apache/poi/ss/formula/
java/org/apache/poi/ss/formula/functions/
ooxml/java/org/apache/poi/xssf/usermodel/ testcases/org/apache/p...
Modified: poi/branches/xssf_structured_references/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java
URL: http://svn.apache.org/viewvc/poi/branches/xssf_structured_references/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java?rev=1747625&r1=1747624&r2=1747625&view=diff
==============================================================================
--- poi/branches/xssf_structured_references/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java (original)
+++ poi/branches/xssf_structured_references/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java Fri Jun 10 02:51:45 2016
@@ -406,9 +406,9 @@ public final class XSSFCell implements C
if (cachedValueType != expectedTypeCode) {
throw typeMismatch(expectedTypeCode, cachedValueType, true);
}
- }
+ }
- /**
+ /**
* Set a string value for the cell.
*
* @param str value to set the cell to. For formulas we'll set the formula
@@ -925,8 +925,8 @@ public final class XSSFCell implements C
throw new IllegalArgumentException("Illegal cell type: " + cellType);
}
if (cellType != CELL_TYPE_FORMULA && _cell.isSetF()) {
- _cell.unsetF();
- }
+ _cell.unsetF();
+ }
}
/**
Modified: poi/branches/xssf_structured_references/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java
URL: http://svn.apache.org/viewvc/poi/branches/xssf_structured_references/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java?rev=1747625&r1=1747624&r2=1747625&view=diff
==============================================================================
--- poi/branches/xssf_structured_references/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java (original)
+++ poi/branches/xssf_structured_references/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java Fri Jun 10 02:51:45 2016
@@ -1762,7 +1762,7 @@ public class XSSFWorkbook extends POIXML
* Get the document's embedded files.
*/
@Override
- public List<PackagePart> getAllEmbedds() throws OpenXML4JException {
+ public List<PackagePart> getAllEmbedds() throws OpenXML4JException {
List<PackagePart> embedds = new LinkedList<PackagePart>();
for(XSSFSheet sheet : sheets){
@@ -1929,7 +1929,7 @@ public class XSSFWorkbook extends POIXML
*/
@Internal
public MapInfo getMapInfo(){
- return mapInfo;
+ return mapInfo;
}
/**
@@ -1946,92 +1946,92 @@ public class XSSFWorkbook extends POIXML
throw new RuntimeException("Not Implemented - see bug #57184");
}
- /**
- * Specifies a boolean value that indicates whether structure of workbook is locked. <br/>
- * A value true indicates the structure of the workbook is locked. Worksheets in the workbook can't be moved,
- * deleted, hidden, unhidden, or renamed, and new worksheets can't be inserted.<br/>
- * A value of false indicates the structure of the workbook is not locked.<br/>
- *
- * @return true if structure of workbook is locked
- */
- public boolean isStructureLocked() {
- return workbookProtectionPresent() && workbook.getWorkbookProtection().getLockStructure();
- }
-
- /**
- * Specifies a boolean value that indicates whether the windows that comprise the workbook are locked. <br/>
- * A value of true indicates the workbook windows are locked. Windows are the same size and position each time the
- * workbook is opened.<br/>
- * A value of false indicates the workbook windows are not locked.
- *
- * @return true if windows that comprise the workbook are locked
- */
- public boolean isWindowsLocked() {
- return workbookProtectionPresent() && workbook.getWorkbookProtection().getLockWindows();
- }
-
- /**
- * Specifies a boolean value that indicates whether the workbook is locked for revisions.
- *
- * @return true if the workbook is locked for revisions.
- */
- public boolean isRevisionLocked() {
- return workbookProtectionPresent() && workbook.getWorkbookProtection().getLockRevision();
- }
-
- /**
- * Locks the structure of workbook.
- */
- public void lockStructure() {
- safeGetWorkbookProtection().setLockStructure(true);
- }
-
- /**
- * Unlocks the structure of workbook.
- */
- public void unLockStructure() {
- safeGetWorkbookProtection().setLockStructure(false);
- }
-
- /**
- * Locks the windows that comprise the workbook.
- */
- public void lockWindows() {
- safeGetWorkbookProtection().setLockWindows(true);
- }
-
- /**
- * Unlocks the windows that comprise the workbook.
- */
- public void unLockWindows() {
- safeGetWorkbookProtection().setLockWindows(false);
- }
-
- /**
- * Locks the workbook for revisions.
- */
- public void lockRevision() {
- safeGetWorkbookProtection().setLockRevision(true);
- }
-
- /**
- * Unlocks the workbook for revisions.
- */
- public void unLockRevision() {
- safeGetWorkbookProtection().setLockRevision(false);
- }
-
- /**
- * Sets the workbook password.
- *
- * @param password if null, the password will be removed
- * @param hashAlgo if null, the password will be set as XOR password (Excel 2010 and earlier)
- * otherwise the given algorithm is used for calculating the hash password (Excel 2013)
- */
- public void setWorkbookPassword(String password, HashAlgorithm hashAlgo) {
+ /**
+ * Specifies a boolean value that indicates whether structure of workbook is locked. <br/>
+ * A value true indicates the structure of the workbook is locked. Worksheets in the workbook can't be moved,
+ * deleted, hidden, unhidden, or renamed, and new worksheets can't be inserted.<br/>
+ * A value of false indicates the structure of the workbook is not locked.<br/>
+ *
+ * @return true if structure of workbook is locked
+ */
+ public boolean isStructureLocked() {
+ return workbookProtectionPresent() && workbook.getWorkbookProtection().getLockStructure();
+ }
+
+ /**
+ * Specifies a boolean value that indicates whether the windows that comprise the workbook are locked. <br/>
+ * A value of true indicates the workbook windows are locked. Windows are the same size and position each time the
+ * workbook is opened.<br/>
+ * A value of false indicates the workbook windows are not locked.
+ *
+ * @return true if windows that comprise the workbook are locked
+ */
+ public boolean isWindowsLocked() {
+ return workbookProtectionPresent() && workbook.getWorkbookProtection().getLockWindows();
+ }
+
+ /**
+ * Specifies a boolean value that indicates whether the workbook is locked for revisions.
+ *
+ * @return true if the workbook is locked for revisions.
+ */
+ public boolean isRevisionLocked() {
+ return workbookProtectionPresent() && workbook.getWorkbookProtection().getLockRevision();
+ }
+
+ /**
+ * Locks the structure of workbook.
+ */
+ public void lockStructure() {
+ safeGetWorkbookProtection().setLockStructure(true);
+ }
+
+ /**
+ * Unlocks the structure of workbook.
+ */
+ public void unLockStructure() {
+ safeGetWorkbookProtection().setLockStructure(false);
+ }
+
+ /**
+ * Locks the windows that comprise the workbook.
+ */
+ public void lockWindows() {
+ safeGetWorkbookProtection().setLockWindows(true);
+ }
+
+ /**
+ * Unlocks the windows that comprise the workbook.
+ */
+ public void unLockWindows() {
+ safeGetWorkbookProtection().setLockWindows(false);
+ }
+
+ /**
+ * Locks the workbook for revisions.
+ */
+ public void lockRevision() {
+ safeGetWorkbookProtection().setLockRevision(true);
+ }
+
+ /**
+ * Unlocks the workbook for revisions.
+ */
+ public void unLockRevision() {
+ safeGetWorkbookProtection().setLockRevision(false);
+ }
+
+ /**
+ * Sets the workbook password.
+ *
+ * @param password if null, the password will be removed
+ * @param hashAlgo if null, the password will be set as XOR password (Excel 2010 and earlier)
+ * otherwise the given algorithm is used for calculating the hash password (Excel 2013)
+ */
+ public void setWorkbookPassword(String password, HashAlgorithm hashAlgo) {
if (password == null && !workbookProtectionPresent()) return;
setPassword(safeGetWorkbookProtection(), password, hashAlgo, "workbook");
- }
+ }
/**
* Validate the password against the stored hash, the hashing method will be determined
@@ -2074,9 +2074,9 @@ public class XSSFWorkbook extends POIXML
}
}
- private boolean workbookProtectionPresent() {
- return workbook.isSetWorkbookProtection();
- }
+ private boolean workbookProtectionPresent() {
+ return workbook.isSetWorkbookProtection();
+ }
private CTWorkbookProtection safeGetWorkbookProtection() {
if (!workbookProtectionPresent()){
@@ -2084,7 +2084,7 @@ public class XSSFWorkbook extends POIXML
}
return workbook.getWorkbookProtection();
}
-
+
/**
*
* Returns the locator of user-defined functions.
Modified: poi/branches/xssf_structured_references/src/testcases/org/apache/poi/hssf/record/TestSharedFormulaRecord.java
URL: http://svn.apache.org/viewvc/poi/branches/xssf_structured_references/src/testcases/org/apache/poi/hssf/record/TestSharedFormulaRecord.java?rev=1747625&r1=1747624&r2=1747625&view=diff
==============================================================================
--- poi/branches/xssf_structured_references/src/testcases/org/apache/poi/hssf/record/TestSharedFormulaRecord.java (original)
+++ poi/branches/xssf_structured_references/src/testcases/org/apache/poi/hssf/record/TestSharedFormulaRecord.java Fri Jun 10 02:51:45 2016
@@ -38,67 +38,67 @@ import org.apache.poi.util.LittleEndianI
*/
public final class TestSharedFormulaRecord extends TestCase {
- /**
- * A sample spreadsheet known to have one sheet with 4 shared formula ranges
- */
- private static final String SHARED_FORMULA_TEST_XLS = "SharedFormulaTest.xls";
- /**
- * Binary data for an encoded formula. Taken from attachment 22062 (bugzilla 45123/45421).
- * The shared formula is in Sheet1!C6:C21, with text "SUMPRODUCT(--(End_Acct=$C6),--(End_Bal))"
- * This data is found at offset 0x1A4A (within the shared formula record).
- * The critical thing about this formula is that it contains shared formula tokens (tRefN*,
- * tAreaN*) with operand class 'array'.
- */
- private static final byte[] SHARED_FORMULA_WITH_REF_ARRAYS_DATA = {
- 0x1A, 0x00,
- 0x63, 0x02, 0x00, 0x00, 0x00,
- 0x6C, 0x00, 0x00, 0x02, (byte)0x80, // tRefNA
- 0x0B,
- 0x15,
- 0x13,
- 0x13,
- 0x63, 0x03, 0x00, 0x00, 0x00,
- 0x15,
- 0x13,
- 0x13,
- 0x42, 0x02, (byte)0xE4, 0x00,
- };
-
- /**
- * The method <tt>SharedFormulaRecord.convertSharedFormulas()</tt> converts formulas from
- * 'shared formula' to 'single cell formula' format. It is important that token operand
- * classes are preserved during this transformation, because Excel may not tolerate the
- * incorrect encoding. The formula here is one such example (Excel displays #VALUE!).
- */
- public void testConvertSharedFormulasOperandClasses_bug45123() {
-
- LittleEndianInput in = TestcaseRecordInputStream.createLittleEndian(SHARED_FORMULA_WITH_REF_ARRAYS_DATA);
- int encodedLen = in.readUShort();
- Ptg[] sharedFormula = Ptg.readTokens(encodedLen, in);
+ /**
+ * A sample spreadsheet known to have one sheet with 4 shared formula ranges
+ */
+ private static final String SHARED_FORMULA_TEST_XLS = "SharedFormulaTest.xls";
+ /**
+ * Binary data for an encoded formula. Taken from attachment 22062 (bugzilla 45123/45421).
+ * The shared formula is in Sheet1!C6:C21, with text "SUMPRODUCT(--(End_Acct=$C6),--(End_Bal))"
+ * This data is found at offset 0x1A4A (within the shared formula record).
+ * The critical thing about this formula is that it contains shared formula tokens (tRefN*,
+ * tAreaN*) with operand class 'array'.
+ */
+ private static final byte[] SHARED_FORMULA_WITH_REF_ARRAYS_DATA = {
+ 0x1A, 0x00,
+ 0x63, 0x02, 0x00, 0x00, 0x00,
+ 0x6C, 0x00, 0x00, 0x02, (byte)0x80, // tRefNA
+ 0x0B,
+ 0x15,
+ 0x13,
+ 0x13,
+ 0x63, 0x03, 0x00, 0x00, 0x00,
+ 0x15,
+ 0x13,
+ 0x13,
+ 0x42, 0x02, (byte)0xE4, 0x00,
+ };
+
+ /**
+ * The method <tt>SharedFormulaRecord.convertSharedFormulas()</tt> converts formulas from
+ * 'shared formula' to 'single cell formula' format. It is important that token operand
+ * classes are preserved during this transformation, because Excel may not tolerate the
+ * incorrect encoding. The formula here is one such example (Excel displays #VALUE!).
+ */
+ public void testConvertSharedFormulasOperandClasses_bug45123() {
+
+ LittleEndianInput in = TestcaseRecordInputStream.createLittleEndian(SHARED_FORMULA_WITH_REF_ARRAYS_DATA);
+ int encodedLen = in.readUShort();
+ Ptg[] sharedFormula = Ptg.readTokens(encodedLen, in);
SharedFormula sf = new SharedFormula(SpreadsheetVersion.EXCEL97);
- Ptg[] convertedFormula = sf.convertSharedFormulas(sharedFormula, 100, 200);
+ Ptg[] convertedFormula = sf.convertSharedFormulas(sharedFormula, 100, 200);
+
+ RefPtg refPtg = (RefPtg) convertedFormula[1];
+ assertEquals("$C101", refPtg.toFormulaString());
+ if (refPtg.getPtgClass() == Ptg.CLASS_REF) {
+ throw new AssertionFailedError("Identified bug 45123");
+ }
- RefPtg refPtg = (RefPtg) convertedFormula[1];
- assertEquals("$C101", refPtg.toFormulaString());
- if (refPtg.getPtgClass() == Ptg.CLASS_REF) {
- throw new AssertionFailedError("Identified bug 45123");
- }
-
- confirmOperandClasses(sharedFormula, convertedFormula);
- }
-
- private static void confirmOperandClasses(Ptg[] originalPtgs, Ptg[] convertedPtgs) {
- assertEquals(originalPtgs.length, convertedPtgs.length);
- for (int i = 0; i < convertedPtgs.length; i++) {
- Ptg originalPtg = originalPtgs[i];
- Ptg convertedPtg = convertedPtgs[i];
- if (originalPtg.getPtgClass() != convertedPtg.getPtgClass()) {
- throw new ComparisonFailure("Different operand class for token[" + i + "]",
- String.valueOf(originalPtg.getPtgClass()), String.valueOf(convertedPtg.getPtgClass()));
- }
- }
- }
+ confirmOperandClasses(sharedFormula, convertedFormula);
+ }
+
+ private static void confirmOperandClasses(Ptg[] originalPtgs, Ptg[] convertedPtgs) {
+ assertEquals(originalPtgs.length, convertedPtgs.length);
+ for (int i = 0; i < convertedPtgs.length; i++) {
+ Ptg originalPtg = originalPtgs[i];
+ Ptg convertedPtg = convertedPtgs[i];
+ if (originalPtg.getPtgClass() != convertedPtg.getPtgClass()) {
+ throw new ComparisonFailure("Different operand class for token[" + i + "]",
+ String.valueOf(originalPtg.getPtgClass()), String.valueOf(convertedPtg.getPtgClass()));
+ }
+ }
+ }
public void testConvertSharedFormulas() {
HSSFWorkbook wb = new HSSFWorkbook();
@@ -138,111 +138,111 @@ public final class TestSharedFormulaReco
}
/**
- * Make sure that POI preserves {@link SharedFormulaRecord}s
- */
- public void testPreserveOnReserialize() {
- HSSFWorkbook wb;
- HSSFSheet sheet;
- HSSFCell cellB32769;
- HSSFCell cellC32769;
-
- // Reading directly from XLS file
- wb = HSSFTestDataSamples.openSampleWorkbook(SHARED_FORMULA_TEST_XLS);
- sheet = wb.getSheetAt(0);
- cellB32769 = sheet.getRow(32768).getCell(1);
- cellC32769 = sheet.getRow(32768).getCell(2);
- // check reading of formulas which are shared (two cells from a 1R x 8C range)
- assertEquals("B32770*2", cellB32769.getCellFormula());
- assertEquals("C32770*2", cellC32769.getCellFormula());
- confirmCellEvaluation(wb, cellB32769, 4);
- confirmCellEvaluation(wb, cellC32769, 6);
- // Confirm this example really does have SharedFormulas.
- // there are 3 others besides the one at A32769:H32769
- assertEquals(4, countSharedFormulas(sheet));
-
-
- // Re-serialize and check again
- wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
- sheet = wb.getSheetAt(0);
- cellB32769 = sheet.getRow(32768).getCell(1);
- cellC32769 = sheet.getRow(32768).getCell(2);
- assertEquals("B32770*2", cellB32769.getCellFormula());
- confirmCellEvaluation(wb, cellB32769, 4);
- assertEquals(4, countSharedFormulas(sheet));
- }
-
- public void testUnshareFormulaDueToChangeFormula() {
- HSSFWorkbook wb;
- HSSFSheet sheet;
- HSSFCell cellB32769;
- HSSFCell cellC32769;
-
- wb = HSSFTestDataSamples.openSampleWorkbook(SHARED_FORMULA_TEST_XLS);
- sheet = wb.getSheetAt(0);
- cellB32769 = sheet.getRow(32768).getCell(1);
- cellC32769 = sheet.getRow(32768).getCell(2);
-
- // Updating cell formula, causing it to become unshared
- cellB32769.setCellFormula("1+1");
- confirmCellEvaluation(wb, cellB32769, 2);
- // currently (Oct 2008) POI handles this by exploding the whole shared formula group
- assertEquals(3, countSharedFormulas(sheet)); // one less now
- // check that nearby cell of the same group still has the same formula
- assertEquals("C32770*2", cellC32769.getCellFormula());
- confirmCellEvaluation(wb, cellC32769, 6);
- }
- public void testUnshareFormulaDueToDelete() {
- HSSFWorkbook wb;
- HSSFSheet sheet;
- HSSFCell cell;
- final int ROW_IX = 2;
-
- // changing shared formula cell to blank
- wb = HSSFTestDataSamples.openSampleWorkbook(SHARED_FORMULA_TEST_XLS);
- sheet = wb.getSheetAt(0);
-
- assertEquals("A$1*2", sheet.getRow(ROW_IX).getCell(1).getCellFormula());
- cell = sheet.getRow(ROW_IX).getCell(1);
- cell.setCellType(HSSFCell.CELL_TYPE_BLANK);
- assertEquals(3, countSharedFormulas(sheet));
-
- wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
- sheet = wb.getSheetAt(0);
- assertEquals("A$1*2", sheet.getRow(ROW_IX+1).getCell(1).getCellFormula());
-
- // deleting shared formula cell
- wb = HSSFTestDataSamples.openSampleWorkbook(SHARED_FORMULA_TEST_XLS);
- sheet = wb.getSheetAt(0);
-
- assertEquals("A$1*2", sheet.getRow(ROW_IX).getCell(1).getCellFormula());
- cell = sheet.getRow(ROW_IX).getCell(1);
- sheet.getRow(ROW_IX).removeCell(cell);
- assertEquals(3, countSharedFormulas(sheet));
-
- wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
- sheet = wb.getSheetAt(0);
- assertEquals("A$1*2", sheet.getRow(ROW_IX+1).getCell(1).getCellFormula());
- }
-
- private static void confirmCellEvaluation(HSSFWorkbook wb, HSSFCell cell, double expectedValue) {
- HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
- CellValue cv = fe.evaluate(cell);
- assertEquals(HSSFCell.CELL_TYPE_NUMERIC, cv.getCellType());
- assertEquals(expectedValue, cv.getNumberValue(), 0.0);
- }
-
- /**
- * @return the number of {@link SharedFormulaRecord}s encoded for the specified sheet
- */
- private static int countSharedFormulas(HSSFSheet sheet) {
- Record[] records = RecordInspector.getRecords(sheet, 0);
- int count = 0;
- for (int i = 0; i < records.length; i++) {
- Record rec = records[i];
- if(rec instanceof SharedFormulaRecord) {
- count++;
- }
- }
- return count;
- }
+ * Make sure that POI preserves {@link SharedFormulaRecord}s
+ */
+ public void testPreserveOnReserialize() {
+ HSSFWorkbook wb;
+ HSSFSheet sheet;
+ HSSFCell cellB32769;
+ HSSFCell cellC32769;
+
+ // Reading directly from XLS file
+ wb = HSSFTestDataSamples.openSampleWorkbook(SHARED_FORMULA_TEST_XLS);
+ sheet = wb.getSheetAt(0);
+ cellB32769 = sheet.getRow(32768).getCell(1);
+ cellC32769 = sheet.getRow(32768).getCell(2);
+ // check reading of formulas which are shared (two cells from a 1R x 8C range)
+ assertEquals("B32770*2", cellB32769.getCellFormula());
+ assertEquals("C32770*2", cellC32769.getCellFormula());
+ confirmCellEvaluation(wb, cellB32769, 4);
+ confirmCellEvaluation(wb, cellC32769, 6);
+ // Confirm this example really does have SharedFormulas.
+ // there are 3 others besides the one at A32769:H32769
+ assertEquals(4, countSharedFormulas(sheet));
+
+
+ // Re-serialize and check again
+ wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
+ sheet = wb.getSheetAt(0);
+ cellB32769 = sheet.getRow(32768).getCell(1);
+ cellC32769 = sheet.getRow(32768).getCell(2);
+ assertEquals("B32770*2", cellB32769.getCellFormula());
+ confirmCellEvaluation(wb, cellB32769, 4);
+ assertEquals(4, countSharedFormulas(sheet));
+ }
+
+ public void testUnshareFormulaDueToChangeFormula() {
+ HSSFWorkbook wb;
+ HSSFSheet sheet;
+ HSSFCell cellB32769;
+ HSSFCell cellC32769;
+
+ wb = HSSFTestDataSamples.openSampleWorkbook(SHARED_FORMULA_TEST_XLS);
+ sheet = wb.getSheetAt(0);
+ cellB32769 = sheet.getRow(32768).getCell(1);
+ cellC32769 = sheet.getRow(32768).getCell(2);
+
+ // Updating cell formula, causing it to become unshared
+ cellB32769.setCellFormula("1+1");
+ confirmCellEvaluation(wb, cellB32769, 2);
+ // currently (Oct 2008) POI handles this by exploding the whole shared formula group
+ assertEquals(3, countSharedFormulas(sheet)); // one less now
+ // check that nearby cell of the same group still has the same formula
+ assertEquals("C32770*2", cellC32769.getCellFormula());
+ confirmCellEvaluation(wb, cellC32769, 6);
+ }
+ public void testUnshareFormulaDueToDelete() {
+ HSSFWorkbook wb;
+ HSSFSheet sheet;
+ HSSFCell cell;
+ final int ROW_IX = 2;
+
+ // changing shared formula cell to blank
+ wb = HSSFTestDataSamples.openSampleWorkbook(SHARED_FORMULA_TEST_XLS);
+ sheet = wb.getSheetAt(0);
+
+ assertEquals("A$1*2", sheet.getRow(ROW_IX).getCell(1).getCellFormula());
+ cell = sheet.getRow(ROW_IX).getCell(1);
+ cell.setCellType(HSSFCell.CELL_TYPE_BLANK);
+ assertEquals(3, countSharedFormulas(sheet));
+
+ wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
+ sheet = wb.getSheetAt(0);
+ assertEquals("A$1*2", sheet.getRow(ROW_IX+1).getCell(1).getCellFormula());
+
+ // deleting shared formula cell
+ wb = HSSFTestDataSamples.openSampleWorkbook(SHARED_FORMULA_TEST_XLS);
+ sheet = wb.getSheetAt(0);
+
+ assertEquals("A$1*2", sheet.getRow(ROW_IX).getCell(1).getCellFormula());
+ cell = sheet.getRow(ROW_IX).getCell(1);
+ sheet.getRow(ROW_IX).removeCell(cell);
+ assertEquals(3, countSharedFormulas(sheet));
+
+ wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
+ sheet = wb.getSheetAt(0);
+ assertEquals("A$1*2", sheet.getRow(ROW_IX+1).getCell(1).getCellFormula());
+ }
+
+ private static void confirmCellEvaluation(HSSFWorkbook wb, HSSFCell cell, double expectedValue) {
+ HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
+ CellValue cv = fe.evaluate(cell);
+ assertEquals(HSSFCell.CELL_TYPE_NUMERIC, cv.getCellType());
+ assertEquals(expectedValue, cv.getNumberValue(), 0.0);
+ }
+
+ /**
+ * @return the number of {@link SharedFormulaRecord}s encoded for the specified sheet
+ */
+ private static int countSharedFormulas(HSSFSheet sheet) {
+ Record[] records = RecordInspector.getRecords(sheet, 0);
+ int count = 0;
+ for (int i = 0; i < records.length; i++) {
+ Record rec = records[i];
+ if(rec instanceof SharedFormulaRecord) {
+ count++;
+ }
+ }
+ return count;
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org