]> source.dussan.org Git - poi.git/commitdiff
Bug 62828: CellReference(Cell) now initializes sheet name.
authorVladislav Galas <gallon@apache.org>
Wed, 2 Jan 2019 22:08:38 +0000 (22:08 +0000)
committerVladislav Galas <gallon@apache.org>
Wed, 2 Jan 2019 22:08:38 +0000 (22:08 +0000)
Changed CellReference to CellAddress in XSSFHyperlink because it is what it should return.
Updated all relevant tests, added a test for CellReference(Cell).

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1850210 13f79535-47bb-0310-9956-ffa450edef68

build.gradle
src/java/org/apache/poi/ss/util/CellReference.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFHyperlink.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCell.java
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java
src/testcases/org/apache/poi/ss/usermodel/BaseTestSheetUpdateArrayFormulas.java
src/testcases/org/apache/poi/ss/util/TestCellReference.java

index 9c4afa814bca1384bf6c0a1dab027ba1c47a842c..cdf4f1767ecc35b422304c631ee9fafa901a50b9 100644 (file)
@@ -185,6 +185,7 @@ project('main') {
         compile 'javax.activation:activation:1.1.1'
 
         testCompile 'junit:junit:4.12'
+        testCompile 'org.mockito:mockito-core:2.21.0'
         testCompile 'org.reflections:reflections:0.9.11'
     }
 
index 3ef32e30f6aa1eafca71eb6b50858037d05b7e4e..be2737a1c1d3eb7caaaa4d7ff258caead7e2c835 100644 (file)
@@ -145,7 +145,7 @@ public class CellReference {
     }
 
     public CellReference(Cell cell) {
-        this(cell.getRowIndex(), cell.getColumnIndex(), false, false);
+        this(cell.getSheet().getSheetName(), cell.getRowIndex(), cell.getColumnIndex(), false, false);
     }
 
     public CellReference(int pRow, int pCol, boolean pAbsRow, boolean pAbsCol) {
index a1a66bf6532501476be775cd2831f590cc68b60e..864b031729b6194ceb1ba37696841fa19bb5d99b 100644 (file)
@@ -1295,7 +1295,7 @@ public final class XSSFCell implements Cell {
     public CellRangeAddress getArrayFormulaRange() {
         XSSFCell cell = getSheet().getFirstCellInArrayFormula(this);
         if (cell == null) {
-            throw new IllegalStateException("Cell " + getReference()
+            throw new IllegalStateException("Cell " + new CellReference(this).formatAsString()
                     + " is not part of an array formula.");
         }
         String formulaRef = cell._cell.getF().getRef();
index be7f6c397c7e957caba74232f2b3581c99d85d61..c02e00ae780fb5bf80c2448bd81b353ccf8ec3b9 100644 (file)
@@ -177,8 +177,7 @@ public class XSSFHyperlink implements Hyperlink {
     }
 
     /**
-     * Get the reference of the cell this applies to,
-     * es A55
+     * Get the address of the cell this hyperlink applies to, e.g. A55
      */
     public String getCellRef() {
         return _ctHyperlink.getRef();
index bbd7eda8385d02f84cc6744b4238b479596c02ef..555968c42a5c616b87cc2962b0f505aab5452d31 100644 (file)
@@ -3996,7 +3996,7 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet  {
                 return cr;
             }
         }
-        String ref = ((XSSFCell)cell).getCTCell().getR();
+        String ref = new CellReference(cell).formatAsString();
         throw new IllegalArgumentException("Cell " + ref + " is not part of an array formula.");
     }
 
index d69ab95cfb84e4ec07ae06ac0ace2c0c951df865..7d78848a29a2f12e59c573e5accb337dae718a95 100644 (file)
@@ -40,6 +40,7 @@ import org.apache.poi.ss.usermodel.Row;
 import org.apache.poi.ss.usermodel.Row.MissingCellPolicy;
 import org.apache.poi.ss.usermodel.Sheet;
 import org.apache.poi.ss.usermodel.Workbook;
+import org.apache.poi.ss.util.CellAddress;
 import org.apache.poi.ss.util.CellRangeAddress;
 import org.apache.poi.ss.util.CellReference;
 import org.apache.poi.xssf.SXSSFITestDataProvider;
@@ -597,9 +598,9 @@ public final class TestXSSFCell extends BaseTestXCell {
         final List<XSSFHyperlink> links = srcCell.getSheet().getHyperlinkList();
         assertEquals("number of hyperlinks on sheet", 2, links.size());
         assertEquals("source hyperlink",
-                new CellReference(srcCell).formatAsString(), links.get(0).getCellRef());
+                new CellAddress(srcCell).formatAsString(), links.get(0).getCellRef());
         assertEquals("destination hyperlink",
-                new CellReference(destCell).formatAsString(), links.get(1).getCellRef());
+                new CellAddress(destCell).formatAsString(), links.get(1).getCellRef());
         
         wb.close();
     }
@@ -639,7 +640,7 @@ public final class TestXSSFCell extends BaseTestXCell {
         links = srcCell.getSheet().getHyperlinkList();
         assertEquals("number of hyperlinks on sheet", 1, links.size());
         assertEquals("source hyperlink",
-                new CellReference(destCell).formatAsString(), links.get(0).getCellRef());
+                new CellAddress(destCell).formatAsString(), links.get(0).getCellRef());
         
         // Merge destCell's hyperlink to srcCell. Since destCell does have a hyperlink, this should copy destCell's hyperlink to srcCell.
         srcCell.copyCellFrom(destCell, policy);
@@ -649,9 +650,9 @@ public final class TestXSSFCell extends BaseTestXCell {
         links = srcCell.getSheet().getHyperlinkList();
         assertEquals("number of hyperlinks on sheet", 2, links.size());
         assertEquals("dest hyperlink",
-                new CellReference(destCell).formatAsString(), links.get(0).getCellRef());
+                new CellAddress(destCell).formatAsString(), links.get(0).getCellRef());
         assertEquals("source hyperlink",
-                new CellReference(srcCell).formatAsString(), links.get(1).getCellRef());
+                new CellAddress(srcCell).formatAsString(), links.get(1).getCellRef());
         
         wb.close();
     }
