Search in sources :

Example 6 with InternalWorkbook

use of org.apache.poi.hssf.model.InternalWorkbook in project poi by apache.

the class TestFeatRecord method testWithoutFeatRecord.

public void testWithoutFeatRecord() throws Exception {
    HSSFWorkbook hssf = HSSFTestDataSamples.openSampleWorkbook("46136-WithWarnings.xls");
    InternalWorkbook wb = HSSFTestHelper.getWorkbookForTest(hssf);
    assertEquals(1, hssf.getNumberOfSheets());
    int countFR = 0;
    int countFRH = 0;
    // Check on the workbook, but shouldn't be there!
    for (Record r : wb.getRecords()) {
        if (r instanceof FeatRecord) {
            countFR++;
        } else if (r.getSid() == FeatRecord.sid) {
            countFR++;
        }
        if (r instanceof FeatHdrRecord) {
            countFRH++;
        } else if (r.getSid() == FeatHdrRecord.sid) {
            countFRH++;
        }
    }
    assertEquals(0, countFR);
    assertEquals(0, countFRH);
    // Now check on the sheet
    HSSFSheet s = hssf.getSheetAt(0);
    InternalSheet sheet = HSSFTestHelper.getSheetForTest(s);
    for (RecordBase rb : sheet.getRecords()) {
        if (rb instanceof Record) {
            Record r = (Record) rb;
            if (r instanceof FeatRecord) {
                countFR++;
            } else if (r.getSid() == FeatRecord.sid) {
                countFR++;
            }
            if (r instanceof FeatHdrRecord) {
                countFRH++;
            } else if (r.getSid() == FeatHdrRecord.sid) {
                countFRH++;
            }
        }
    }
    assertEquals(0, countFR);
    assertEquals(0, countFRH);
}
Also used : InternalSheet(org.apache.poi.hssf.model.InternalSheet) HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) InternalWorkbook(org.apache.poi.hssf.model.InternalWorkbook)

Example 7 with InternalWorkbook

use of org.apache.poi.hssf.model.InternalWorkbook in project poi by apache.

the class TestHSSFSheet method autoFilter.

