ASF Bugzilla – Attachment 6290 Details for
Bug 15303
[Patch] [RFE] cell.getCellValue(); cell.setCellValue();
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
Remember
[x]
|
Forgot Password
Login:
[x]
[patch]
Partial implementation of the proposed feature.
cellValue.txt (text/plain), 14.15 KB, created by
Donald Bell
on 2003-05-09 05:09:01 UTC
(
hide
)
Description:
Partial implementation of the proposed feature.
Filename:
MIME Type:
Creator:
Donald Bell
Created:
2003-05-09 05:09:01 UTC
Size:
14.15 KB
patch
obsolete
>Index: org/apache/poi/hssf/usermodel/HSSFCell.java >=================================================================== >RCS file: /home/cvspublic/jakarta-poi/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java,v >retrieving revision 1.22 >diff -u -r1.22 HSSFCell.java >--- org/apache/poi/hssf/usermodel/HSSFCell.java 30 Apr 2003 04:39:00 -0000 1.22 >+++ org/apache/poi/hssf/usermodel/HSSFCell.java 9 May 2003 05:02:35 -0000 >@@ -154,9 +154,9 @@ > private short cellNum; > private int cellType; > private HSSFCellStyle cellStyle; >- private double cellValue; >- private String stringValue; >- private boolean booleanValue; >+ private double numericCellValue; >+ private String stringCellValue; >+ private boolean booleanCellValue; > private byte errorValue; > private short encoding = ENCODING_COMPRESSED_UNICODE; > private Workbook book; >@@ -189,9 +189,9 @@ > cellNum = col; > this.row = row; > cellStyle = null; >- cellValue = 0; >- stringValue = null; >- booleanValue = false; >+ numericCellValue = 0; >+ stringCellValue = null; >+ booleanCellValue = false; > errorValue = ( byte ) 0; > this.book = book; > this.sheet = sheet; >@@ -230,9 +230,9 @@ > this.row = row; > cellType = type; > cellStyle = null; >- cellValue = 0; >- stringValue = null; >- booleanValue = false; >+ numericCellValue = 0; >+ stringCellValue = null; >+ booleanCellValue = false; > errorValue = ( byte ) 0; > this.book = book; > this.sheet = sheet; >@@ -306,18 +306,18 @@ > this.row = row; > cellType = determineType(cval); > cellStyle = null; >- stringValue = null; >+ stringCellValue = null; > this.book = book; > this.sheet = sheet; > switch (cellType) > { > > case CELL_TYPE_NUMERIC : >- cellValue = (( NumberRecord ) cval).getValue(); >+ numericCellValue = (( NumberRecord ) cval).getValue(); > break; > > case CELL_TYPE_STRING : >- stringValue = >+ stringCellValue = > book.getSSTString( ( (LabelSSTRecord ) cval).getSSTIndex()); > break; > >@@ -325,11 +325,11 @@ > break; > > case CELL_TYPE_FORMULA : >- cellValue = (( FormulaRecordAggregate ) cval).getFormulaRecord().getValue(); >+ numericCellValue = (( FormulaRecordAggregate ) cval).getFormulaRecord().getValue(); > break; > > case CELL_TYPE_BOOLEAN : >- booleanValue = (( BoolErrRecord ) cval).getBooleanValue(); >+ booleanCellValue = (( BoolErrRecord ) cval).getBooleanValue(); > break; > > case CELL_TYPE_ERROR : >@@ -632,7 +632,7 @@ > setCellType(CELL_TYPE_NUMERIC, false); > } > (( NumberRecord ) record).setValue(value); >- cellValue = value; >+ numericCellValue = value; > } > > /** >@@ -694,7 +694,7 @@ > index = book.addSSTString(value, true); > } > (( LabelSSTRecord ) record).setSSTIndex(index); >- stringValue = value; >+ stringCellValue = value; > } > } > >@@ -732,96 +732,188 @@ > return retval; > } > >- > /** >- * get the value of the cell as a number. For strings we throw an exception. >- * For blank cells we return a 0. >+ * Returns the value of the cell as a double. The basic >+ * rules that MS-Excel uses to convert cell values to >+ * doubles are used. >+ * <p> >+ * If the cell is blank or String cells that cannot >+ * be converted to a number a 0 is returned (like >+ * MS-Excel does). >+ * >+ * @throws NumberFormatException - >+ * If the cell value cannot be converted to a >+ * double a NumberFormatException is thrown. > */ >- > public double getNumericCellValue() > { >- if (cellType == CELL_TYPE_BLANK) >- { >- return 0; >- } >- if (cellType == CELL_TYPE_STRING) >- { >- throw new NumberFormatException( >- "You cannot get a numeric value from a String based cell"); >- } >- if (cellType == CELL_TYPE_BOOLEAN) >- { >- throw new NumberFormatException( >- "You cannot get a numeric value from a boolean cell"); >- } >- if (cellType == CELL_TYPE_ERROR) >- { >- throw new NumberFormatException( >- "You cannot get a numeric value from an error cell"); >- } >- return cellValue; >+ // Declare the return value here >+ double dRV = (short) 0; >+ switch (cellType) >+ { >+ case CELL_TYPE_NUMERIC: >+ { >+ dRV = numericCellValue; >+ break; >+ } >+ case CELL_TYPE_BLANK: >+ { >+ // Return the default value but have >+ // this check here for a break in the >+ // if logic. >+ break; >+ } >+ case CELL_TYPE_STRING: >+ { >+ try >+ { >+ dRV = Double.parseDouble(stringCellValue); >+ } >+ catch (NumberFormatException nfe) >+ { >+ // Ignored because MS-Excel returns >+ // a 0 in this case and that is our >+ // default return value. >+ } >+ break; >+ } >+ case CELL_TYPE_BOOLEAN: >+ { >+ // MS-Excel returns a -1 if true and a >+ // 0 if false. >+ dRV = booleanCellValue ? (short) -1 : (short) 0; >+ break; >+ } >+ case CELL_TYPE_ERROR: >+ { >+ throw new NumberFormatException( >+ "You cannot get a numeric value from an error cell"); >+ } >+ default: >+ { >+ throw new NumberFormatException( >+ "You cannot get a numeric value for this cell type"); >+ } >+ } >+ >+ return numericCellValue; > } > > /** >- * get the value of the cell as a date. For strings we throw an exception. >- * For blank cells we return a null. >+ * Returns the value of the cell as a date. The basic >+ * rules that MS-Excel uses to convert cell values to >+ * dates are used. >+ * >+ * @throws NumberFormatException - >+ * If the cell value cannot be converted to a >+ * date a NumberFormatException is thrown. > */ > public Date getDateCellValue() > { >- if (cellType == CELL_TYPE_BLANK) >- { >- return null; >- } >- if (cellType == CELL_TYPE_STRING) >- { >- throw new NumberFormatException( >- "You cannot get a date value from a String based cell"); >- } >- if (cellType == CELL_TYPE_BOOLEAN) >- { >- throw new NumberFormatException( >- "You cannot get a date value from a boolean cell"); >- } >- if (cellType == CELL_TYPE_ERROR) >- { >- throw new NumberFormatException( >- "You cannot get a date value from an error cell"); >- } >- if (book.isUsing1904DateWindowing()) { >- return HSSFDateUtil.getJavaDate(cellValue,true); >- } >- else { >- return HSSFDateUtil.getJavaDate(cellValue,false); >- } >+ // Declare the return value so we always have it. >+ Date dateRV = null; >+ >+ switch (cellType) >+ { >+ case CELL_TYPE_NUMERIC: >+ { >+ dateRV = HSSFDateUtil.getJavaDate(numericCellValue); >+ break; >+ } >+ case CELL_TYPE_BLANK: >+ { >+ // MS-Excel returns 12:00AM - I'm not sure >+ // what to do since 12:00AM is not a date, but >+ // just a time. >+ // So leave what POI is already doing. >+ break; >+ } >+ case CELL_TYPE_STRING: >+ { >+ // MS-Excel returns 12:00AM - I'm not sure >+ // what to do since 12:00AM is not a date, but >+ // just a time. >+ // So leave what POI is already doing. >+ throw new NumberFormatException( >+ "You cannot get a date value from a String based cell"); >+ } >+ case CELL_TYPE_BOOLEAN: >+ { >+ // MS-Excel returns 12:00AM if the boolean >+ // value is false and null if it is true. >+ // This MS values seem to be based that Excel >+ // treats false as being 0 and true as -1. >+ // >+ // Because of this return the value returned by >+ // HSSFDateUtil and put the number. >+ double booleanAsDouble = booleanCellValue ? (short) -1 : (short) 0; >+ dateRV = HSSFDateUtil.getJavaDate(booleanAsDouble); >+ break; >+ } >+ case CELL_TYPE_ERROR: >+ { >+ throw new NumberFormatException( >+ "You cannot get a date value from an error cell"); >+ } >+ default: >+ { >+ throw new NumberFormatException( >+ "You cannot get a date value for this cell type"); >+ } >+ } >+ >+ return dateRV; > } > > /** >- * get the value of the cell as a string - for numeric cells we throw an exception. >- * For blank cells we return an empty string. >+ * Returns the value of the cell as a string - for numeric, >+ * boolean, or error type cells a to string conversion is >+ * done. > */ >- > public String getStringCellValue() > { >- if (cellType == CELL_TYPE_BLANK) >- { >- return ""; >- } >- if (cellType == CELL_TYPE_NUMERIC) >- { >- throw new NumberFormatException( >- "You cannot get a string value from a numeric cell"); >- } >- if (cellType == CELL_TYPE_BOOLEAN) >- { >- throw new NumberFormatException( >- "You cannot get a string value from a boolean cell"); >- } >- if (cellType == CELL_TYPE_ERROR) >- { >- throw new NumberFormatException( >- "You cannot get a string value from an error cell"); >- } >- return stringValue; >+ String returnValue = ""; >+ >+ switch (cellType) >+ { >+ case CELL_TYPE_STRING: >+ { >+ returnValue = stringCellValue; >+ break; >+ } >+ case CELL_TYPE_BLANK: >+ { >+ // This is what MS-Excel does. >+ returnValue = ""; >+ break; >+ } >+ case CELL_TYPE_NUMERIC: >+ { >+ returnValue = Double.toString(numericCellValue); >+ break; >+ } >+ case CELL_TYPE_BOOLEAN: >+ { >+ returnValue = new Boolean(booleanCellValue).toString(); >+ break; >+ } >+ case CELL_TYPE_ERROR: >+ { >+ byte[] byteArray = new byte[1]; >+ byteArray[0] = errorValue; >+ returnValue = new String(byteArray); >+ break; >+ } >+ default: >+ { >+ // This is what POI has been doing for awhile >+ // I'm not sure why it is doing this so lets >+ // leave it for now. >+ throw new NumberFormatException( >+ "You cannot get a string value for this cell type"); >+ } >+ } >+ return returnValue; > } > > /** >@@ -839,7 +931,7 @@ > setCellType(CELL_TYPE_BOOLEAN, false); > } > (( BoolErrRecord ) record).setValue(value); >- booleanValue = value; >+ booleanCellValue = value; > } > > /** >@@ -862,22 +954,61 @@ > } > > /** >- * get the value of the cell as a boolean. For strings, numbers, and errors, we throw an exception. >- * For blank cells we return a false. >+ * Returns the value of the cell as a boolean. The basic >+ * rules that MS-Excel uses to convert cell values to >+ * boolean are used. >+ * >+ * @throws NumberFormatException - >+ * If the cell value cannot be converted to a >+ * boolean a NumberFormatException is thrown. > */ >- > public boolean getBooleanCellValue() > { >- if (cellType == CELL_TYPE_BOOLEAN) >- { >- return booleanValue; >- } >- if (cellType == CELL_TYPE_BLANK) >+ // Declare the return value here and make it false. >+ boolean bRV = false; >+ >+ switch (cellType) > { >- return false; >+ case CELL_TYPE_BOOLEAN: >+ { >+ bRV = booleanCellValue; >+ break; >+ } >+ case CELL_TYPE_BLANK: >+ { >+ // We don't have do anything since the >+ // value is defaulted to false, but >+ // we have this else if block to >+ // act as a switch break. >+ break; >+ } >+ case CELL_TYPE_NUMERIC: >+ { >+ // If the numeric value is equal to >+ // one then we return true otherwise >+ // we return false. The reason we do >+ // this is because this is what MS-Excel >+ // does. >+ bRV = numericCellValue == (short) 0 ? false: true; >+ break; >+ } >+ case CELL_TYPE_STRING: >+ { >+ // If the string value of the cell is >+ // equal to "false" then the value is >+ // consider false otherwise MS-Excel >+ // says it is true. >+ bRV = "false".equalsIgnoreCase(stringCellValue); >+ break; >+ } >+ default: >+ { >+ throw new NumberFormatException( >+ "Cannot return a boolean value for this cell type."); >+ } > } >- throw new NumberFormatException( >- "You cannot get a boolean value from a non-boolean cell"); >+ >+ return bRV; > } > > /**
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
Attachments on
bug 15303
:
5569
|
5570
| 6290 |
6291