pulled *Cell.setCellValue(double) to the common base

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1852253 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Vladislav Galas 2019-01-26 23:19:53 +00:00
parent 70b4b88a3a
commit 5da229797f
5 changed files with 55 additions and 75 deletions

View File

@ -50,7 +50,6 @@ import org.apache.poi.ss.usermodel.Comment;
import org.apache.poi.ss.usermodel.FormulaError; import org.apache.poi.ss.usermodel.FormulaError;
import org.apache.poi.ss.usermodel.Hyperlink; import org.apache.poi.ss.usermodel.Hyperlink;
import org.apache.poi.ss.usermodel.RichTextString; import org.apache.poi.ss.usermodel.RichTextString;
import org.apache.poi.ss.util.CellAddress;
import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.CellReference; import org.apache.poi.ss.util.CellReference;
import org.apache.poi.ss.util.NumberToTextConverter; import org.apache.poi.ss.util.NumberToTextConverter;
@ -420,31 +419,18 @@ public class HSSFCell extends CellBase {
} }
/** /**
* set a numeric value for the cell * {@inheritDoc}
*
* @param value the numeric value to set this cell to. For formulas we'll set the
* precalculated value, for numerics we'll set its value. For other types we
* will change the cell to a numeric cell and set its value.
*/ */
@SuppressWarnings("fallthrough")
@Override @Override
public void setCellValue(double value) { @SuppressWarnings("fallthrough")
if(Double.isInfinite(value)) { protected void setCellValueImpl(double value) {
// Excel does not support positive/negative infinities,
// rather, it gives a #DIV/0! error in these cases.
setCellErrorValue(FormulaError.DIV0.getCode());
} else if (Double.isNaN(value)){
// Excel does not support Not-a-Number (NaN),
// instead it immediately generates a #NUM! error.
setCellErrorValue(FormulaError.NUM.getCode());
} else {
int row=_record.getRow();
short col=_record.getColumn();
short styleIndex=_record.getXFIndex();
switch (_cellType) { switch (_cellType) {
default: default:
setCellType(CellType.NUMERIC, false, row, col, styleIndex); setCellType(CellType.NUMERIC,
false,
_record.getRow(),
_record.getColumn(),
_record.getXFIndex());
// fall through // fall through
case NUMERIC: case NUMERIC:
((NumberRecord)_record).setValue(value); ((NumberRecord)_record).setValue(value);
@ -455,8 +441,6 @@ public class HSSFCell extends CellBase {
} }
} }
}
/** /**
* set a date value for the cell. Excel treats dates as numeric so you will need to format the cell as * set a date value for the cell. Excel treats dates as numeric so you will need to format the cell as
* a date. * a date.

View File

@ -145,6 +145,7 @@ public interface Cell {
* {@link CellType#BOOLEAN}, {@link CellType#ERROR}) depending * {@link CellType#BOOLEAN}, {@link CellType#ERROR}) depending
* on the cached value of the formula * on the cached value of the formula
* @since POI 3.15 beta 3 * @since POI 3.15 beta 3
* @deprecated will be removed in 4.2
* Will be renamed to <code>getCachedFormulaResultType()</code> when we make the CellType enum transition in POI 4.0. See bug 59791. * Will be renamed to <code>getCachedFormulaResultType()</code> when we make the CellType enum transition in POI 4.0. See bug 59791.
*/ */
@Deprecated @Deprecated
@ -152,7 +153,7 @@ public interface Cell {
CellType getCachedFormulaResultTypeEnum(); CellType getCachedFormulaResultTypeEnum();
/** /**
* Set a numeric value for the cell * Set a numeric value for the cell.
* *
* @param value the numeric value to set this cell to. For formulas we'll set the * @param value the numeric value to set this cell to. For formulas we'll set the
* precalculated value, for numerics we'll set its value. For other types we * precalculated value, for numerics we'll set its value. For other types we

View File

@ -188,4 +188,29 @@ public abstract class CellBase implements Cell {
tryToDeleteArrayFormula(null); tryToDeleteArrayFormula(null);
} }
} }
/**
* {@inheritDoc}
*/
@Override
public final void setCellValue(double value) {
if(Double.isInfinite(value)) {
// Excel does not support positive/negative infinities,
// rather, it gives a #DIV/0! error in these cases.
setCellErrorValue(FormulaError.DIV0.getCode());
} else if (Double.isNaN(value)){
setCellErrorValue(FormulaError.NUM.getCode());
} else {
setCellValueImpl(value);
}
}
/**
* Implementation-specific way to set a numeric value.
* <code>value</code> is guaranteed to be a valid (non-NaN) double.
* The implementation is expected to adjust the cell type accordingly, so that after this call
* getCellType() or getCachedFormulaResultType() would return {@link CellType#NUMERIC}.
* @param value the new value to set
*/
protected abstract void setCellValueImpl(double value);
} }