@Test
public void autoFilter() throws IOException {
    HSSFWorkbook wb1 = new HSSFWorkbook();
    HSSFSheet sh = wb1.createSheet();
    InternalWorkbook iwb = wb1.getWorkbook();
    InternalSheet ish = sh.getSheet();
    assertNull(iwb.getSpecificBuiltinRecord(NameRecord.BUILTIN_FILTER_DB, 1));
    assertNull(ish.findFirstRecordBySid(AutoFilterInfoRecord.sid));
    CellRangeAddress range = CellRangeAddress.valueOf("A1:B10");
    sh.setAutoFilter(range);
    NameRecord name = iwb.getSpecificBuiltinRecord(NameRecord.BUILTIN_FILTER_DB, 1);
    assertNotNull(name);
    // The built-in name for auto-filter must consist of a single Area3d Ptg.
    Ptg[] ptg = name.getNameDefinition();
    assertEquals("The built-in name for auto-filter must consist of a single Area3d Ptg", 1, ptg.length);
    assertTrue("The built-in name for auto-filter must consist of a single Area3d Ptg", ptg[0] instanceof Area3DPtg);
    Area3DPtg aref = (Area3DPtg) ptg[0];
    assertEquals(range.getFirstColumn(), aref.getFirstColumn());
    assertEquals(range.getFirstRow(), aref.getFirstRow());
    assertEquals(range.getLastColumn(), aref.getLastColumn());
    assertEquals(range.getLastRow(), aref.getLastRow());
    // verify  AutoFilterInfoRecord
    AutoFilterInfoRecord afilter = (AutoFilterInfoRecord) ish.findFirstRecordBySid(AutoFilterInfoRecord.sid);
    assertNotNull(afilter);
    //filter covers two columns
    assertEquals(2, afilter.getNumEntries());
    HSSFPatriarch dr = sh.getDrawingPatriarch();
    assertNotNull(dr);
    HSSFSimpleShape comboBoxShape = (HSSFSimpleShape) dr.getChildren().get(0);
    assertEquals(comboBoxShape.getShapeType(), HSSFSimpleShape.OBJECT_TYPE_COMBO_BOX);
    // ObjRecord will appear after serializetion
    assertNull(ish.findFirstRecordBySid(ObjRecord.sid));
    HSSFWorkbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb1);
    wb1.close();
    sh = wb2.getSheetAt(0);
    ish = sh.getSheet();
    ObjRecord objRecord = (ObjRecord) ish.findFirstRecordBySid(ObjRecord.sid);
    List<SubRecord> subRecords = objRecord.getSubRecords();
    assertEquals(3, subRecords.size());
    assertTrue(subRecords.get(0) instanceof CommonObjectDataSubRecord);
    // must be present, see Bug 51481
    assertTrue(subRecords.get(1) instanceof FtCblsSubRecord);
    assertTrue(subRecords.get(2) instanceof LbsDataSubRecord);
    wb2.close();
}
Also used : AutoFilterInfoRecord(org.apache.poi.hssf.record.AutoFilterInfoRecord) Ptg(org.apache.poi.ss.formula.ptg.Ptg) Area3DPtg(org.apache.poi.ss.formula.ptg.Area3DPtg) CommonObjectDataSubRecord(org.apache.poi.hssf.record.CommonObjectDataSubRecord) LbsDataSubRecord(org.apache.poi.hssf.record.LbsDataSubRecord) InternalWorkbook(org.apache.poi.hssf.model.InternalWorkbook) InternalSheet(org.apache.poi.hssf.model.InternalSheet) NameRecord(org.apache.poi.hssf.record.NameRecord) Area3DPtg(org.apache.poi.ss.formula.ptg.Area3DPtg) FtCblsSubRecord(org.apache.poi.hssf.record.FtCblsSubRecord) LbsDataSubRecord(org.apache.poi.hssf.record.LbsDataSubRecord) SubRecord(org.apache.poi.hssf.record.SubRecord) CommonObjectDataSubRecord(org.apache.poi.hssf.record.CommonObjectDataSubRecord) FtCblsSubRecord(org.apache.poi.hssf.record.FtCblsSubRecord) ObjRecord(org.apache.poi.hssf.record.ObjRecord) CellRangeAddress(org.apache.poi.ss.util.CellRangeAddress) Test(org.junit.Test)

Example 8 with InternalWorkbook

use of org.apache.poi.hssf.model.InternalWorkbook in project poi by apache.

the class TestHSSFDateUtil method onARealFile.

/**
     * Test that against a real, test file, we still do everything
     *  correctly
     * @throws IOException 
     */
