Bug 62810: AreaReference ctor looses sheet name if rows or columns swapped

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1851209 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Dominik Stadler 2019-01-13 17:14:05 +00:00
parent 88b9e48287
commit 068c820ef4
3 changed files with 50 additions and 18 deletions

View 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();

View 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());

View File

@ -16,17 +16,24 @@
==================================================================== */
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());
}
}