2011-05-17 06:46:35 -04:00
/ * = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Licensed to the Apache Software Foundation ( ASF ) under one or more
contributor license agreements . See the NOTICE file distributed with
this work for additional information regarding copyright ownership .
The ASF licenses this file to You under the Apache License , Version 2 . 0
( the " License " ) ; you may not use this file except in compliance with
the License . You may obtain a copy of the License at
http : //www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing , software
distributed under the License is distributed on an " AS IS " BASIS ,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND , either express or implied .
See the License for the specific language governing permissions and
limitations under the License .
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = * /
package org.apache.poi.xssf.streaming ;
2011-05-19 08:00:10 -04:00
import java.text.DateFormat ;
import java.text.SimpleDateFormat ;
2011-05-17 06:46:35 -04:00
import java.util.Calendar ;
import java.util.Date ;
2016-03-28 16:20:29 -04:00
import java.util.Map ;
2011-05-17 06:46:35 -04:00
2015-04-03 11:55:21 -04:00
import org.apache.poi.ss.SpreadsheetVersion ;
2011-05-17 06:46:35 -04:00
import org.apache.poi.ss.formula.FormulaParseException ;
2013-10-16 15:47:21 -04:00
import org.apache.poi.ss.formula.eval.ErrorEval ;
import org.apache.poi.ss.usermodel.Cell ;
import org.apache.poi.ss.usermodel.CellStyle ;
2016-07-04 05:45:46 -04:00
import org.apache.poi.ss.usermodel.CellType ;
2013-10-16 15:47:21 -04:00
import org.apache.poi.ss.usermodel.Comment ;
import org.apache.poi.ss.usermodel.DateUtil ;
import org.apache.poi.ss.usermodel.FormulaError ;
import org.apache.poi.ss.usermodel.Hyperlink ;
import org.apache.poi.ss.usermodel.RichTextString ;
import org.apache.poi.ss.usermodel.Row ;
2015-12-04 01:36:27 -05:00
import org.apache.poi.ss.util.CellAddress ;
2011-05-17 06:46:35 -04:00
import org.apache.poi.ss.util.CellRangeAddress ;
2011-07-12 11:18:29 -04:00
import org.apache.poi.ss.util.CellReference ;
2016-07-04 07:54:20 -04:00
import org.apache.poi.util.Internal ;
2015-09-07 16:19:50 -04:00
import org.apache.poi.util.LocaleUtil ;
2015-11-29 23:43:32 -05:00
import org.apache.poi.util.NotImplemented ;
2015-07-31 13:23:01 -04:00
import org.apache.poi.util.POILogFactory ;
import org.apache.poi.util.POILogger ;
2016-09-14 01:51:52 -04:00
import org.apache.poi.util.Removal ;
2011-07-12 11:18:29 -04:00
import org.apache.poi.xssf.usermodel.XSSFHyperlink ;
2015-07-31 13:23:01 -04:00
import org.apache.poi.xssf.usermodel.XSSFRichTextString ;
2011-05-17 06:46:35 -04:00
/ * *
* Streaming version of XSSFRow implementing the " BigGridDemo " strategy .
* /
2015-07-31 13:23:01 -04:00
public class SXSSFCell implements Cell {
2015-11-29 23:43:32 -05:00
private static final POILogger logger = POILogFactory . getLogger ( SXSSFCell . class ) ;
2011-05-17 06:46:35 -04:00
2015-11-29 23:43:32 -05:00
private final SXSSFRow _row ;
private Value _value ;
private CellStyle _style ;
private Property _firstProperty ;
2016-07-04 09:09:30 -04:00
/ * *
* @deprecated POI 3 . 15 beta 3 .
* Will be deleted when we make the CellType enum transition . See bug 59791 .
* /
2016-09-14 01:51:52 -04:00
@Removal ( version = " 3.17 " )
@Deprecated
2016-07-04 09:09:30 -04:00
public SXSSFCell ( SXSSFRow row , int cellType )
{
this ( row , CellType . forInt ( ( cellType ) ) ) ;
}
2011-05-17 06:46:35 -04:00
2016-07-04 05:45:46 -04:00
public SXSSFCell ( SXSSFRow row , CellType cellType )
2011-05-17 06:46:35 -04:00
{
_row = row ;
setType ( cellType ) ;
}
//start of interface implementation
/ * *
* Returns column index of this cell
*
* @return zero - based column index of a column in a sheet .
* /
2015-11-29 23:43:32 -05:00
@Override
2011-05-17 06:46:35 -04:00
public int getColumnIndex ( )
{
return _row . getCellIndex ( this ) ;
}
/ * *
* Returns row index of a row in the sheet that contains this cell
*
* @return zero - based row index of a row in the sheet that contains this cell
* /
2015-11-29 23:43:32 -05:00
@Override
2011-05-17 06:46:35 -04:00
public int getRowIndex ( )
{
return _row . getRowNum ( ) ;
}
2015-12-04 01:36:27 -05:00
/ * *
* { @inheritDoc }
* /
@Override
public CellAddress getAddress ( ) {
return new CellAddress ( this ) ;
}
2011-05-17 06:46:35 -04:00
/ * *
* Returns the sheet this cell belongs to
*
* @return the sheet this cell belongs to
* /
2015-11-29 23:43:32 -05:00
@Override
2015-07-31 13:23:01 -04:00
public SXSSFSheet getSheet ( )
2011-05-17 06:46:35 -04:00
{
return _row . getSheet ( ) ;
}
/ * *
* Returns the Row this cell belongs to
*
* @return the Row that owns this cell
* /
2015-11-29 23:43:32 -05:00
@Override
public Row getRow ( )
{
return _row ;
}
2011-05-17 06:46:35 -04:00
/ * *
* Set the cells type ( numeric , formula or string )
*
* @throws IllegalArgumentException if the specified cell type is invalid
2016-07-04 05:45:46 -04:00
* @see CellType # NUMERIC
* @see CellType # STRING
* @see CellType # FORMULA
* @see CellType # BLANK
* @see CellType # BOOLEAN
* @see CellType # ERROR
* @deprecated POI 3 . 15 beta 3 . Use { @link # setCellType ( CellType ) } instead .
2016-07-04 08:35:49 -04:00
* Will be deleted when we make the CellType enum transition . See bug 59791 .
2011-05-17 06:46:35 -04:00
* /
2015-11-29 23:43:32 -05:00
@Override
2011-05-17 06:46:35 -04:00
public void setCellType ( int cellType )
2016-07-04 05:45:46 -04:00
{
ensureType ( CellType . forInt ( cellType ) ) ;
}
/ * *
* Set the cells type ( numeric , formula or string )
*
* @throws IllegalArgumentException if the specified cell type is invalid
* /
@Override
public void setCellType ( CellType cellType )
2011-05-17 06:46:35 -04:00
{
ensureType ( cellType ) ;
}
/ * *
* Return the cell type .
*
* @return the cell type
* /
2015-11-29 23:43:32 -05:00
@Override
2016-07-04 07:54:20 -04:00
public int getCellType ( )
{
return getCellTypeEnum ( ) . getCode ( ) ;
}
/ * *
* Return the cell type .
*
* @return the cell type
2016-07-04 08:35:49 -04:00
* @since POI 3 . 15 beta 3
2016-07-04 07:54:20 -04:00
* @deprecated POI 3 . 15 beta 3
2016-07-04 08:35:49 -04:00
* Will be deleted when we make the CellType enum transition . See bug 59791 .
2016-07-04 07:54:20 -04:00
* /
2016-07-05 19:22:49 -04:00
@Internal ( since = " POI 3.15 beta 3 " )
2016-07-04 07:54:20 -04:00
@Override
public CellType getCellTypeEnum ( )
2011-05-17 06:46:35 -04:00
{
return _value . getType ( ) ;
}
/ * *
* Only valid for formula cells
2016-07-04 05:45:46 -04:00
* @return one of ( { @link CellType # NUMERIC } , { @link CellType # STRING } ,
* { @link CellType # BOOLEAN } , { @link CellType # ERROR } ) depending
2011-05-17 06:46:35 -04:00
* on the cached value of the formula
* /
2015-11-29 23:43:32 -05:00
@Override
2016-07-04 07:54:20 -04:00
public int getCachedFormulaResultType ( )
{
return getCachedFormulaResultTypeEnum ( ) . getCode ( ) ;
}
/ * *
* Only valid for formula cells
* @return one of ( { @link CellType # NUMERIC } , { @link CellType # STRING } ,
* { @link CellType # BOOLEAN } , { @link CellType # ERROR } ) depending
* on the cached value of the formula
2016-07-04 08:35:49 -04:00
* @since POI 3 . 15 beta 3
2016-07-04 07:54:20 -04:00
* @deprecated POI 3 . 15 beta 3 .
2016-07-04 08:35:49 -04:00
* Will be deleted when we make the CellType enum transition . See bug 59791 .
2016-07-04 07:54:20 -04:00
* /
2016-07-05 19:22:49 -04:00
@Internal ( since = " POI 3.15 beta 3 " )
2016-07-04 07:54:20 -04:00
@Override
public CellType getCachedFormulaResultTypeEnum ( )
2011-05-17 06:46:35 -04:00
{
2016-07-04 05:45:46 -04:00
if ( _value . getType ( ) ! = CellType . FORMULA ) {
2011-05-19 08:00:10 -04:00
throw new IllegalStateException ( " Only formula cells have cached results " ) ;
}
return ( ( FormulaValue ) _value ) . getFormulaType ( ) ;
2011-05-17 06:46:35 -04:00
}
/ * *
* Set a numeric value for the cell
*
* @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 .
* /
2015-11-29 23:43:32 -05:00
@Override
2011-05-17 06:46:35 -04:00
public void setCellValue ( double value )
{
2011-05-19 08:00:10 -04:00
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 {
2016-07-04 05:45:46 -04:00
ensureTypeOrFormulaType ( CellType . NUMERIC ) ;
if ( _value . getType ( ) = = CellType . FORMULA )
2011-05-19 08:00:10 -04:00
( ( NumericFormulaValue ) _value ) . setPreEvaluatedValue ( value ) ;
else
( ( NumericValue ) _value ) . setValue ( value ) ;
}
2011-05-17 06:46:35 -04:00
}
/ * *
* Converts the supplied date to its equivalent Excel numeric value and sets
* that into the cell .
* < p / >
* < b > Note < / b > - There is actually no ' DATE ' cell type in Excel . In many
* cases ( when entering date values ) , Excel automatically adjusts the
* < i > cell style < / i > to some date format , creating the illusion that the cell
2016-07-04 05:45:46 -04:00
* data type is now something besides { @link CellType # NUMERIC } . POI
2011-05-17 06:46:35 -04:00
* does not attempt to replicate this behaviour . To make a numeric cell
* display as a date , use { @link # setCellStyle ( CellStyle ) } etc .
*
* @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 numerics cell and set its value .
* /
2015-11-29 23:43:32 -05:00
@Override
2015-07-31 13:23:01 -04:00
public void setCellValue ( Date value ) {
2016-03-28 16:20:29 -04:00
if ( value = = null ) {
2016-07-04 05:45:46 -04:00
setCellType ( CellType . BLANK ) ;
2016-03-28 16:20:29 -04:00
return ;
}
2015-07-31 13:23:01 -04:00
boolean date1904 = getSheet ( ) . getWorkbook ( ) . isDate1904 ( ) ;
2011-05-17 06:46:35 -04:00
setCellValue ( DateUtil . getExcelDate ( value , date1904 ) ) ;
}
/ * *
* Set a date value for the cell . Excel treats dates as numeric so you will need to format the cell as
* a date .
* < p >
* This will set the cell value based on the Calendar ' s timezone . As Excel
* does not support timezones this means that both 20 : 00 + 03 : 00 and
* 20 : 00 - 03 : 00 will be reported as the same value ( 20 : 00 ) even that there
* are 6 hours difference between the two times . This difference can be
* preserved by using < code > setCellValue ( value . getTime ( ) ) < / code > which will
* automatically shift the times to the default timezone .
* < / p >
*
* @param value the date value to set this cell to . For formulas we ' ll set the
* precalculated value , for numerics we ' ll set its value . For othertypes we
* will change the cell to a numeric cell and set its value .
* /
2015-11-29 23:43:32 -05:00
@Override
2015-07-31 13:23:01 -04:00
public void setCellValue ( Calendar value ) {
2016-03-28 16:20:29 -04:00
if ( value = = null ) {
2016-07-04 05:45:46 -04:00
setCellType ( CellType . BLANK ) ;
2016-03-28 16:20:29 -04:00
return ;
}
2015-07-31 13:23:01 -04:00
boolean date1904 = getSheet ( ) . getWorkbook ( ) . isDate1904 ( ) ;
2011-05-17 06:46:35 -04:00
setCellValue ( DateUtil . getExcelDate ( value , date1904 ) ) ;
}
/ * *
* Set a rich 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 null then we will change the cell to a Blank cell .
* /
2015-11-29 23:43:32 -05:00
@Override
2011-05-17 06:46:35 -04:00
public void setCellValue ( RichTextString value )
{
2015-10-28 13:42:56 -04:00
XSSFRichTextString xvalue = ( XSSFRichTextString ) value ;
2015-07-31 13:23:01 -04:00
2016-02-11 10:13:40 -05:00
if ( xvalue ! = null & & xvalue . getString ( ) ! = null ) {
2015-10-28 13:42:56 -04:00
ensureRichTextStringType ( ) ;
if ( xvalue . length ( ) > SpreadsheetVersion . EXCEL2007 . getMaxTextLength ( ) ) {
throw new IllegalArgumentException ( " The maximum length of cell contents (text) is 32,767 characters " ) ;
}
if ( xvalue . hasFormatting ( ) )
logger . log ( POILogger . WARN , " SXSSF doesn't support Shared Strings, rich text formatting information has be lost " ) ;
( ( RichTextValue ) _value ) . setValue ( xvalue ) ;
} else {
2016-07-04 05:45:46 -04:00
setCellType ( CellType . BLANK ) ;
2015-10-28 13:42:56 -04:00
}
2011-05-17 06:46:35 -04:00
}
/ * *
* 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 null then we will change the cell to a Blank cell .
* /
2015-11-29 23:43:32 -05:00
@Override
2011-05-17 06:46:35 -04:00
public void setCellValue ( String value )
{
2016-02-11 10:13:40 -05:00
if ( value ! = null ) {
2016-07-04 05:45:46 -04:00
ensureTypeOrFormulaType ( CellType . STRING ) ;
2016-02-11 10:13:40 -05:00
2016-02-17 21:03:11 -05:00
if ( value . length ( ) > SpreadsheetVersion . EXCEL2007 . getMaxTextLength ( ) ) {
2016-02-11 10:13:40 -05:00
throw new IllegalArgumentException ( " The maximum length of cell contents (text) is 32,767 characters " ) ;
}
2016-07-04 05:45:46 -04:00
if ( _value . getType ( ) = = CellType . FORMULA )
2016-07-27 14:15:54 -04:00
if ( _value instanceof NumericFormulaValue ) {
( ( NumericFormulaValue ) _value ) . setPreEvaluatedValue ( Double . parseDouble ( value ) ) ;
} else {
( ( StringFormulaValue ) _value ) . setPreEvaluatedValue ( value ) ;
}
2016-02-11 10:13:40 -05:00
else
( ( PlainStringValue ) _value ) . setValue ( value ) ;
} else {
2016-07-04 05:45:46 -04:00
setCellType ( CellType . BLANK ) ;
2015-04-03 11:55:21 -04:00
}
2011-05-17 06:46:35 -04:00
}
/ * *
* Sets formula for this cell .
* < p >
* Note , this method only sets the formula string and does not calculate the formula value .
* To set the precalculated value use { @link # setCellValue ( double ) } or { @link # setCellValue ( String ) }
* < / p >
*
* @param formula the formula to set , e . g . < code > " SUM(C4:E4) " < / code > .
* If the argument is < code > null < / code > then the current formula is removed .
* @throws FormulaParseException if the formula has incorrect syntax or is otherwise invalid
* /
2015-11-29 23:43:32 -05:00
@Override
2011-05-17 06:46:35 -04:00
public void setCellFormula ( String formula ) throws FormulaParseException
{
2011-05-19 08:00:10 -04:00
if ( formula = = null ) {
2016-07-04 05:45:46 -04:00
setType ( CellType . BLANK ) ;
2011-05-19 08:00:10 -04:00
return ;
}
2011-05-17 06:46:35 -04:00
ensureFormulaType ( computeTypeFromFormula ( formula ) ) ;
( ( FormulaValue ) _value ) . setValue ( formula ) ;
}
/ * *
* Return a formula for the cell , for example , < code > SUM ( C4 : E4 ) < / code >
*
* @return a formula for the cell
2016-07-04 07:54:20 -04:00
* @throws IllegalStateException if the cell type returned by { @link # getCellTypeEnum ( ) } is not CellType . FORMULA
2011-05-17 06:46:35 -04:00
* /
2015-11-29 23:43:32 -05:00
@Override
2011-05-17 06:46:35 -04:00
public String getCellFormula ( )
{
2016-07-04 05:45:46 -04:00
if ( _value . getType ( ) ! = CellType . FORMULA )
throw typeMismatch ( CellType . FORMULA , _value . getType ( ) , false ) ;
2011-05-17 06:46:35 -04:00
return ( ( FormulaValue ) _value ) . getValue ( ) ;
}
/ * *
* Get the value of the cell as a number .
* < p >
* For strings we throw an exception . For blank cells we return a 0 .
* For formulas or error cells we return the precalculated value ;
* < / p >
* @return the value of the cell as a number
2016-07-04 07:54:20 -04:00
* @throws IllegalStateException if the cell type returned by { @link # getCellTypeEnum ( ) } is CellType . STRING
2011-05-17 06:46:35 -04:00
* @exception NumberFormatException if the cell value isn ' t a parsable < code > double < / code > .
* @see org . apache . poi . ss . usermodel . DataFormatter for turning this number into a string similar to that which Excel would render this number as .
* /
2015-11-29 23:43:32 -05:00
@Override
2011-05-17 06:46:35 -04:00
public double getNumericCellValue ( )
{
2016-07-04 07:54:20 -04:00
CellType cellType = getCellTypeEnum ( ) ;
2011-05-17 06:46:35 -04:00
switch ( cellType )
{
2016-07-04 05:45:46 -04:00
case BLANK :
2011-05-17 06:46:35 -04:00
return 0 . 0 ;
2016-07-04 05:45:46 -04:00
case FORMULA :
2011-05-17 06:46:35 -04:00
{
FormulaValue fv = ( FormulaValue ) _value ;
2016-07-04 05:45:46 -04:00
if ( fv . getFormulaType ( ) ! = CellType . NUMERIC )
throw typeMismatch ( CellType . NUMERIC , CellType . FORMULA , false ) ;
2011-05-17 06:46:35 -04:00
return ( ( NumericFormulaValue ) _value ) . getPreEvaluatedValue ( ) ;
}
2016-07-04 05:45:46 -04:00
case NUMERIC :
2011-05-17 06:46:35 -04:00
return ( ( NumericValue ) _value ) . getValue ( ) ;
default :
2016-07-04 05:45:46 -04:00
throw typeMismatch ( CellType . NUMERIC , cellType , false ) ;
2011-05-17 06:46:35 -04:00
}
}
/ * *
* Get the value of the cell as a date .
* < p >
* For strings we throw an exception . For blank cells we return a null .
* < / p >
* @return the value of the cell as a date
2016-07-04 07:54:20 -04:00
* @throws IllegalStateException if the cell type returned by { @link # getCellTypeEnum ( ) } is CellType . STRING
2011-05-17 06:46:35 -04:00
* @exception NumberFormatException if the cell value isn ' t a parsable < code > double < / code > .
* @see org . apache . poi . ss . usermodel . DataFormatter for formatting this date into a string similar to how excel does .
* /
2015-11-29 23:43:32 -05:00
@Override
2011-05-17 06:46:35 -04:00
public Date getDateCellValue ( )
{
2016-07-04 07:54:20 -04:00
CellType cellType = getCellTypeEnum ( ) ;
2016-07-04 05:45:46 -04:00
if ( cellType = = CellType . BLANK )
2011-05-17 06:46:35 -04:00
{
return null ;
}
double value = getNumericCellValue ( ) ;
2015-07-31 13:23:01 -04:00
boolean date1904 = getSheet ( ) . getWorkbook ( ) . isDate1904 ( ) ;
2011-05-17 06:46:35 -04:00
return DateUtil . getJavaDate ( value , date1904 ) ;
}
/ * *
* Get the value of the cell as a XSSFRichTextString
* < p >
* For numeric cells we throw an exception . For blank cells we return an empty string .
* For formula cells we return the pre - calculated value if a string , otherwise an exception .
* < / p >
* @return the value of the cell as a XSSFRichTextString
* /
2015-11-29 23:43:32 -05:00
@Override
2011-05-17 06:46:35 -04:00
public RichTextString getRichStringCellValue ( )
{
2016-07-04 07:54:20 -04:00
CellType cellType = getCellTypeEnum ( ) ;
if ( getCellTypeEnum ( ) ! = CellType . STRING )
2016-07-04 05:45:46 -04:00
throw typeMismatch ( CellType . STRING , cellType , false ) ;
2011-05-19 08:00:10 -04:00
StringValue sval = ( StringValue ) _value ;
if ( sval . isRichText ( ) )
return ( ( RichTextValue ) _value ) . getValue ( ) ;
else {
String plainText = getStringCellValue ( ) ;
return getSheet ( ) . getWorkbook ( ) . getCreationHelper ( ) . createRichTextString ( plainText ) ;
}
2011-05-17 06:46:35 -04:00
}
/ * *
* Get the value of the cell as a string
* < p >
* For numeric cells we throw an exception . For blank cells we return an empty string .
* For formulaCells that are not string Formulas , we throw an exception .
* < / p >
* @return the value of the cell as a string
* /
2015-11-29 23:43:32 -05:00
@Override
2011-05-17 06:46:35 -04:00
public String getStringCellValue ( )
{
2016-07-04 07:54:20 -04:00
CellType cellType = getCellTypeEnum ( ) ;
2011-05-17 06:46:35 -04:00
switch ( cellType )
{
2016-07-04 05:45:46 -04:00
case BLANK :
2011-05-17 06:46:35 -04:00
return " " ;
2016-07-04 05:45:46 -04:00
case FORMULA :
2011-05-17 06:46:35 -04:00
{
FormulaValue fv = ( FormulaValue ) _value ;
2016-07-04 05:45:46 -04:00
if ( fv . getFormulaType ( ) ! = CellType . STRING )
throw typeMismatch ( CellType . STRING , CellType . FORMULA , false ) ;
2011-05-17 06:46:35 -04:00
return ( ( StringFormulaValue ) _value ) . getPreEvaluatedValue ( ) ;
}
2016-07-04 05:45:46 -04:00
case STRING :
2011-05-17 06:46:35 -04:00
{
if ( ( ( StringValue ) _value ) . isRichText ( ) )
return ( ( RichTextValue ) _value ) . getValue ( ) . getString ( ) ;
else
return ( ( PlainStringValue ) _value ) . getValue ( ) ;
}
default :
2016-07-04 05:45:46 -04:00
throw typeMismatch ( CellType . STRING , cellType , false ) ;
2011-05-17 06:46:35 -04:00
}
}
/ * *
* Set a boolean value for the cell
*
* @param value the boolean value to set this cell to . For formulas we ' ll set the
* precalculated value , for booleans we ' ll set its value . For other types we
* will change the cell to a boolean cell and set its value .
* /
2015-11-29 23:43:32 -05:00
@Override
2011-05-17 06:46:35 -04:00
public void setCellValue ( boolean value )
{
2016-07-04 05:45:46 -04:00
ensureTypeOrFormulaType ( CellType . BOOLEAN ) ;
if ( _value . getType ( ) = = CellType . FORMULA )
2011-05-17 06:46:35 -04:00
( ( BooleanFormulaValue ) _value ) . setPreEvaluatedValue ( value ) ;
else
( ( BooleanValue ) _value ) . setValue ( value ) ;
}
/ * *
* Set a error value for the cell
*
* @param value the error value to set this cell to . For formulas we ' ll set the
* precalculated value , for errors we ' ll set
* its value . For other types we will change the cell to an error
* cell and set its value .
* @see org . apache . poi . ss . usermodel . FormulaError
* /
2015-11-29 23:43:32 -05:00
@Override
2011-05-17 06:46:35 -04:00
public void setCellErrorValue ( byte value )
{
2016-07-04 05:45:46 -04:00
ensureType ( CellType . ERROR ) ;
if ( _value . getType ( ) = = CellType . FORMULA )
2011-05-17 06:46:35 -04:00
( ( ErrorFormulaValue ) _value ) . setPreEvaluatedValue ( value ) ;
else
( ( ErrorValue ) _value ) . setValue ( value ) ;
}
/ * *
* Get the value of the cell as a boolean .
* < p >
* For strings , numbers , and errors , we throw an exception . For blank cells we return a false .
* < / p >
* @return the value of the cell as a boolean
2016-07-04 07:54:20 -04:00
* @throws IllegalStateException if the cell type returned by { @link # getCellTypeEnum ( ) }
2016-07-04 05:45:46 -04:00
* is not CellType . BOOLEAN , CellType . BLANK or CellType . FORMULA
2011-05-17 06:46:35 -04:00
* /
2015-11-29 23:43:32 -05:00
@Override
2011-05-17 06:46:35 -04:00
public boolean getBooleanCellValue ( )
{
2016-07-04 07:54:20 -04:00
CellType cellType = getCellTypeEnum ( ) ;
2011-05-17 06:46:35 -04:00
switch ( cellType )
{
2016-07-04 05:45:46 -04:00
case BLANK :
2011-05-17 06:46:35 -04:00
return false ;
2016-07-04 05:45:46 -04:00
case FORMULA :
2011-05-17 06:46:35 -04:00
{
FormulaValue fv = ( FormulaValue ) _value ;
2016-07-04 05:45:46 -04:00
if ( fv . getFormulaType ( ) ! = CellType . BOOLEAN )
throw typeMismatch ( CellType . BOOLEAN , CellType . FORMULA , false ) ;
2011-05-17 06:46:35 -04:00
return ( ( BooleanFormulaValue ) _value ) . getPreEvaluatedValue ( ) ;
}
2016-07-04 05:45:46 -04:00
case BOOLEAN :
2011-05-17 06:46:35 -04:00
{
return ( ( BooleanValue ) _value ) . getValue ( ) ;
}
default :
2016-07-04 05:45:46 -04:00
throw typeMismatch ( CellType . BOOLEAN , cellType , false ) ;
2011-05-17 06:46:35 -04:00
}
}
/ * *
* Get the value of the cell as an error code .
* < p >
* For strings , numbers , and booleans , we throw an exception .
* For blank cells we return a 0 .
* < / p >
*
* @return the value of the cell as an error code
2016-07-04 07:54:20 -04:00
* @throws IllegalStateException if the cell type returned by { @link # getCellTypeEnum ( ) } isn ' t CellType . ERROR
2011-05-17 06:46:35 -04:00
* @see org . apache . poi . ss . usermodel . FormulaError for error codes
* /
2015-11-29 23:43:32 -05:00
@Override
2011-05-17 06:46:35 -04:00
public byte getErrorCellValue ( )
{
2016-07-04 07:54:20 -04:00
CellType cellType = getCellTypeEnum ( ) ;
2011-05-17 06:46:35 -04:00
switch ( cellType )
{
2016-07-04 05:45:46 -04:00
case BLANK :
2011-05-17 06:46:35 -04:00
return 0 ;
2016-07-04 05:45:46 -04:00
case FORMULA :
2011-05-17 06:46:35 -04:00
{
FormulaValue fv = ( FormulaValue ) _value ;
2016-07-04 05:45:46 -04:00
if ( fv . getFormulaType ( ) ! = CellType . ERROR )
throw typeMismatch ( CellType . ERROR , CellType . FORMULA , false ) ;
2011-05-17 06:46:35 -04:00
return ( ( ErrorFormulaValue ) _value ) . getPreEvaluatedValue ( ) ;
}
2016-07-04 05:45:46 -04:00
case ERROR :
2011-05-17 06:46:35 -04:00
{
return ( ( ErrorValue ) _value ) . getValue ( ) ;
}
default :
2016-07-04 05:45:46 -04:00
throw typeMismatch ( CellType . ERROR , cellType , false ) ;
2011-05-17 06:46:35 -04:00
}
}
/ * *
2016-01-01 23:29:34 -05:00
* < p > Set the style for the cell . The style should be an CellStyle created / retreived from
* the Workbook . < / p >
*
* < p > To change the style of a cell without affecting other cells that use the same style ,
2016-04-01 03:17:04 -04:00
* use { @link org . apache . poi . ss . util . CellUtil # setCellStyleProperties ( Cell , Map ) } < / p >
2016-01-01 23:29:34 -05:00
*
2011-05-17 06:46:35 -04:00
* @param style reference contained in the workbook .
* If the value is null then the style information is removed causing the cell to used the default workbook style .
2016-04-01 03:17:04 -04:00
* @see org . apache . poi . ss . usermodel . Workbook # createCellStyle
2011-05-17 06:46:35 -04:00
* /
2015-11-29 23:43:32 -05:00
@Override
2011-05-17 06:46:35 -04:00
public void setCellStyle ( CellStyle style )
{
_style = style ;
}
/ * *
* Return the cell ' s style .
*
* @return the cell ' s style . Always not - null . Default cell style has zero index and can be obtained as
* < code > workbook . getCellStyleAt ( 0 ) < / code >
2015-12-28 21:57:58 -05:00
* @see org . apache . poi . ss . usermodel . Workbook # getCellStyleAt ( int )
2011-05-17 06:46:35 -04:00
* /
2015-11-01 21:06:43 -05:00
@Override
2011-05-17 06:46:35 -04:00
public CellStyle getCellStyle ( )
{
2011-05-19 08:00:10 -04:00
if ( _style = = null ) {
SXSSFWorkbook wb = ( SXSSFWorkbook ) getRow ( ) . getSheet ( ) . getWorkbook ( ) ;
return wb . getCellStyleAt ( ( short ) 0 ) ;
} else {
return _style ;
}
2011-05-17 06:46:35 -04:00
}
/ * *
2015-12-04 01:36:27 -05:00
* { @inheritDoc }
2011-05-17 06:46:35 -04:00
* /
2015-11-29 23:43:32 -05:00
@Override
2011-05-17 06:46:35 -04:00
public void setAsActiveCell ( )
{
2015-12-04 01:36:27 -05:00
getSheet ( ) . setActiveCell ( getAddress ( ) ) ;
2011-05-17 06:46:35 -04:00
}
/ * *
* Assign a comment to this cell
*
* @param comment comment associated with this cell
* /
2015-11-29 23:43:32 -05:00
@Override
2011-05-17 06:46:35 -04:00
public void setCellComment ( Comment comment )
{
setProperty ( Property . COMMENT , comment ) ;
}
/ * *
* Returns comment associated with this cell
*
* @return comment associated with this cell or < code > null < / code > if not found
* /
2015-11-29 23:43:32 -05:00
@Override
2011-05-17 06:46:35 -04:00
public Comment getCellComment ( )
{
return ( Comment ) getPropertyValue ( Property . COMMENT ) ;
}
/ * *
* Removes the comment for this cell , if there is one .
* /
2015-11-29 23:43:32 -05:00
@Override
2011-05-17 06:46:35 -04:00
public void removeCellComment ( )
{
removeProperty ( Property . COMMENT ) ;
}
/ * *
* @return hyperlink associated with this cell or < code > null < / code > if not found
* /
2015-11-02 04:40:49 -05:00
@Override
2011-05-17 06:46:35 -04:00
public Hyperlink getHyperlink ( )
{
return ( Hyperlink ) getPropertyValue ( Property . HYPERLINK ) ;
}
/ * *
2014-11-08 08:46:30 -05:00
* Assign a hyperlink to this cell . If the supplied hyperlink is null , the
* hyperlink for this cell will be removed .
2011-05-17 06:46:35 -04:00
*
* @param link hyperlink associated with this cell
* /
2015-11-02 04:40:49 -05:00
@Override
2011-05-17 06:46:35 -04:00
public void setHyperlink ( Hyperlink link )
{
2014-11-08 08:46:30 -05:00
if ( link = = null ) {
removeHyperlink ( ) ;
return ;
}
2011-05-17 06:46:35 -04:00
setProperty ( Property . HYPERLINK , link ) ;
2011-07-12 11:18:29 -04:00
XSSFHyperlink xssfobj = ( XSSFHyperlink ) link ;
// Assign to us
CellReference ref = new CellReference ( getRowIndex ( ) , getColumnIndex ( ) ) ;
xssfobj . getCTHyperlink ( ) . setRef ( ref . formatAsString ( ) ) ;
// Add to the lists
2015-07-31 13:23:01 -04:00
getSheet ( ) . _sh . addHyperlink ( xssfobj ) ;
2011-05-17 06:46:35 -04:00
}
2014-11-08 08:46:30 -05:00
/ * *
* Removes the hyperlink for this cell , if there is one .
* /
2015-11-29 23:43:32 -05:00
@Override
2014-11-08 08:46:30 -05:00
public void removeHyperlink ( )
{
removeProperty ( Property . HYPERLINK ) ;
2015-07-31 13:23:01 -04:00
getSheet ( ) . _sh . removeHyperlink ( getRowIndex ( ) , getColumnIndex ( ) ) ;
2014-11-08 08:46:30 -05:00
}
2011-05-17 06:46:35 -04:00
/ * *
* Only valid for array formula cells
*
* @return range of the array formula group that the cell belongs to .
* /
2015-11-29 23:43:32 -05:00
// TODO: What is this?
@NotImplemented
2011-05-17 06:46:35 -04:00
public CellRangeAddress getArrayFormulaRange ( )
{
return null ;
}
/ * *
* @return < code > true < / code > if this cell is part of group of cells having a common array formula .
* /
//TODO: What is this?
2015-11-29 23:43:32 -05:00
@NotImplemented
2011-05-17 06:46:35 -04:00
public boolean isPartOfArrayFormulaGroup ( )
{
return false ;
}
//end of interface implementation
2011-05-19 08:00:10 -04:00
/ * *
* Returns a string representation of the cell
* < p >
* Formula cells return the formula string , rather than the formula result .
* Dates are displayed in dd - MMM - yyyy format
* Errors are displayed as # ERR & lt ; errIdx & gt ;
* < / p >
* /
2013-10-16 15:47:21 -04:00
@Override
2011-05-19 08:00:10 -04:00
public String toString ( ) {
2016-07-04 07:54:20 -04:00
switch ( getCellTypeEnum ( ) ) {
2016-07-04 05:45:46 -04:00
case BLANK :
2011-05-19 08:00:10 -04:00
return " " ;
2016-07-04 05:45:46 -04:00
case BOOLEAN :
2011-05-19 08:00:10 -04:00
return getBooleanCellValue ( ) ? " TRUE " : " FALSE " ;
2016-07-04 05:45:46 -04:00
case ERROR :
2011-05-19 08:00:10 -04:00
return ErrorEval . getText ( getErrorCellValue ( ) ) ;
2016-07-04 05:45:46 -04:00
case FORMULA :
2011-05-19 08:00:10 -04:00
return getCellFormula ( ) ;
2016-07-04 05:45:46 -04:00
case NUMERIC :
2011-05-19 08:00:10 -04:00
if ( DateUtil . isCellDateFormatted ( this ) ) {
2015-09-07 16:19:50 -04:00
DateFormat sdf = new SimpleDateFormat ( " dd-MMM-yyyy " , LocaleUtil . getUserLocale ( ) ) ;
2015-09-08 20:41:03 -04:00
sdf . setTimeZone ( LocaleUtil . getUserTimeZone ( ) ) ;
2011-05-19 08:00:10 -04:00
return sdf . format ( getDateCellValue ( ) ) ;
}
return getNumericCellValue ( ) + " " ;
2016-07-04 05:45:46 -04:00
case STRING :
2011-05-19 08:00:10 -04:00
return getRichStringCellValue ( ) . toString ( ) ;
default :
2016-07-04 07:54:20 -04:00
return " Unknown Cell Type: " + getCellTypeEnum ( ) ;
2011-05-19 08:00:10 -04:00
}
}
2015-11-29 23:43:32 -05:00
/*package*/ void removeProperty ( int type )
2011-05-17 06:46:35 -04:00
{
Property current = _firstProperty ;
Property previous = null ;
while ( current ! = null & & current . getType ( ) ! = type )
{
previous = current ;
current = current . _next ;
}
if ( current ! = null )
{
if ( previous ! = null )
{
previous . _next = current . _next ;
}
else
{
_firstProperty = current . _next ;
}
}
}
2015-11-29 23:43:32 -05:00
/*package*/ void setProperty ( int type , Object value )
2011-05-17 06:46:35 -04:00
{
Property current = _firstProperty ;
Property previous = null ;
while ( current ! = null & & current . getType ( ) ! = type )
{
previous = current ;
current = current . _next ;
}
if ( current ! = null )
{
current . setValue ( value ) ;
}
else
{
switch ( type )
{
case Property . COMMENT :
{
current = new CommentProperty ( value ) ;
break ;
}
case Property . HYPERLINK :
{
current = new HyperlinkProperty ( value ) ;
break ;
}
2016-06-13 02:38:53 -04:00
default :
{
throw new IllegalArgumentException ( " Invalid type: " + type ) ;
}
2011-05-17 06:46:35 -04:00
}
if ( previous ! = null )
{
previous . _next = current ;
}
else
{
_firstProperty = current ;
}
}
}
2015-11-29 23:43:32 -05:00
/*package*/ Object getPropertyValue ( int type )
2011-05-17 06:46:35 -04:00
{
return getPropertyValue ( type , null ) ;
}
2015-11-29 23:43:32 -05:00
/*package*/ Object getPropertyValue ( int type , String defaultValue )
2011-05-17 06:46:35 -04:00
{
Property current = _firstProperty ;
while ( current ! = null & & current . getType ( ) ! = type ) current = current . _next ;
return current = = null ? defaultValue : current . getValue ( ) ;
}
2015-11-29 23:43:32 -05:00
/*package*/ void ensurePlainStringType ( )
2011-05-17 06:46:35 -04:00
{
2016-07-04 05:45:46 -04:00
if ( _value . getType ( ) ! = CellType . STRING
2011-05-17 06:46:35 -04:00
| | ( ( StringValue ) _value ) . isRichText ( ) )
_value = new PlainStringValue ( ) ;
}
2015-11-29 23:43:32 -05:00
/*package*/ void ensureRichTextStringType ( )
2011-05-17 06:46:35 -04:00
{
2016-07-04 05:45:46 -04:00
if ( _value . getType ( ) ! = CellType . STRING
2011-05-17 06:46:35 -04:00
| | ! ( ( StringValue ) _value ) . isRichText ( ) )
_value = new RichTextValue ( ) ;
}
2016-07-04 05:45:46 -04:00
/*package*/ void ensureType ( CellType type )
2011-05-17 06:46:35 -04:00
{
if ( _value . getType ( ) ! = type )
setType ( type ) ;
}
2016-07-04 05:45:46 -04:00
/*package*/ void ensureFormulaType ( CellType type )
2011-05-17 06:46:35 -04:00
{
2016-07-04 05:45:46 -04:00
if ( _value . getType ( ) ! = CellType . FORMULA
2011-05-17 06:46:35 -04:00
| | ( ( FormulaValue ) _value ) . getFormulaType ( ) ! = type )
setFormulaType ( type ) ;
}
2015-11-29 23:43:32 -05:00
/ *
* Sets the cell type to type if it is different
* /
2016-07-04 05:45:46 -04:00
/*package*/ void ensureTypeOrFormulaType ( CellType type )
2011-05-17 06:46:35 -04:00
{
if ( _value . getType ( ) = = type )
{
2016-07-04 05:45:46 -04:00
if ( type = = CellType . STRING & & ( ( StringValue ) _value ) . isRichText ( ) )
setType ( CellType . STRING ) ;
2011-05-17 06:46:35 -04:00
return ;
}
2016-07-04 05:45:46 -04:00
if ( _value . getType ( ) = = CellType . FORMULA )
2011-05-17 06:46:35 -04:00
{
if ( ( ( FormulaValue ) _value ) . getFormulaType ( ) = = type )
return ;
setFormulaType ( type ) ; // once a formula, always a formula
return ;
}
setType ( type ) ;
}
2015-11-29 23:43:32 -05:00
/ * *
* changes the cell type to the specified type , and resets the value to the default value for that type
* If cell type is the same as specified type , this will reset the value to the default value for that type
*
* @param type the cell type to set
* @throws IllegalArgumentException if type is not a recognized type
* /
2016-07-04 05:45:46 -04:00
/*package*/ void setType ( CellType type )
2011-05-17 06:46:35 -04:00
{
switch ( type )
{
2016-07-04 05:45:46 -04:00
case NUMERIC :
2011-05-17 06:46:35 -04:00
{
_value = new NumericValue ( ) ;
break ;
}
2016-07-04 05:45:46 -04:00
case STRING :
2011-05-17 06:46:35 -04:00
{
2011-05-19 08:00:10 -04:00
PlainStringValue sval = new PlainStringValue ( ) ;
if ( _value ! = null ) {
// if a cell is not blank then convert the old value to string
String str = convertCellValueToString ( ) ;
sval . setValue ( str ) ;
}
_value = sval ;
2011-05-17 06:46:35 -04:00
break ;
}
2016-07-04 05:45:46 -04:00
case FORMULA :
2011-05-17 06:46:35 -04:00
{
_value = new NumericFormulaValue ( ) ;
break ;
}
2016-07-04 05:45:46 -04:00
case BLANK :
2011-05-17 06:46:35 -04:00
{
_value = new BlankValue ( ) ;
break ;
}
2016-07-04 05:45:46 -04:00
case BOOLEAN :
2011-05-17 06:46:35 -04:00
{
2011-05-19 08:00:10 -04:00
BooleanValue bval = new BooleanValue ( ) ;
if ( _value ! = null ) {
// if a cell is not blank then convert the old value to string
boolean val = convertCellValueToBoolean ( ) ;
bval . setValue ( val ) ;
}
_value = bval ;
2011-05-17 06:46:35 -04:00
break ;
}
2016-07-04 05:45:46 -04:00
case ERROR :
2011-05-17 06:46:35 -04:00
{
_value = new ErrorValue ( ) ;
break ;
}
default :
{
throw new IllegalArgumentException ( " Illegal type " + type ) ;
}
}
}
2016-07-04 05:45:46 -04:00
/*package*/ void setFormulaType ( CellType type )
2011-05-17 06:46:35 -04:00
{
2016-07-27 14:15:54 -04:00
Value prevValue = _value ;
2011-05-17 06:46:35 -04:00
switch ( type )
{
2016-07-04 05:45:46 -04:00
case NUMERIC :
2011-05-17 06:46:35 -04:00
{
_value = new NumericFormulaValue ( ) ;
break ;
}
2016-07-04 05:45:46 -04:00
case STRING :
2011-05-17 06:46:35 -04:00
{
_value = new StringFormulaValue ( ) ;
break ;
}
2016-07-04 05:45:46 -04:00
case BOOLEAN :
2011-05-17 06:46:35 -04:00
{
_value = new BooleanFormulaValue ( ) ;
break ;
}
2016-07-04 05:45:46 -04:00
case ERROR :
2011-05-17 06:46:35 -04:00
{
_value = new ErrorFormulaValue ( ) ;
break ;
}
default :
{
throw new IllegalArgumentException ( " Illegal type " + type ) ;
}
}
2016-07-27 14:15:54 -04:00
// if we had a Formula before, we should copy over the _value of the formula
if ( prevValue instanceof FormulaValue ) {
( ( FormulaValue ) _value ) . _value = ( ( FormulaValue ) prevValue ) . _value ;
}
2011-05-17 06:46:35 -04:00
}
2016-07-31 13:19:17 -04:00
2011-05-17 06:46:35 -04:00
//TODO: implement this correctly
2015-11-29 23:43:32 -05:00
@NotImplemented
2016-07-04 05:45:46 -04:00
/*package*/ CellType computeTypeFromFormula ( String formula )
2011-05-17 06:46:35 -04:00
{
2016-07-04 05:45:46 -04:00
return CellType . NUMERIC ;
2011-05-17 06:46:35 -04:00
}
//COPIED FROM https://svn.apache.org/repos/asf/poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java since the functions are declared private there
/ * *
* Used to help format error messages
* /
2016-07-04 05:45:46 -04:00
private static RuntimeException typeMismatch ( CellType expectedTypeCode , CellType actualTypeCode , boolean isFormulaCell ) {
String msg = " Cannot get a " + expectedTypeCode + " value from a " + actualTypeCode
+ " " + ( isFormulaCell ? " formula " : " " ) + " cell " ;
2011-05-17 06:46:35 -04:00
return new IllegalStateException ( msg ) ;
}
2016-07-04 05:45:46 -04:00
2011-05-19 08:00:10 -04:00
private boolean convertCellValueToBoolean ( ) {
2016-07-04 07:54:20 -04:00
CellType cellType = getCellTypeEnum ( ) ;
2011-05-19 08:00:10 -04:00
2016-07-04 05:45:46 -04:00
if ( cellType = = CellType . FORMULA ) {
2016-07-04 07:54:20 -04:00
cellType = getCachedFormulaResultTypeEnum ( ) ;
2011-05-19 08:00:10 -04:00
}
switch ( cellType ) {
2016-07-04 05:45:46 -04:00
case BOOLEAN :
2011-05-19 08:00:10 -04:00
return getBooleanCellValue ( ) ;
2016-07-04 05:45:46 -04:00
case STRING :
2011-05-19 08:00:10 -04:00
String text = getStringCellValue ( ) ;
return Boolean . parseBoolean ( text ) ;
2016-07-04 05:45:46 -04:00
case NUMERIC :
2011-05-19 08:00:10 -04:00
return getNumericCellValue ( ) ! = 0 ;
2016-07-04 05:45:46 -04:00
case ERROR :
case BLANK :
2011-05-19 08:00:10 -04:00
return false ;
2016-07-04 05:45:46 -04:00
default : throw new RuntimeException ( " Unexpected cell type ( " + cellType + " ) " ) ;
2011-05-19 08:00:10 -04:00
}
2016-07-04 05:45:46 -04:00
2011-05-19 08:00:10 -04:00
}
private String convertCellValueToString ( ) {
2016-07-04 07:54:20 -04:00
CellType cellType = getCellTypeEnum ( ) ;
2015-07-31 18:07:58 -04:00
return convertCellValueToString ( cellType ) ;
}
2016-07-04 05:45:46 -04:00
private String convertCellValueToString ( CellType cellType ) {
2011-05-19 08:00:10 -04:00
switch ( cellType ) {
2016-07-04 05:45:46 -04:00
case BLANK :
2011-05-19 08:00:10 -04:00
return " " ;
2016-07-04 05:45:46 -04:00
case BOOLEAN :
2011-05-19 08:00:10 -04:00
return getBooleanCellValue ( ) ? " TRUE " : " FALSE " ;
2016-07-04 05:45:46 -04:00
case STRING :
2011-05-19 08:00:10 -04:00
return getStringCellValue ( ) ;
2016-07-04 05:45:46 -04:00
case NUMERIC :
return Double . toString ( getNumericCellValue ( ) ) ;
case ERROR :
2011-05-19 08:00:10 -04:00
byte errVal = getErrorCellValue ( ) ;
return FormulaError . forInt ( errVal ) . getString ( ) ;
2016-07-04 05:45:46 -04:00
case FORMULA :
2015-07-31 18:07:58 -04:00
if ( _value ! = null ) {
FormulaValue fv = ( FormulaValue ) _value ;
2016-07-04 05:45:46 -04:00
if ( fv . getFormulaType ( ) ! = CellType . FORMULA ) {
2015-07-31 18:07:58 -04:00
return convertCellValueToString ( fv . getFormulaType ( ) ) ;
}
}
2011-05-19 08:00:10 -04:00
return " " ;
default :
throw new IllegalStateException ( " Unexpected cell type ( " + cellType + " ) " ) ;
}
}
2011-05-17 06:46:35 -04:00
//END OF COPIED CODE
static abstract class Property
{
final static int COMMENT = 1 ;
final static int HYPERLINK = 2 ;
Object _value ;
Property _next ;
public Property ( Object value )
{
_value = value ;
}
abstract int getType ( ) ;
void setValue ( Object value )
{
_value = value ;
}
Object getValue ( )
{
return _value ;
}
}
static class CommentProperty extends Property
{
public CommentProperty ( Object value )
{
super ( value ) ;
}
2013-10-16 15:47:21 -04:00
@Override
2011-05-17 06:46:35 -04:00
public int getType ( )
{
return COMMENT ;
}
}
static class HyperlinkProperty extends Property
{
public HyperlinkProperty ( Object value )
{
super ( value ) ;
}
2013-10-16 15:47:21 -04:00
@Override
2011-05-17 06:46:35 -04:00
public int getType ( )
{
return HYPERLINK ;
}
}
interface Value
{
2016-07-04 05:45:46 -04:00
CellType getType ( ) ;
2011-05-17 06:46:35 -04:00
}
static class NumericValue implements Value
{
double _value ;
2016-07-04 05:45:46 -04:00
public CellType getType ( )
2011-05-17 06:46:35 -04:00
{
2016-07-04 05:45:46 -04:00
return CellType . NUMERIC ;
2011-05-17 06:46:35 -04:00
}
void setValue ( double value )
{
_value = value ;
}
double getValue ( )
{
return _value ;
}
}
static abstract class StringValue implements Value
{
2016-07-04 05:45:46 -04:00
public CellType getType ( )
2011-05-17 06:46:35 -04:00
{
2016-07-04 05:45:46 -04:00
return CellType . STRING ;
2011-05-17 06:46:35 -04:00
}
2016-07-04 05:45:46 -04:00
//We cannot introduce a new type CellType.RICH_TEXT because the types are public so we have to make rich text as a type of string
2011-05-17 06:46:35 -04:00
abstract boolean isRichText ( ) ; // using the POI style which seems to avoid "instanceof".
}
static class PlainStringValue extends StringValue
{
String _value ;
void setValue ( String value )
{
_value = value ;
}
String getValue ( )
{
return _value ;
}
2013-10-16 15:47:21 -04:00
@Override
2011-05-17 06:46:35 -04:00
boolean isRichText ( )
{
return false ;
}
}
2011-05-19 08:00:10 -04:00
static class RichTextValue extends StringValue
2011-05-17 06:46:35 -04:00
{
RichTextString _value ;
2013-10-16 15:47:21 -04:00
@Override
2016-07-04 05:45:46 -04:00
public CellType getType ( )
2011-05-17 06:46:35 -04:00
{
2016-07-04 05:45:46 -04:00
return CellType . STRING ;
2011-05-17 06:46:35 -04:00
}
void setValue ( RichTextString value )
{
_value = value ;
}
RichTextString getValue ( )
{
return _value ;
}
2013-10-16 15:47:21 -04:00
@Override
2011-05-17 06:46:35 -04:00
boolean isRichText ( )
{
return true ;
}
}
static abstract class FormulaValue implements Value
{
String _value ;
2016-07-04 05:45:46 -04:00
public CellType getType ( )
2011-05-17 06:46:35 -04:00
{
2016-07-04 05:45:46 -04:00
return CellType . FORMULA ;
2011-05-17 06:46:35 -04:00
}
void setValue ( String value )
{
_value = value ;
}
String getValue ( )
{
return _value ;
}
2016-07-04 05:45:46 -04:00
abstract CellType getFormulaType ( ) ;
2011-05-17 06:46:35 -04:00
}
static class NumericFormulaValue extends FormulaValue
{
double _preEvaluatedValue ;
2013-10-16 15:47:21 -04:00
@Override
2016-07-04 05:45:46 -04:00
CellType getFormulaType ( )
2011-05-17 06:46:35 -04:00
{
2016-07-04 05:45:46 -04:00
return CellType . NUMERIC ;
2011-05-17 06:46:35 -04:00
}
void setPreEvaluatedValue ( double value )
{
_preEvaluatedValue = value ;
}
double getPreEvaluatedValue ( )
{
return _preEvaluatedValue ;
}
}
static class StringFormulaValue extends FormulaValue
{
String _preEvaluatedValue ;
2013-10-16 15:47:21 -04:00
@Override
2016-07-04 05:45:46 -04:00
CellType getFormulaType ( )
2011-05-17 06:46:35 -04:00
{
2016-07-04 05:45:46 -04:00
return CellType . STRING ;
2011-05-17 06:46:35 -04:00
}
void setPreEvaluatedValue ( String value )
{
_preEvaluatedValue = value ;
}
String getPreEvaluatedValue ( )
{
return _preEvaluatedValue ;
}
}
static class BooleanFormulaValue extends FormulaValue
{
boolean _preEvaluatedValue ;
2013-10-16 15:47:21 -04:00
@Override
2016-07-04 05:45:46 -04:00
CellType getFormulaType ( )
2011-05-17 06:46:35 -04:00
{
2016-07-04 05:45:46 -04:00
return CellType . BOOLEAN ;
2011-05-17 06:46:35 -04:00
}
void setPreEvaluatedValue ( boolean value )
{
_preEvaluatedValue = value ;
}
boolean getPreEvaluatedValue ( )
{
return _preEvaluatedValue ;
}
}
static class ErrorFormulaValue extends FormulaValue
{
byte _preEvaluatedValue ;
2013-10-16 15:47:21 -04:00
@Override
2016-07-04 05:45:46 -04:00
CellType getFormulaType ( )
2011-05-17 06:46:35 -04:00
{
2016-07-04 05:45:46 -04:00
return CellType . ERROR ;
2011-05-17 06:46:35 -04:00
}
void setPreEvaluatedValue ( byte value )
{
_preEvaluatedValue = value ;
}
byte getPreEvaluatedValue ( )
{
return _preEvaluatedValue ;
}
}
static class BlankValue implements Value
{
2016-07-04 05:45:46 -04:00
public CellType getType ( )
2011-05-17 06:46:35 -04:00
{
2016-07-04 05:45:46 -04:00
return CellType . BLANK ;
2011-05-17 06:46:35 -04:00
}
}
static class BooleanValue implements Value
{
boolean _value ;
2016-07-04 05:45:46 -04:00
public CellType getType ( )
2011-05-17 06:46:35 -04:00
{
2016-07-04 05:45:46 -04:00
return CellType . BOOLEAN ;
2011-05-17 06:46:35 -04:00
}
void setValue ( boolean value )
{
_value = value ;
}
boolean getValue ( )
{
return _value ;
}
}
static class ErrorValue implements Value
{
byte _value ;
2016-07-04 05:45:46 -04:00
public CellType getType ( )
2011-05-17 06:46:35 -04:00
{
2016-07-04 05:45:46 -04:00
return CellType . ERROR ;
2011-05-17 06:46:35 -04:00
}
void setValue ( byte value )
{
_value = value ;
}
byte getValue ( )
{
return _value ;
}
}
}