@@ -745,4 +746,4 @@ public final class TestXSSFCell extends BaseTestXCell {
         }
 
     }
-}
\ No newline at end of file
+}
index de828ba1c986b936ac5dffaafbb66e52ad6f5f02..85118916c87f0d0229cd7b436b959399141f7c29 100644 (file)
@@ -1506,7 +1506,7 @@ public final class TestXSSFSheet extends BaseTestXSheet {
 
         // Cell Formula
         cell = CellUtil.getCell(destRow, col++);
-        assertEquals("J7", new CellReference(cell).formatAsString());
+        assertEquals("Sheet1!J7", new CellReference(cell).formatAsString());
         assertEquals("[Cell Formula] J7 cell type", CellType.FORMULA, cell.getCellType());
         assertEquals("[Cell Formula] J7 cell formula", "5+2", cell.getCellFormula());
         //System.out.println("Cell formula evaluation currently unsupported");
@@ -1514,7 +1514,7 @@ public final class TestXSSFSheet extends BaseTestXSheet {
         // Cell Formula with Reference
         // Formula row references should be adjusted by destRowNum-srcRowNum
         cell = CellUtil.getCell(destRow, col++);
-        assertEquals("K7", new CellReference(cell).formatAsString());
+        assertEquals("Sheet1!K7", new CellReference(cell).formatAsString());
         assertEquals("[Cell Formula with Reference] K7 cell type",
                 CellType.FORMULA, cell.getCellType());
         assertEquals("[Cell Formula with Reference] K7 cell formula",
index a076c1685c19e883b15c2b23b2f4a3fb2d0c222c..9b20c6b3c63c9a20bd3d0582dd8ff5b1bef4a17b 100644 (file)
@@ -86,7 +86,7 @@ public abstract class BaseTestSheetUpdateArrayFormulas {
             cell.getArrayFormulaRange();
             fail("expected exception");
         } catch (IllegalStateException e){
-            assertEquals("Cell A1 is not part of an array formula.", e.getMessage());
+            assertEquals("Cell Sheet0!A1 is not part of an array formula.", e.getMessage());
         }
 
         // row 3 does not yet exist
@@ -177,14 +177,14 @@ public abstract class BaseTestSheetUpdateArrayFormulas {
             cell.getArrayFormulaRange();
             fail("expected exception");
         } catch (IllegalStateException e){
-            assertEquals("Cell A1 is not part of an array formula.", e.getMessage());
+            assertEquals("Cell Sheet0!A1 is not part of an array formula.", e.getMessage());
         }
 
         try {
             sheet.removeArrayFormula(cell);
             fail("expected exception");
         } catch (IllegalArgumentException e){
-            assertEquals("Cell A1 is not part of an array formula.", e.getMessage());
+            assertEquals("Cell Sheet0!A1 is not part of an array formula.", e.getMessage());
         }
         
         workbook.close();
@@ -221,7 +221,7 @@ public abstract class BaseTestSheetUpdateArrayFormulas {
                 fail("expected exception");
             } catch (IllegalArgumentException e){
                 String ref = new CellReference(acell).formatAsString();
-                assertEquals("Cell "+ref+" is not part of an array formula.", e.getMessage());
+                assertEquals("Cell " + ref + " is not part of an array formula.", e.getMessage());
             }
         }
         
index a39bd0b725c44dcf38a610ba3e9b9107e5ffe1f5..103d3926fbbfb2dd0d7a0b4c7c6d31388d6977c1 100644 (file)
@@ -19,12 +19,16 @@ package org.apache.poi.ss.util;
 
 import org.apache.poi.ss.SpreadsheetVersion;
 
+import org.apache.poi.ss.usermodel.Cell;
 import org.junit.Test;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
 
 /**
  * Tests that the common CellReference works as we need it to.
@@ -56,6 +60,27 @@ public final class TestCellReference {
 
         cellReference = new CellReference(sheet, row, col, absRow, absCol);
         assertEquals("Sheet1!A$1", cellReference.formatAsString());
+
+        cellReference = new CellReference(sheet+"!$A1");
+        assertFalse(cellReference.isRowAbsolute());
+        assertTrue(cellReference.isColAbsolute());
+
+        cellReference = new CellReference(sheet+"!A$1");
+        assertTrue(cellReference.isRowAbsolute());
+        assertFalse(cellReference.isColAbsolute());
+    }
+
+    @Test
+    public void testCtorFromCell() {
+        Cell cell = mock(Cell.class, RETURNS_DEEP_STUBS);
+        when(cell.getSheet().getSheetName()).thenReturn("sheet");
+
+        CellReference result = new CellReference(cell);
+        assertEquals("sheet", result.getSheetName());
+        assertEquals(cell.getRowIndex(), result.getRow());
+        assertEquals(cell.getColumnIndex(), result.getCol());
+        assertFalse(result.isRowAbsolute());
+        assertFalse(result.isColAbsolute());
     }
 
     @Test