You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by ug...@apache.org on 2008/02/07 16:44:35 UTC

svn commit: r619463 - in /poi/branches/ooxml/src/ooxml: java/org/apache/poi/xssf/usermodel/XSSFCell.java testcases/org/apache/poi/xssf/io/TestLoadSaveXSSF.java testcases/org/apache/poi/xssf/usermodel/TestXSSFCell.java

Author: ugo
Date: Thu Feb  7 07:44:32 2008
New Revision: 619463

URL: http://svn.apache.org/viewvc?rev=619463&view=rev
Log:
Correct implementation of cell coordinates conversion.

Modified:
    poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java
    poi/branches/ooxml/src/ooxml/testcases/org/apache/poi/xssf/io/TestLoadSaveXSSF.java
    poi/branches/ooxml/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCell.java

Modified: poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java
URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java?rev=619463&r1=619462&r2=619463&view=diff
==============================================================================
--- poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java (original)
+++ poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java Thu Feb  7 07:44:32 2008
@@ -50,8 +50,10 @@
     
     public XSSFCell(XSSFRow row, CTCell cell) {
         this.cell = cell;
-        // TODO: parse cell.getR() to obtain cellnum
         this.row = row;
+        if (cell.getR() != null) {
+            this.cellNum = parseCellNum(cell.getR());
+        }
     }
 
     protected void setSharedStringSource(SharedStringSource sharedStringSource) {
@@ -196,7 +198,30 @@
     }
 
     public void setCellNum(short num) {
+        checkBounds(num);
         this.cellNum = num;
+        this.cell.setR(formatPosition());
+    }
+
+    protected static short parseCellNum(String r) {
+        r = r.split("\\d+")[0];
+        if (r.length() == 1) {
+            return (short) (r.charAt(0) - 'A');
+        } else {
+            return (short) (r.charAt(1) - 'A' + 26 * (r.charAt(0) - '@'));
+
+        }
+    }
+
+    protected String formatPosition() {
+        int col = this.getCellNum();
+        String result = Character.valueOf((char) (col % 26 + 'A')).toString();
+        if (col >= 26){
+            col = col / 26;
+            result = Character.valueOf((char) (col + '@')) + result;
+        }
+        result = result + String.valueOf(row.getRowNum() + 1);
+        return result;
     }
 
     public void setCellStyle(CellStyle style) {
@@ -263,6 +288,19 @@
     @Override
     public String toString() {
         return "[" + this.row.getRowNum() + "," + this.getCellNum() + "] " + this.cell.getV();
+    }
+
+    /**
+     * @throws RuntimeException if the bounds are exceeded.
+     */
+    private void checkBounds(int cellNum) {
+      if (cellNum > 255) {
+          throw new RuntimeException("You cannot have more than 255 columns "+
+                    "in a given row (IV).  Because Excel can't handle it");
+      }
+      else if (cellNum < 0) {
+          throw new RuntimeException("You cannot reference columns with an index of less then 0.");
+      }
     }
 
     

Modified: poi/branches/ooxml/src/ooxml/testcases/org/apache/poi/xssf/io/TestLoadSaveXSSF.java
URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/ooxml/testcases/org/apache/poi/xssf/io/TestLoadSaveXSSF.java?rev=619463&r1=619462&r2=619463&view=diff
==============================================================================
--- poi/branches/ooxml/src/ooxml/testcases/org/apache/poi/xssf/io/TestLoadSaveXSSF.java (original)
+++ poi/branches/ooxml/src/ooxml/testcases/org/apache/poi/xssf/io/TestLoadSaveXSSF.java Thu Feb  7 07:44:32 2008
@@ -46,8 +46,8 @@
         Sheet sheet = workbook.getSheetAt(0);
         Row row = sheet.getRow(0);
         Cell cell = row.getCell((short) 1);
-        // assertNotNull(cell);
-        // assertEquals(111.0, cell.getNumericCellValue());
+        assertNotNull(cell);
+        assertEquals(111.0, cell.getNumericCellValue());
     }
 
 }

Modified: poi/branches/ooxml/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCell.java
URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCell.java?rev=619463&r1=619462&r2=619463&view=diff
==============================================================================
--- poi/branches/ooxml/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCell.java (original)
+++ poi/branches/ooxml/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCell.java Thu Feb  7 07:44:32 2008
@@ -160,6 +160,31 @@
         cell.setCellType(Cell.CELL_TYPE_STRING);
         assertEquals("", cell.getRichStringCellValue().getString());
     }
+
+    public void testParseCellNum() {
+        assertEquals(0, XSSFCell.parseCellNum("A1"));
+        assertEquals(1, XSSFCell.parseCellNum("B1"));
+        assertEquals(1, XSSFCell.parseCellNum("B2"));
+        assertEquals(26, XSSFCell.parseCellNum("AA1"));
+        assertEquals(255, XSSFCell.parseCellNum("IV1"));
+        assertEquals(255, XSSFCell.parseCellNum("IV32768"));
+    }
+    
+    public void testFormatPosition() {
+        XSSFRow row = new XSSFRow();
+        row.setRowNum(0);
+        XSSFCell cell = new XSSFCell(row);
+        cell.setCellNum((short) 0);
+        assertEquals("A1", cell.formatPosition());
+        cell.setCellNum((short) 25);
+        assertEquals("Z1", cell.formatPosition());
+        cell.setCellNum((short) 26);
+        assertEquals("AA1", cell.formatPosition());
+        cell.setCellNum((short) 255);
+        assertEquals("IV1", cell.formatPosition());
+        row.setRowNum(32767);
+        assertEquals("IV32768", cell.formatPosition());
+    }
     
     public static class DummySharedStringSource implements SharedStringSource {
         ArrayList<String> strs = new ArrayList<String>();



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org