]> source.dussan.org Git - poi.git/commitdiff
Bug 62810: AreaReference ctor looses sheet name if rows or columns swapped
authorDominik Stadler <centic@apache.org>
Sun, 13 Jan 2019 17:14:05 +0000 (17:14 +0000)
committerDominik Stadler <centic@apache.org>
Sun, 13 Jan 2019 17:14:05 +0000 (17:14 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1851209 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/ss/util/AreaReference.java
src/testcases/org/apache/poi/hssf/util/TestAreaReference.java
src/testcases/org/apache/poi/ss/util/TestAreaReference.java

index 821413af118f37cc7dd34d040db9ef98c7ab4b69..4dd4771be660e90e6145fca94a1c32afde06c048 100644 (file)
@@ -113,6 +113,8 @@ public class AreaReference {
         boolean swapRows = topLeft.getRow() > botRight.getRow();
         boolean swapCols = topLeft.getCol() > botRight.getCol();
         if (swapRows || swapCols) {
+            String firstSheet;
+            String lastSheet;
             int firstRow; 
             int lastRow; 
             int firstColumn; 
@@ -133,18 +135,22 @@ public class AreaReference {
                 lastRowAbs = botRight.isRowAbsolute();
             }
             if (swapCols) {
+                firstSheet = botRight.getSheetName();
                 firstColumn = botRight.getCol();
                 firstColAbs = botRight.isColAbsolute();
+                lastSheet = topLeft.getSheetName();
                 lastColumn = topLeft.getCol();
                 lastColAbs = topLeft.isColAbsolute();
             } else {
+                firstSheet = topLeft.getSheetName();
                 firstColumn = topLeft.getCol();
                 firstColAbs = topLeft.isColAbsolute();
+                lastSheet = botRight.getSheetName();
                 lastColumn = botRight.getCol();
                 lastColAbs = botRight.isColAbsolute();
             }
-            _firstCell = new CellReference(firstRow, firstColumn, firstRowAbs, firstColAbs);
-            _lastCell = new CellReference(lastRow, lastColumn, lastRowAbs, lastColAbs);
+            _firstCell = new CellReference(firstSheet, firstRow, firstColumn, firstRowAbs, firstColAbs);
+            _lastCell = new CellReference(lastSheet, lastRow, lastColumn, lastRowAbs, lastColAbs);
         } else {
             _firstCell = topLeft;
             _lastCell = botRight;
@@ -220,7 +226,7 @@ public class AreaReference {
                     new AreaReference(st.nextToken(), version)
             );
         }
-        return refs.toArray(new AreaReference[refs.size()]);
+        return refs.toArray(new AreaReference[0]);
     }
 
     /**
@@ -271,7 +277,7 @@ public class AreaReference {
                 refs.add(ref);
             }
         }
-        return refs.toArray(new CellReference[refs.size()]);
+        return refs.toArray(new CellReference[0]);
     }
 
     /**
@@ -316,7 +322,7 @@ public class AreaReference {
         try {
             sb.append(formatAsString());
         } catch(Exception e) {
-            sb.append(e.toString());
+            sb.append(e);
         }
         sb.append(']');
         return sb.toString();
index 8668b956883cd983eea23dd840765e6961a379be..4594cf56550153eae971edb039b5204cdfc84b32 100644 (file)
@@ -46,18 +46,18 @@ public final class TestAreaReference extends TestCase {
         AreaReference ar = new AreaReference("$A$1:$B$2", SpreadsheetVersion.EXCEL97);
         assertFalse("Two cells expected", ar.isSingleCell());
         CellReference cf = ar.getFirstCell();
-        assertTrue("row is 4",cf.getRow()==0);
-        assertTrue("col is 1",cf.getCol()==0);
+        assertEquals("row is 4", 0, cf.getRow());
+        assertEquals("col is 1", 0, cf.getCol());
         assertTrue("row is abs",cf.isRowAbsolute());
         assertTrue("col is abs",cf.isColAbsolute());
-        assertTrue("string is $A$1",cf.formatAsString().equals("$A$1"));
+        assertEquals("string is $A$1", "$A$1", cf.formatAsString());
 
         cf = ar.getLastCell();
-        assertTrue("row is 4",cf.getRow()==1);
-        assertTrue("col is 1",cf.getCol()==1);
+        assertEquals("row is 4", 1, cf.getRow());
+        assertEquals("col is 1", 1, cf.getCol());
         assertTrue("row is abs",cf.isRowAbsolute());
         assertTrue("col is abs",cf.isColAbsolute());
-        assertTrue("string is $B$2",cf.formatAsString().equals("$B$2"));
+        assertEquals("string is $B$2", "$B$2", cf.formatAsString());
 
         CellReference[] refs = ar.getAllReferencedCells();
         assertEquals(4, refs.length);
@@ -225,11 +225,13 @@ public final class TestAreaReference extends TestCase {
         HSSFName aNamedCell = wb.getNameAt(idx);
 
         // Should have 2 references
-        assertEquals(ref, aNamedCell.getRefersToFormula());
+        String formulaRefs = aNamedCell.getRefersToFormula();
+        assertNotNull(formulaRefs);
+        assertEquals(ref, formulaRefs);
 
         // Check the parsing of the reference into cells
-        assertFalse(AreaReference.isContiguous(aNamedCell.getRefersToFormula()));
-        AreaReference[] arefs = AreaReference.generateContiguous(SpreadsheetVersion.EXCEL97, aNamedCell.getRefersToFormula());
+        assertFalse(AreaReference.isContiguous(formulaRefs));
+        AreaReference[] arefs = AreaReference.generateContiguous(SpreadsheetVersion.EXCEL97, formulaRefs);
         assertEquals(2, arefs.length);
         assertEquals(refA, arefs[0].formatAsString());
         assertEquals(refB, arefs[1].formatAsString());
index d8416972c13ff9f04adb9c93653e60c9e5e6dd1f..d15dd5f7dd7dfee8b00169d9e993a09b4a3c8304 100644 (file)
 ==================================================================== */
 package org.apache.poi.ss.util;
 
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
 import org.apache.poi.ss.SpreadsheetVersion;
 
-import junit.framework.TestCase;
+import org.apache.poi.ss.usermodel.Sheet;
+import org.apache.poi.ss.usermodel.Workbook;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
 
 /**
  * Test for {@link AreaReference} handling of max rows.
  * 
  * @author David North
  */
-public class TestAreaReference extends TestCase {
-    
+public class TestAreaReference {
+    @Test
     public void testWholeColumn() {
         AreaReference oldStyle = AreaReference.getWholeColumn(SpreadsheetVersion.EXCEL97, "A", "B");
         assertEquals(0, oldStyle.getFirstCell().getCol());
@@ -48,7 +55,8 @@ public class TestAreaReference extends TestCase {
         AreaReference newStyleNonWholeColumn = new AreaReference("A1:B23", SpreadsheetVersion.EXCEL2007);
         assertFalse(newStyleNonWholeColumn.isWholeColumnReference());
     }
-    
+
+    @Test
     public void testWholeRow() {
         AreaReference oldStyle = AreaReference.getWholeRow(SpreadsheetVersion.EXCEL97, "1", "2");
         assertEquals(0, oldStyle.getFirstCell().getCol());
@@ -62,4 +70,20 @@ public class TestAreaReference extends TestCase {
         assertEquals(SpreadsheetVersion.EXCEL2007.getLastColumnIndex(), newStyle.getLastCell().getCol());
         assertEquals(1, newStyle.getLastCell().getRow());
     }
+
+    @Test
+    public void test62810() {
+        final Workbook wb = new HSSFWorkbook();
+        final Sheet sheet = wb.createSheet("Ctor test");
+        final String sheetName = sheet.getSheetName();
+        final CellReference topLeft = new CellReference(sheetName, 1, 1, true, true);
+        final CellReference bottomRight = new CellReference(sheetName, 5, 10, true, true);
+        final AreaReference goodAreaRef = new AreaReference(topLeft, bottomRight, SpreadsheetVersion.EXCEL2007);
+        final AreaReference badAreaRef = new AreaReference(bottomRight, topLeft, SpreadsheetVersion.EXCEL2007);
+
+        assertEquals("'Ctor test'!$B$2", topLeft.formatAsString());
+        assertEquals("'Ctor test'!$K$6", bottomRight.formatAsString());
+        assertEquals("'Ctor test'!$B$2:$K$6", goodAreaRef.formatAsString());
+        assertEquals("'Ctor test'!$B$2:$K$6", badAreaRef.formatAsString());
+    }
 }