use of org.apache.poi.hssf.record.aggregates.FormulaRecordAggregate in project poi by apache.
the class HSSFCell method getErrorCellValue.
/**
* get the value of the cell as an error code. For strings, numbers, and booleans, we throw an exception.
* For blank cells we return a 0.
*/
@Override
public byte getErrorCellValue() {
switch(_cellType) {
case ERROR:
return ((BoolErrRecord) _record).getErrorValue();
case FORMULA:
FormulaRecord fr = ((FormulaRecordAggregate) _record).getFormulaRecord();
checkFormulaCachedValueType(CellType.ERROR, fr);
return (byte) fr.getCachedErrorValue();
default:
throw typeMismatch(CellType.ERROR, _cellType, false);
}
}
use of org.apache.poi.hssf.record.aggregates.FormulaRecordAggregate in project poi by apache.
the class HSSFCell method setCellValue.
/**
* Set a string value for the cell.
*
* @param value value to set the cell to. For formulas we'll set the formula
* string, for String cells we'll set its value. For other types we will
* change the cell to a string cell and set its value.
* If value is <code>null</code> then we will change the cell to a Blank cell.
*/
public void setCellValue(RichTextString value) {
int row = _record.getRow();
short col = _record.getColumn();
short styleIndex = _record.getXFIndex();
if (value == null) {
notifyFormulaChanging();
setCellType(CellType.BLANK, false, row, col, styleIndex);
return;
}
if (value.length() > SpreadsheetVersion.EXCEL97.getMaxTextLength()) {
throw new IllegalArgumentException("The maximum length of cell contents (text) is 32,767 characters");
}
if (_cellType == CellType.FORMULA) {
// Set the 'pre-evaluated result' for the formula
// note - formulas do not preserve text formatting.
FormulaRecordAggregate fr = (FormulaRecordAggregate) _record;
fr.setCachedStringResult(value.getString());
// Update our local cache to the un-formatted version
_stringValue = new HSSFRichTextString(value.getString());
// All done
return;
}
if (_cellType != CellType.STRING) {
setCellType(CellType.STRING, false, row, col, styleIndex);
}
int index = 0;
HSSFRichTextString hvalue = (HSSFRichTextString) value;
UnicodeString str = hvalue.getUnicodeString();
index = _book.getWorkbook().addSSTString(str);
((LabelSSTRecord) _record).setSSTIndex(index);
_stringValue = hvalue;
_stringValue.setWorkbookReferences(_book.getWorkbook(), ((LabelSSTRecord) _record));
_stringValue.setUnicodeString(_book.getWorkbook().getSSTString(index));
}
use of org.apache.poi.hssf.record.aggregates.FormulaRecordAggregate 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;
}
use of org.apache.poi.hssf.record.aggregates.FormulaRecordAggregate in project poi by apache.
the class HSSFCell method setCellFormula.
public void setCellFormula(String formula) {
if (isPartOfArrayFormulaGroup()) {
notifyArrayFormulaChanging();
}
int row = _record.getRow();
short col = _record.getColumn();
short styleIndex = _record.getXFIndex();
if (formula == null) {
notifyFormulaChanging();
setCellType(CellType.BLANK, false, row, col, styleIndex);
return;
}
int sheetIndex = _book.getSheetIndex(_sheet);
Ptg[] ptgs = HSSFFormulaParser.parse(formula, _book, FormulaType.CELL, sheetIndex);
setCellType(CellType.FORMULA, false, row, col, styleIndex);
FormulaRecordAggregate agg = (FormulaRecordAggregate) _record;
FormulaRecord frec = agg.getFormulaRecord();
frec.setOptions((short) 2);
frec.setValue(0);
//only set to default if there is no extended format index already set
if (agg.getXFIndex() == (short) 0) {
agg.setXFIndex((short) 0x0f);
}
agg.setParsedExpression(ptgs);
}
use of org.apache.poi.hssf.record.aggregates.FormulaRecordAggregate in project poi by apache.
the class HSSFCell method convertCellValueToString.
private String convertCellValueToString() {
switch(_cellType) {
case BLANK:
return "";
case BOOLEAN:
return ((BoolErrRecord) _record).getBooleanValue() ? "TRUE" : "FALSE";
case STRING:
int sstIndex = ((LabelSSTRecord) _record).getSSTIndex();
return _book.getWorkbook().getSSTString(sstIndex).getString();
case NUMERIC:
return NumberToTextConverter.toText(((NumberRecord) _record).getValue());
case ERROR:
return FormulaError.forInt(((BoolErrRecord) _record).getErrorValue()).getString();
case FORMULA:
// just use cached formula result instead
break;
default:
throw new IllegalStateException("Unexpected cell type (" + _cellType + ")");
}
FormulaRecordAggregate fra = ((FormulaRecordAggregate) _record);
FormulaRecord fr = fra.getFormulaRecord();
switch(CellType.forInt(fr.getCachedResultType())) {
case BOOLEAN:
return fr.getCachedBooleanValue() ? "TRUE" : "FALSE";
case STRING:
return fra.getStringValue();
case NUMERIC:
return NumberToTextConverter.toText(fr.getValue());
case ERROR:
return FormulaError.forInt(fr.getCachedErrorValue()).getString();
default:
throw new IllegalStateException("Unexpected formula result type (" + _cellType + ")");
}
}
Aggregations