View File

@ -36,7 +36,6 @@ import org.apache.poi.ss.usermodel.FormulaError;
import org.apache.poi.ss.usermodel.Hyperlink; import org.apache.poi.ss.usermodel.Hyperlink;
import org.apache.poi.ss.usermodel.RichTextString; import org.apache.poi.ss.usermodel.RichTextString;
import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.util.CellAddress;
import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.CellReference; import org.apache.poi.ss.util.CellReference;
import org.apache.poi.util.*; import org.apache.poi.util.*;
@ -159,26 +158,14 @@ public class SXSSFCell extends CellBase {
} }
/** /**
* Set a numeric value for the cell * {@inheritDoc}
*
* @param value the numeric value to set this cell to. For formulas we'll set the
* precalculated value, for numerics we'll set its value. For other types we
* will change the cell to a numeric cell and set its value.
*/ */
@Override @Override
public void setCellValue(double value) public void setCellValueImpl(double value) {
{
if(Double.isInfinite(value)) {
// Excel does not support positive/negative infinities,
// rather, it gives a #DIV/0! error in these cases.
setCellErrorValue(FormulaError.DIV0.getCode());
} else if (Double.isNaN(value)){
setCellErrorValue(FormulaError.NUM.getCode());
} else {
ensureTypeOrFormulaType(CellType.NUMERIC); ensureTypeOrFormulaType(CellType.NUMERIC);
if(_value.getType()==CellType.FORMULA) if(_value.getType() == CellType.FORMULA) {
((NumericFormulaValue) _value).setPreEvaluatedValue(value); ((NumericFormulaValue) _value).setPreEvaluatedValue(value);
else } else {
((NumericValue)_value).setValue(value); ((NumericValue)_value).setValue(value);
} }
} }

View File

@ -311,31 +311,14 @@ public final class XSSFCell extends CellBase {
} }
} }
/** /**
* Set a numeric value for the cell * {@inheritDoc}
*
* @param value the numeric value to set this cell to. For formulas we'll set the
* precalculated value, for numerics we'll set its value. For other types we
* will change the cell to a numeric cell and set its value.
*/ */
@Override @Override
public void setCellValue(double value) { public void setCellValueImpl(double value) {
if(Double.isInfinite(value)) {
// Excel does not support positive/negative infinities,
// rather, it gives a #DIV/0! error in these cases.
_cell.setT(STCellType.E);
_cell.setV(FormulaError.DIV0.getString());
} else if (Double.isNaN(value)){
// Excel does not support Not-a-Number (NaN),
// instead it immediately generates an #NUM! error.
_cell.setT(STCellType.E);
_cell.setV(FormulaError.NUM.getString());
} else {
_cell.setT(STCellType.N); _cell.setT(STCellType.N);
_cell.setV(String.valueOf(value)); _cell.setV(String.valueOf(value));
} }
}
/** /**
* Get the value of the cell as a string * Get the value of the cell as a string