Search in sources :

Example 1 with BlankRecord

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

the class HSSFCell method setCellType.

/**
     * sets the cell type. The setValue flag indicates whether to bother about
     *  trying to preserve the current value in the new record if one is created.
     *  <p>
     *  The @see #setCellValue method will call this method with false in setValue
     *  since it will overwrite the cell value later
     *
     */
private void setCellType(CellType cellType, boolean setValue, int row, short col, short styleIndex) {
    switch(cellType) {
        case FORMULA:
            FormulaRecordAggregate frec;
            if (cellType != _cellType) {
                frec = _sheet.getSheet().getRowsAggregate().createFormula(row, col);
            } else {
                frec = (FormulaRecordAggregate) _record;
                frec.setRow(row);
                frec.setColumn(col);
            }
            if (setValue) {
                frec.getFormulaRecord().setValue(getNumericCellValue());
            }
            frec.setXFIndex(styleIndex);
            _record = frec;
            break;
        case NUMERIC:
            NumberRecord nrec = null;
            if (cellType != _cellType) {
                nrec = new NumberRecord();
            } else {
                nrec = (NumberRecord) _record;
            }
            nrec.setColumn(col);
            if (setValue) {
                nrec.setValue(getNumericCellValue());
            }
            nrec.setXFIndex(styleIndex);
            nrec.setRow(row);
            _record = nrec;
            break;
        case STRING:
            LabelSSTRecord lrec;
            if (cellType == _cellType) {
                lrec = (LabelSSTRecord) _record;
            } else {
                lrec = new LabelSSTRecord();
                lrec.setColumn(col);
                lrec.setRow(row);
                lrec.setXFIndex(styleIndex);
            }
            if (setValue) {
                String str = convertCellValueToString();
                if (str == null) {
                    // bug 55668: don't try to store null-string when formula
                    // results in empty/null value
                    setCellType(CellType.BLANK, false, row, col, styleIndex);
                    return;
                } else {
                    int sstIndex = _book.getWorkbook().addSSTString(new UnicodeString(str));
                    lrec.setSSTIndex(sstIndex);
                    UnicodeString us = _book.getWorkbook().getSSTString(sstIndex);
                    _stringValue = new HSSFRichTextString();
                    _stringValue.setUnicodeString(us);
                }
            }
            _record = lrec;
            break;
        case BLANK:
            BlankRecord brec = null;
            if (cellType != _cellType) {
                brec = new BlankRecord();
            } else {
                brec = (BlankRecord) _record;
            }
            brec.setColumn(col);
            // During construction the cellStyle may be null for a Blank cell.
            brec.setXFIndex(styleIndex);
            brec.setRow(row);
            _record = brec;
            break;
        case BOOLEAN:
            BoolErrRecord boolRec = null;
            if (cellType != _cellType) {
                boolRec = new BoolErrRecord();
            } else {
                boolRec = (BoolErrRecord) _record;
            }
            boolRec.setColumn(col);
            if (setValue) {
                boolRec.setValue(convertCellValueToBoolean());
            }
            boolRec.setXFIndex(styleIndex);
            boolRec.setRow(row);
            _record = boolRec;
            break;
        case ERROR:
            BoolErrRecord errRec = null;
            if (cellType != _cellType) {
                errRec = new BoolErrRecord();
            } else {
                errRec = (BoolErrRecord) _record;
            }
            errRec.setColumn(col);
            if (setValue) {
                errRec.setValue(FormulaError.VALUE.getCode());
            }
            errRec.setXFIndex(styleIndex);
            errRec.setRow(row);
            _record = errRec;
            break;
        default:
            throw new IllegalStateException("Invalid cell type: " + cellType);
    }
    if (cellType != _cellType && // Special Value to indicate an uninitialized Cell
    _cellType != CellType._NONE) {
        _sheet.getSheet().replaceValueRecord(_record);
    }
    _cellType = cellType;
}
Also used : BoolErrRecord(org.apache.poi.hssf.record.BoolErrRecord) BlankRecord(org.apache.poi.hssf.record.BlankRecord) UnicodeString(org.apache.poi.hssf.record.common.UnicodeString) FormulaRecordAggregate(org.apache.poi.hssf.record.aggregates.FormulaRecordAggregate) RichTextString(org.apache.poi.ss.usermodel.RichTextString) UnicodeString(org.apache.poi.hssf.record.common.UnicodeString) NumberRecord(org.apache.poi.hssf.record.NumberRecord) LabelSSTRecord(org.apache.poi.hssf.record.LabelSSTRecord)

Example 2 with BlankRecord

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

the class ValueRecordsAggregate method addMultipleBlanks.

public void addMultipleBlanks(MulBlankRecord mbr) {
    for (int j = 0; j < mbr.getNumColumns(); j++) {
        BlankRecord br = new BlankRecord();
        br.setColumn((short) (j + mbr.getFirstColumn()));
        br.setRow(mbr.getRow());
        br.setXFIndex(mbr.getXFAt(j));
        insertCell(br);
    }
}
Also used : MulBlankRecord(org.apache.poi.hssf.record.MulBlankRecord) BlankRecord(org.apache.poi.hssf.record.BlankRecord)

