Search in sources :

Example 1 with AutoFilterInfoRecord

use of org.apache.poi.hssf.record.AutoFilterInfoRecord 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 2 with AutoFilterInfoRecord

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

the class HSSFSheet method setAutoFilter.

@Override
public HSSFAutoFilter setAutoFilter(CellRangeAddress range) {
    InternalWorkbook workbook = _workbook.getWorkbook();
    int sheetIndex = _workbook.getSheetIndex(this);
    NameRecord name = workbook.getSpecificBuiltinRecord(NameRecord.BUILTIN_FILTER_DB, sheetIndex + 1);
    if (name == null) {
        name = workbook.createBuiltInName(NameRecord.BUILTIN_FILTER_DB, sheetIndex + 1);
    }
    int firstRow = range.getFirstRow();
    // if row was not given when constructing the range...
    if (firstRow == -1) {
        firstRow = 0;
    }
    // The built-in name must consist of a single Area3d Ptg.
    Area3DPtg ptg = new Area3DPtg(firstRow, range.getLastRow(), range.getFirstColumn(), range.getLastColumn(), false, false, false, false, sheetIndex);
    name.setNameDefinition(new Ptg[] { ptg });
    AutoFilterInfoRecord r = new AutoFilterInfoRecord();
    // the number of columns that have AutoFilter enabled.
    int numcols = 1 + range.getLastColumn() - range.getFirstColumn();
    r.setNumEntries((short) numcols);
    int idx = _sheet.findFirstRecordLocBySid(DimensionsRecord.sid);
    _sheet.getRecords().add(idx, r);
    //create a combobox control for each column
    HSSFPatriarch p = createDrawingPatriarch();
    final int firstColumn = range.getFirstColumn();
    final int lastColumn = range.getLastColumn();
    for (int col = firstColumn; col <= lastColumn; col++) {
        p.createComboBox(new HSSFClientAnchor(0, 0, 0, 0, (short) col, firstRow, (short) (col + 1), firstRow + 1));
    }
    return new HSSFAutoFilter(this);
}
Also used : AutoFilterInfoRecord(org.apache.poi.hssf.record.AutoFilterInfoRecord) NameRecord(org.apache.poi.hssf.record.NameRecord) Area3DPtg(org.apache.poi.ss.formula.ptg.Area3DPtg) InternalWorkbook(org.apache.poi.hssf.model.InternalWorkbook)

Aggregations

InternalWorkbook (org.apache.poi.hssf.model.InternalWorkbook)2 AutoFilterInfoRecord (org.apache.poi.hssf.record.AutoFilterInfoRecord)2 NameRecord (org.apache.poi.hssf.record.NameRecord)2 Area3DPtg (org.apache.poi.ss.formula.ptg.Area3DPtg)2 InternalSheet (org.apache.poi.hssf.model.InternalSheet)1 CommonObjectDataSubRecord (org.apache.poi.hssf.record.CommonObjectDataSubRecord)1 FtCblsSubRecord (org.apache.poi.hssf.record.FtCblsSubRecord)1 LbsDataSubRecord (org.apache.poi.hssf.record.LbsDataSubRecord)1 ObjRecord (org.apache.poi.hssf.record.ObjRecord)1 SubRecord (org.apache.poi.hssf.record.SubRecord)1 Ptg (org.apache.poi.ss.formula.ptg.Ptg)1 CellRangeAddress (org.apache.poi.ss.util.CellRangeAddress)1 Test (org.junit.Test)1