Search in sources :

Example 1 with SharedFormulaRecord

use of org.apache.poi.hssf.record.SharedFormulaRecord in project poi by apache.

the class TestValueRecordsAggregate method testSharedFormula.

/**
	 * Make sure the shared formula DOESNT makes it to the FormulaRecordAggregate when being parsed
	 * as part of the value records
	 */
@Test
public void testSharedFormula() {
    List<Record> records = new ArrayList<Record>();
    records.add(new FormulaRecord());
    records.add(new SharedFormulaRecord());
    records.add(new WindowTwoRecord());
    constructValueRecord(records);
    List<CellValueRecordInterface> cvrs = getValueRecords();
    //Ensure that the SharedFormulaRecord has been converted
    assertEquals(1, cvrs.size());
    CellValueRecordInterface record = cvrs.get(0);
    assertNotNull("Row contains a value", record);
    assertTrue("First record is a FormulaRecordsAggregate", (record instanceof FormulaRecordAggregate));
}
Also used : SharedFormulaRecord(org.apache.poi.hssf.record.SharedFormulaRecord) FormulaRecord(org.apache.poi.hssf.record.FormulaRecord) CellValueRecordInterface(org.apache.poi.hssf.record.CellValueRecordInterface) ArrayList(java.util.ArrayList) SharedFormulaRecord(org.apache.poi.hssf.record.SharedFormulaRecord) Record(org.apache.poi.hssf.record.Record) MulBlankRecord(org.apache.poi.hssf.record.MulBlankRecord) SharedFormulaRecord(org.apache.poi.hssf.record.SharedFormulaRecord) WindowTwoRecord(org.apache.poi.hssf.record.WindowTwoRecord) BlankRecord(org.apache.poi.hssf.record.BlankRecord) FormulaRecord(org.apache.poi.hssf.record.FormulaRecord) WindowTwoRecord(org.apache.poi.hssf.record.WindowTwoRecord) Test(org.junit.Test)

Example 2 with SharedFormulaRecord

use of org.apache.poi.hssf.record.SharedFormulaRecord in project poi by apache.

the class TestMissingRecordAwareHSSFListener method testEndOfRow_bug45672.

/**
	 * Make sure that the presence of shared formulas does not cause extra 
	 * end-of-row records.
	 */
public void testEndOfRow_bug45672() {
    readRecords("ex45672.xls");
    Record[] rr = r;
    int eorCount = 0;
    int sfrCount = 0;
    for (Record record : rr) {
        if (record instanceof SharedFormulaRecord) {
            sfrCount++;
        }
        if (record instanceof LastCellOfRowDummyRecord) {
            eorCount++;
        }
    }
    if (eorCount == 2) {
        throw new AssertionFailedError("Identified bug 45672");
    }
    assertEquals(1, eorCount);
    assertEquals(1, sfrCount);
}
Also used : LastCellOfRowDummyRecord(org.apache.poi.hssf.eventusermodel.dummyrecord.LastCellOfRowDummyRecord) SharedFormulaRecord(org.apache.poi.hssf.record.SharedFormulaRecord) Record(org.apache.poi.hssf.record.Record) NumberRecord(org.apache.poi.hssf.record.NumberRecord) StringRecord(org.apache.poi.hssf.record.StringRecord) MissingRowDummyRecord(org.apache.poi.hssf.eventusermodel.dummyrecord.MissingRowDummyRecord) MissingCellDummyRecord(org.apache.poi.hssf.eventusermodel.dummyrecord.MissingCellDummyRecord) LastCellOfRowDummyRecord(org.apache.poi.hssf.eventusermodel.dummyrecord.LastCellOfRowDummyRecord) LabelSSTRecord(org.apache.poi.hssf.record.LabelSSTRecord) MulBlankRecord(org.apache.poi.hssf.record.MulBlankRecord) RowRecord(org.apache.poi.hssf.record.RowRecord) BOFRecord(org.apache.poi.hssf.record.BOFRecord) SharedFormulaRecord(org.apache.poi.hssf.record.SharedFormulaRecord) DimensionsRecord(org.apache.poi.hssf.record.DimensionsRecord) WindowTwoRecord(org.apache.poi.hssf.record.WindowTwoRecord) BlankRecord(org.apache.poi.hssf.record.BlankRecord) FormulaRecord(org.apache.poi.hssf.record.FormulaRecord) AssertionFailedError(junit.framework.AssertionFailedError)

Example 3 with SharedFormulaRecord

use of org.apache.poi.hssf.record.SharedFormulaRecord in project poi by apache.

the class TestRowRecordsAggregate method testArraysAndTables.

/**
	 * Prior to Aug 2008, POI would re-serialize spreadsheets with {@link ArrayRecord}s or
	 * {@link TableRecord}s with those records out of order.  Similar to
	 * {@link SharedFormulaRecord}s, these records should appear immediately after the first
	 * {@link FormulaRecord}s that they apply to (and only once).<br/>
	 */