@Test
public void onARealFile() throws IOException {
    HSSFWorkbook workbook = HSSFTestDataSamples.openSampleWorkbook("DateFormats.xls");
    HSSFSheet sheet = workbook.getSheetAt(0);
    InternalWorkbook wb = workbook.getWorkbook();
    assertNotNull(wb);
    HSSFRow row;
    HSSFCell cell;
    HSSFCellStyle style;
    double aug_10_2007 = 39304.0;
    // Should have dates in 2nd column
    // All of them are the 10th of August
    // 2 US dates, 3 UK dates
    row = sheet.getRow(0);
    cell = row.getCell(1);
    style = cell.getCellStyle();
    assertEquals(aug_10_2007, cell.getNumericCellValue(), 0.0001);
    assertEquals("d-mmm-yy", style.getDataFormatString());
    assertTrue(HSSFDateUtil.isInternalDateFormat(style.getDataFormat()));
    assertTrue(HSSFDateUtil.isADateFormat(style.getDataFormat(), style.getDataFormatString()));
    assertTrue(HSSFDateUtil.isCellDateFormatted(cell));
    row = sheet.getRow(1);
    cell = row.getCell(1);
    style = cell.getCellStyle();
    assertEquals(aug_10_2007, cell.getNumericCellValue(), 0.0001);
    assertFalse(HSSFDateUtil.isInternalDateFormat(cell.getCellStyle().getDataFormat()));
    assertTrue(HSSFDateUtil.isADateFormat(style.getDataFormat(), style.getDataFormatString()));
    assertTrue(HSSFDateUtil.isCellDateFormatted(cell));
    row = sheet.getRow(2);
    cell = row.getCell(1);
    style = cell.getCellStyle();
    assertEquals(aug_10_2007, cell.getNumericCellValue(), 0.0001);
    assertTrue(HSSFDateUtil.isInternalDateFormat(cell.getCellStyle().getDataFormat()));
    assertTrue(HSSFDateUtil.isADateFormat(style.getDataFormat(), style.getDataFormatString()));
    assertTrue(HSSFDateUtil.isCellDateFormatted(cell));
    row = sheet.getRow(3);
    cell = row.getCell(1);
    style = cell.getCellStyle();
    assertEquals(aug_10_2007, cell.getNumericCellValue(), 0.0001);
    assertFalse(HSSFDateUtil.isInternalDateFormat(cell.getCellStyle().getDataFormat()));
    assertTrue(HSSFDateUtil.isADateFormat(style.getDataFormat(), style.getDataFormatString()));
    assertTrue(HSSFDateUtil.isCellDateFormatted(cell));
    row = sheet.getRow(4);
    cell = row.getCell(1);
    style = cell.getCellStyle();
    assertEquals(aug_10_2007, cell.getNumericCellValue(), 0.0001);
    assertFalse(HSSFDateUtil.isInternalDateFormat(cell.getCellStyle().getDataFormat()));
    assertTrue(HSSFDateUtil.isADateFormat(style.getDataFormat(), style.getDataFormatString()));
    assertTrue(HSSFDateUtil.isCellDateFormatted(cell));
    workbook.close();
}
Also used : InternalWorkbook(org.apache.poi.hssf.model.InternalWorkbook) Test(org.junit.Test)

Example 9 with InternalWorkbook

use of org.apache.poi.hssf.model.InternalWorkbook in project poi by apache.

the class TestWorkbook method testBackupRecord.

/**
     * Test the backup field gets set as expected.
     * @throws IOException 
     */
@Test
public void testBackupRecord() throws IOException {
    HSSFWorkbook wb = new HSSFWorkbook();
    wb.createSheet();
    InternalWorkbook workbook = wb.getWorkbook();
    BackupRecord record = workbook.getBackupRecord();
    assertEquals(0, record.getBackup());
    assertFalse(wb.getBackupFlag());
    wb.setBackupFlag(true);
    assertEquals(1, record.getBackup());
    assertTrue(wb.getBackupFlag());
    wb.setBackupFlag(false);
    assertEquals(0, record.getBackup());
    assertFalse(wb.getBackupFlag());
    wb.close();
}
Also used : BackupRecord(org.apache.poi.hssf.record.BackupRecord) InternalWorkbook(org.apache.poi.hssf.model.InternalWorkbook) Test(org.junit.Test)

Example 10 with InternalWorkbook

use of org.apache.poi.hssf.model.InternalWorkbook in project poi by apache.

the class TestAreaReference method testDiscontinousReference.

