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;
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;
new AreaReference(st.nextToken(), version)
);
}
- return refs.toArray(new AreaReference[refs.size()]);
+ return refs.toArray(new AreaReference[0]);
}
/**
refs.add(ref);
}
}
- return refs.toArray(new CellReference[refs.size()]);
+ return refs.toArray(new CellReference[0]);
}
/**
try {
sb.append(formatAsString());
} catch(Exception e) {
- sb.append(e.toString());
+ sb.append(e);
}
sb.append(']');
return sb.toString();
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);
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());
==================================================================== */
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());
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());
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());
+ }
}