@Test
public void testArraysAndTables() throws Exception {
    HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("testArraysAndTables.xls");
    Record[] sheetRecs = RecordInspector.getRecords(wb.getSheetAt(0), 0);
    int countArrayFormulas = verifySharedValues(sheetRecs, ArrayRecord.class);
    assertEquals(5, countArrayFormulas);
    int countTableFormulas = verifySharedValues(sheetRecs, TableRecord.class);
    assertEquals(3, countTableFormulas);
    // Note - SharedFormulaRecords are currently not re-serialized by POI (each is extracted
    // into many non-shared formulas), but if they ever were, the same rules would apply.
    int countSharedFormulas = verifySharedValues(sheetRecs, SharedFormulaRecord.class);
    assertEquals(0, countSharedFormulas);
    //		if (false) { // set true to observe re-serialized file
    //			File f = new File(System.getProperty("java.io.tmpdir") + "/testArraysAndTables-out.xls");
    //			try {
    //				OutputStream os = new FileOutputStream(f);
    //				wb.write(os);
    //				os.close();
    //			} catch (IOException e) {
    //				throw new RuntimeException(e);
    //			}
    //			System.out.println("Output file to " + f.getAbsolutePath());
    //		}
    wb.close();
}
Also used : Record(org.apache.poi.hssf.record.Record) NumberRecord(org.apache.poi.hssf.record.NumberRecord) UnknownRecord(org.apache.poi.hssf.record.UnknownRecord) ArrayRecord(org.apache.poi.hssf.record.ArrayRecord) ContinueRecord(org.apache.poi.hssf.record.ContinueRecord) TableRecord(org.apache.poi.hssf.record.TableRecord) RowRecord(org.apache.poi.hssf.record.RowRecord) SharedFormulaRecord(org.apache.poi.hssf.record.SharedFormulaRecord) FormulaRecord(org.apache.poi.hssf.record.FormulaRecord) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) Test(org.junit.Test)

Example 4 with SharedFormulaRecord

use of org.apache.poi.hssf.record.SharedFormulaRecord in project poi by apache.

the class TestSharedValueManager method testPartiallyOverlappingRanges.

/**
	 * This bug happened when there were two or more shared formula ranges that overlapped.  POI
	 * would sometimes associate formulas in the overlapping region with the wrong shared formula
	 */
public void testPartiallyOverlappingRanges() {
    Record[] records;
    int attempt = 1;
    do {
        HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook(SAMPLE_FILE_NAME);
        HSSFSheet sheet = wb.getSheetAt(0);
        RecordInspector.getRecords(sheet, 0);
        assertEquals("1+1", sheet.getRow(2).getCell(0).getCellFormula());
        if ("1+1".equals(sheet.getRow(3).getCell(0).getCellFormula())) {
            throw new AssertionFailedError("Identified bug - wrong shared formula record chosen" + " (attempt " + attempt + ")");
        }
        assertEquals("2+2", sheet.getRow(3).getCell(0).getCellFormula());
        records = RecordInspector.getRecords(sheet, 0);
    } while (attempt++ < MAX_ATTEMPTS);
    int count = 0;
    for (Record record : records) {
        if (record instanceof SharedFormulaRecord) {
            count++;
        }
    }
    assertEquals(2, count);
}
Also used : SharedFormulaRecord(org.apache.poi.hssf.record.SharedFormulaRecord) Record(org.apache.poi.hssf.record.Record) SharedFormulaRecord(org.apache.poi.hssf.record.SharedFormulaRecord) HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet) AssertionFailedError(junit.framework.AssertionFailedError) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook)

Example 5 with SharedFormulaRecord

use of org.apache.poi.hssf.record.SharedFormulaRecord in project poi by apache.

the class FormulaRecordAggregate method unlinkSharedFormula.

public void unlinkSharedFormula() {
    SharedFormulaRecord sfr = _sharedFormulaRecord;
    if (sfr == null) {
        throw new IllegalStateException("Formula not linked to shared formula");
    }
    Ptg[] ptgs = sfr.getFormulaTokens(_formulaRecord);
    _formulaRecord.setParsedExpression(ptgs);
    //Now its not shared!
    _formulaRecord.setSharedFormula(false);
    _sharedFormulaRecord = null;
}
Also used : Ptg(org.apache.poi.ss.formula.ptg.Ptg) ExpPtg(org.apache.poi.ss.formula.ptg.ExpPtg) SharedFormulaRecord(org.apache.poi.hssf.record.SharedFormulaRecord)

Aggregations

SharedFormulaRecord (org.apache.poi.hssf.record.SharedFormulaRecord)7 Record (org.apache.poi.hssf.record.Record)6 FormulaRecord (org.apache.poi.hssf.record.FormulaRecord)4 AssertionFailedError (junit.framework.AssertionFailedError)3 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)3 ArrayRecord (org.apache.poi.hssf.record.ArrayRecord)2 BlankRecord (org.apache.poi.hssf.record.BlankRecord)2 MulBlankRecord (org.apache.poi.hssf.record.MulBlankRecord)2 NumberRecord (org.apache.poi.hssf.record.NumberRecord)2 RowRecord (org.apache.poi.hssf.record.RowRecord)2 StringRecord (org.apache.poi.hssf.record.StringRecord)2 WindowTwoRecord (org.apache.poi.hssf.record.WindowTwoRecord)2 HSSFSheet (org.apache.poi.hssf.usermodel.HSSFSheet)2 Test (org.junit.Test)2 ArrayList (java.util.ArrayList)1 LastCellOfRowDummyRecord (org.apache.poi.hssf.eventusermodel.dummyrecord.LastCellOfRowDummyRecord)1 MissingCellDummyRecord (org.apache.poi.hssf.eventusermodel.dummyrecord.MissingCellDummyRecord)1 MissingRowDummyRecord (org.apache.poi.hssf.eventusermodel.dummyrecord.MissingRowDummyRecord)1 BOFRecord (org.apache.poi.hssf.record.BOFRecord)1 CellValueRecordInterface (org.apache.poi.hssf.record.CellValueRecordInterface)1