Example 3 with BlankRecord

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

the class TestSheet method testSheetDimensions.

@Test
public void testSheetDimensions() throws IOException {
    InternalSheet sheet = InternalSheet.createSheet();
    DimensionsRecord dimensions = (DimensionsRecord) sheet.findFirstRecordBySid(DimensionsRecord.sid);
    assertEquals(0, dimensions.getFirstCol());
    assertEquals(0, dimensions.getFirstRow());
    // plus pne
    assertEquals(1, dimensions.getLastCol());
    // plus pne
    assertEquals(1, dimensions.getLastRow());
    RowRecord rr = new RowRecord(0);
    sheet.addRow(rr);
    assertEquals(0, dimensions.getFirstCol());
    assertEquals(0, dimensions.getFirstRow());
    assertEquals(1, dimensions.getLastCol());
    assertEquals(1, dimensions.getLastRow());
    CellValueRecordInterface cvr;
    cvr = new BlankRecord();
    cvr.setColumn((short) 0);
    cvr.setRow(0);
    sheet.addValueRecord(0, cvr);
    assertEquals(0, dimensions.getFirstCol());
    assertEquals(0, dimensions.getFirstRow());
    assertEquals(1, dimensions.getLastCol());
    assertEquals(1, dimensions.getLastRow());
    cvr = new BlankRecord();
    cvr.setColumn((short) 1);
    cvr.setRow(0);
    sheet.addValueRecord(0, cvr);
    assertEquals(0, dimensions.getFirstCol());
    assertEquals(0, dimensions.getFirstRow());
    //YK:  failed until Bugzilla 53414 was fixed
    assertEquals(2, dimensions.getLastCol());
    assertEquals(1, dimensions.getLastRow());
}
Also used : MulBlankRecord(org.apache.poi.hssf.record.MulBlankRecord) BlankRecord(org.apache.poi.hssf.record.BlankRecord) CellValueRecordInterface(org.apache.poi.hssf.record.CellValueRecordInterface) RowRecord(org.apache.poi.hssf.record.RowRecord) DimensionsRecord(org.apache.poi.hssf.record.DimensionsRecord) Test(org.junit.Test)

Example 4 with BlankRecord

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

the class TestValueRecordsAggregate method testGetPhysicalNumberOfCells.

@Test
public void testGetPhysicalNumberOfCells() {
    assertEquals(0, valueRecord.getPhysicalNumberOfCells());
    BlankRecord blankRecord1 = newBlankRecord();
    valueRecord.insertCell(blankRecord1);
    assertEquals(1, valueRecord.getPhysicalNumberOfCells());
    valueRecord.removeCell(blankRecord1);
    assertEquals(0, valueRecord.getPhysicalNumberOfCells());
}
Also used : MulBlankRecord(org.apache.poi.hssf.record.MulBlankRecord) BlankRecord(org.apache.poi.hssf.record.BlankRecord) Test(org.junit.Test)

Example 5 with BlankRecord

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

the class TestValueRecordsAggregate method newBlankRecord.

private static BlankRecord newBlankRecord(int col, int row) {
    BlankRecord blankRecord = new BlankRecord();
    blankRecord.setRow(row);
    blankRecord.setColumn((short) col);
    return blankRecord;
}
Also used : MulBlankRecord(org.apache.poi.hssf.record.MulBlankRecord) BlankRecord(org.apache.poi.hssf.record.BlankRecord)

Aggregations

BlankRecord (org.apache.poi.hssf.record.BlankRecord)15 MulBlankRecord (org.apache.poi.hssf.record.MulBlankRecord)12 Test (org.junit.Test)7 FormulaRecord (org.apache.poi.hssf.record.FormulaRecord)4 RowRecord (org.apache.poi.hssf.record.RowRecord)4 CellValueRecordInterface (org.apache.poi.hssf.record.CellValueRecordInterface)3 LabelSSTRecord (org.apache.poi.hssf.record.LabelSSTRecord)3 NumberRecord (org.apache.poi.hssf.record.NumberRecord)3 Record (org.apache.poi.hssf.record.Record)3 SharedFormulaRecord (org.apache.poi.hssf.record.SharedFormulaRecord)3 WindowTwoRecord (org.apache.poi.hssf.record.WindowTwoRecord)3 AssertionFailedError (junit.framework.AssertionFailedError)2 LastCellOfRowDummyRecord (org.apache.poi.hssf.eventusermodel.dummyrecord.LastCellOfRowDummyRecord)2 MissingCellDummyRecord (org.apache.poi.hssf.eventusermodel.dummyrecord.MissingCellDummyRecord)2 BOFRecord (org.apache.poi.hssf.record.BOFRecord)2 BoolErrRecord (org.apache.poi.hssf.record.BoolErrRecord)2 DimensionsRecord (org.apache.poi.hssf.record.DimensionsRecord)2 StringRecord (org.apache.poi.hssf.record.StringRecord)2 ArrayList (java.util.ArrayList)1 MissingRowDummyRecord (org.apache.poi.hssf.eventusermodel.dummyrecord.MissingRowDummyRecord)1