poi-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From a...@apache.org
Subject cvs commit: jakarta-poi/src/testcases/org/apache/poi/hssf/usermodel TestHSSFCell.java
Date Fri, 20 May 2005 09:13:14 GMT
avik        2005/05/20 02:13:14

  Modified:    src/java/org/apache/poi/hssf/usermodel HSSFCell.java
               src/testcases/org/apache/poi/hssf/usermodel
                        TestHSSFCell.java
  Log:
  toString() method for HSSFCell to return
  a simple string representation of the cell,
  irrespective of its type.
  Originally submitted by Wes Gilster as bug 19294
  made some modifications, added testcase.
  
  Revision  Changes    Path
  1.30      +42 -3     jakarta-poi/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java
  
  Index: HSSFCell.java
  ===================================================================
  RCS file: /home/cvs/jakarta-poi/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java,v
  retrieving revision 1.29
  retrieving revision 1.30
  diff -u -r1.29 -r1.30
  --- HSSFCell.java	17 Feb 2005 05:35:50 -0000	1.29
  +++ HSSFCell.java	20 May 2005 09:13:14 -0000	1.30
  @@ -30,6 +30,9 @@
   import org.apache.poi.hssf.record.aggregates.FormulaRecordAggregate;
   import org.apache.poi.hssf.record.formula.Ptg;
   
  +import java.text.DateFormat;
  +import java.text.DecimalFormat;
  +import java.text.SimpleDateFormat;
   import java.util.Calendar;
   import java.util.Date;
   
  @@ -38,13 +41,12 @@
    * Cells can be numeric, formula-based or string-based (text).  The cell type
    * specifies this.  String cells cannot conatin numbers and numeric cells cannot
    * contain strings (at least according to our model).  Client apps should do the
  - * conversions themselves.  Formula cells are treated like string cells, simply
  - * containing a formula string.  They'll be rendered differently.
  + * conversions themselves.  Formula cells have the formula string, as well as 
  + * the formula result, which can be numeric or string. 
    * <p>
    * Cells should have their number (0 based) before being added to a row.  Only
    * cells that have values should be added.
    * <p>
  - * NOTE: the alpha won't be implementing formulas
    *
    * @author  Andrew C. Oliver (acoliver at apache dot org)
    * @author  Dan Sherman (dsherman at isisph.com)
  @@ -942,4 +944,41 @@
           this.sheet.setActiveCellRow(this.row);
           this.sheet.setActiveCellCol(this.cellNum);
       }
  +    
  +    /**
  +     * Returns a string representation of the cell
  +     * 
  +     * This method returns a simple representation, 
  +     * anthing more complex should be in user code, with
  +     * knowledge of the semantics of the sheet being processed. 
  +     * 
  +     * 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;
  +     */
  +    public String toString() {
  +    	switch     (getCellType()) {
  +    		case CELL_TYPE_BLANK:
  +    			return "";
  +    		case CELL_TYPE_BOOLEAN:
  +    			return getBooleanCellValue()?"TRUE":"FALSE";
  +    		case CELL_TYPE_ERROR:
  +    			return "#ERR"+getErrorCellValue();
  +    		case CELL_TYPE_FORMULA:
  +    			return getCellFormula();
  +    		case CELL_TYPE_NUMERIC:
  +    			//TODO apply the dataformat for this cell
  +    			if (HSSFDateUtil.isCellDateFormatted(this)) {
  +    				DateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy");
  +    				return sdf.format(getDateCellValue());
  +    			}else {
  +    				return  getNumericCellValue() + "";
  +    			}
  +    		case CELL_TYPE_STRING:
  +    			return getStringCellValue();
  +    		default:
  +    			return "Unknown Cell Type: " + getCellType();
  +    	}
  +    }
   }
  
  
  
  1.13      +36 -0     jakarta-poi/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFCell.java
  
  Index: TestHSSFCell.java
  ===================================================================
  RCS file: /home/cvs/jakarta-poi/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFCell.java,v
  retrieving revision 1.12
  retrieving revision 1.13
  diff -u -r1.12 -r1.13
  --- TestHSSFCell.java	12 Oct 2004 05:49:01 -0000	1.12
  +++ TestHSSFCell.java	20 May 2005 09:13:14 -0000	1.13
  @@ -244,6 +244,42 @@
               in.close();
       }    
       
  +    /*tests the toString() method of HSSFCell*/
  +    public void testToString() throws Exception {
  +    	HSSFWorkbook wb = new HSSFWorkbook();
  +    	HSSFSheet s = wb.createSheet("Sheet1");
  +    	HSSFRow r = s.createRow(0);
  +    	HSSFCell c;
  +    	c=r.createCell((short) 0); c.setCellValue(true);
  +    	assertEquals("Boolean", "TRUE", c.toString());
  +    	c=r.createCell((short) 1); c.setCellValue(1.5);
  +    	assertEquals("Numeric", "1.5", c.toString());
  +    	c=r.createCell((short)(2)); c.setCellValue("Astring");
  +    	assertEquals("String", "Astring", c.toString());
  +    	c=r.createCell((short) 3); c.setCellErrorValue((byte) 7);
  +    	assertEquals("Error", "#ERR7", c.toString());
  +    	c=r.createCell((short)4); c.setCellFormula("A1+B1");
  +    	assertEquals("Formula", "A1+B1", c.toString());
  +    	
  +    	//Write out the file, read it in, and then check cell values
  +    	File f = File.createTempFile("testCellToString",".xls");
  +    	wb.write(new FileOutputStream(f));
  +    	wb = new HSSFWorkbook(new FileInputStream(f));
  +    	assertTrue("File exists and can be read", f.canRead());
  +    	
  +    	s = wb.getSheetAt(0);r=s.getRow(0);
  +    	c=r.getCell((short) 0);
  +    	assertEquals("Boolean", "TRUE", c.toString());
  +    	c=r.getCell((short) 1); 
  +    	assertEquals("Numeric", "1.5", c.toString());
  +    	c=r.getCell((short)(2)); 
  +    	assertEquals("String", "Astring", c.toString());
  +    	c=r.getCell((short) 3); 
  +    	assertEquals("Error", "#ERR7", c.toString());
  +    	c=r.getCell((short)4); 
  +    	assertEquals("Formula", "A1+B1", c.toString());
  +    }
  +    
       public static void main(String [] args) {
           System.out
           .println("Testing org.apache.poi.hssf.usermodel.TestHSSFCell");
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: poi-dev-unsubscribe@jakarta.apache.org
Mailing List:    http://jakarta.apache.org/site/mail2.html#poi
The Apache Jakarta POI Project: http://jakarta.apache.org/poi/


Mime
View raw message