public void testDiscontinousReference() throws Exception {
    InputStream is = HSSFTestDataSamples.openSampleFileStream("44167.xls");
    HSSFWorkbook wb = new HSSFWorkbook(is);
    InternalWorkbook workbook = TestHSSFWorkbook.getInternalWorkbook(wb);
    HSSFEvaluationWorkbook eb = HSSFEvaluationWorkbook.create(wb);
    assertEquals(1, wb.getNumberOfNames());
    String sheetName = "Tabelle1";
    String rawRefA = "$C$10:$C$14";
    String rawRefB = "$C$16:$C$18";
    String refA = sheetName + "!" + rawRefA;
    String refB = sheetName + "!" + rawRefB;
    String ref = refA + "," + refB;
    // Check the low level record
    NameRecord nr = workbook.getNameRecord(0);
    assertNotNull(nr);
    assertEquals("test", nr.getNameText());
    Ptg[] def = nr.getNameDefinition();
    assertEquals(4, def.length);
    MemFuncPtg ptgA = (MemFuncPtg) def[0];
    Area3DPtg ptgB = (Area3DPtg) def[1];
    Area3DPtg ptgC = (Area3DPtg) def[2];
    UnionPtg ptgD = (UnionPtg) def[3];
    assertEquals("", ptgA.toFormulaString());
    assertEquals(refA, ptgB.toFormulaString(eb));
    assertEquals(refB, ptgC.toFormulaString(eb));
    assertEquals(",", ptgD.toFormulaString());
    assertEquals(ref, HSSFFormulaParser.toFormulaString(wb, nr.getNameDefinition()));
    // Check the high level definition
    int idx = wb.getNameIndex("test");
    assertEquals(0, idx);
    HSSFName aNamedCell = wb.getNameAt(idx);
    // Should have 2 references
    assertEquals(ref, aNamedCell.getRefersToFormula());
    // Check the parsing of the reference into cells
    assertFalse(AreaReference.isContiguous(aNamedCell.getRefersToFormula()));
    AreaReference[] arefs = AreaReference.generateContiguous(aNamedCell.getRefersToFormula());
    assertEquals(2, arefs.length);
    assertEquals(refA, arefs[0].formatAsString());
    assertEquals(refB, arefs[1].formatAsString());
    for (AreaReference ar : arefs) {
        confirmResolveCellRef(wb, ar.getFirstCell());
        confirmResolveCellRef(wb, ar.getLastCell());
    }
}
Also used : Ptg(org.apache.poi.ss.formula.ptg.Ptg) UnionPtg(org.apache.poi.ss.formula.ptg.UnionPtg) Area3DPtg(org.apache.poi.ss.formula.ptg.Area3DPtg) MemFuncPtg(org.apache.poi.ss.formula.ptg.MemFuncPtg) AreaReference(org.apache.poi.ss.util.AreaReference) MemFuncPtg(org.apache.poi.ss.formula.ptg.MemFuncPtg) InputStream(java.io.InputStream) TestHSSFWorkbook(org.apache.poi.hssf.usermodel.TestHSSFWorkbook) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) HSSFEvaluationWorkbook(org.apache.poi.hssf.usermodel.HSSFEvaluationWorkbook) InternalWorkbook(org.apache.poi.hssf.model.InternalWorkbook) HSSFName(org.apache.poi.hssf.usermodel.HSSFName) NameRecord(org.apache.poi.hssf.record.NameRecord) Area3DPtg(org.apache.poi.ss.formula.ptg.Area3DPtg) UnionPtg(org.apache.poi.ss.formula.ptg.UnionPtg)

Aggregations

InternalWorkbook (org.apache.poi.hssf.model.InternalWorkbook)18 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)5 Test (org.junit.Test)5 NameRecord (org.apache.poi.hssf.record.NameRecord)4 EscherBSERecord (org.apache.poi.ddf.EscherBSERecord)3 InternalSheet (org.apache.poi.hssf.model.InternalSheet)3 Area3DPtg (org.apache.poi.ss.formula.ptg.Area3DPtg)3 Ptg (org.apache.poi.ss.formula.ptg.Ptg)3 AutoFilterInfoRecord (org.apache.poi.hssf.record.AutoFilterInfoRecord)2 RecalcIdRecord (org.apache.poi.hssf.record.RecalcIdRecord)2 HSSFEvaluationWorkbook (org.apache.poi.hssf.usermodel.HSSFEvaluationWorkbook)2 HSSFSheet (org.apache.poi.hssf.usermodel.HSSFSheet)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 EscherBlipRecord (org.apache.poi.ddf.EscherBlipRecord)1 EscherRecord (org.apache.poi.ddf.EscherRecord)1 BackupRecord (org.apache.poi.hssf.record.BackupRecord)1 CommonObjectDataSubRecord (org.apache.poi.hssf.record.CommonObjectDataSubRecord)1 DrawingGroupRecord (org.apache.poi.hssf.record.DrawingGroupRecord)1 ExtendedFormatRecord (org.apache.poi.hssf.record.ExtendedFormatRecord)1