]> source.dussan.org Git - poi.git/commitdiff
Correct implementation of cell coordinates conversion.
authorUgo Cei <ugo@apache.org>
Thu, 7 Feb 2008 15:44:32 +0000 (15:44 +0000)
committerUgo Cei <ugo@apache.org>
Thu, 7 Feb 2008 15:44:32 +0000 (15:44 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@619463 13f79535-47bb-0310-9956-ffa450edef68

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

index 7656936d94c6fdd5b41559d82f97be8f90054504..aed5b6463ef8a0e64301449c6fc78486aff35cde 100644 (file)
@@ -50,8 +50,10 @@ public class XSSFCell implements Cell {
     
     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 class XSSFCell implements Cell {
     }
 
     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) {
@@ -265,5 +290,18 @@ public class XSSFCell implements Cell {
         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.");
+      }
+    }
+
     
 }
index 73139587451299e89fbcb7607e54bdee340e2c93..291856ccea8f0e39a7d68defa871702fbc69e736 100644 (file)
@@ -46,8 +46,8 @@ public class TestLoadSaveXSSF extends TestCase {
         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());
     }
 
 }
index 75dceb9e6c1b697da2bcd1965793988dca8118e0..fdd3c1e1ca40f76ffe95b89f6131a158886061e5 100644 (file)
@@ -160,6 +160,31 @@ public class TestXSSFCell extends TestCase {
         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>();