// retrieve the cell at the named range and test its contents
AreaReference aref = new AreaReference(aNamedCell.getReference());
- CellReference[] crefs = aref.getCells();
+ CellReference[] crefs = aref.getAllReferencedCells();
for (int i=0; i<crefs.length; i++) {
HSSFSheet s = wb.getSheet(crefs[i].getSheetName());
HSSFRow r = sheet.getRow(crefs[i].getRow());
// another for D12 to D14
AreaReference[] arefs = AreaReference.generateContiguous(aNamedCell.getReference());
for (int i=0; i<arefs.length; i++) {
+ // Only get the corners of the Area
+ // (use arefs[i].getAllReferencedCells() to get all cells)
CellReference[] crefs = arefs[i].getCells();
for (int j=0; j<crefs.length; j++) {
// Check it turns into real stuff
HSSFSheet s = wb.getSheet(crefs[j].getSheetName());
HSSFRow r = s.getRow(crefs[j].getRow());
HSSFCell c = r.getCell(crefs[j].getCol());
- // extract the cell contents based on cell type etc.
+ // Do something with this corner cell
}
}
</source>
public int getDim() {
return dim;
}
- /** return the cell references that define this area */
+ /**
+ * Return the cell references that define this area
+ * (i.e. the two corners)
+ */
public CellReference[] getCells() {
return cells;
}
+ /**
+ * Returns a reference to every cell covered by this area
+ */
+ public CellReference[] getAllReferencedCells() {
+ // Special case for single cell reference
+ if(cells.length == 1) {
+ return cells;
+ }
+ // Interpolate between the two
+ int minRow = Math.min(cells[0].getRow(), cells[1].getRow());
+ int maxRow = Math.max(cells[0].getRow(), cells[1].getRow());
+ int minCol = Math.min(cells[0].getCol(), cells[1].getCol());
+ int maxCol = Math.max(cells[0].getCol(), cells[1].getCol());
+
+ ArrayList refs = new ArrayList();
+ for(int row=minRow; row<=maxRow; row++) {
+ for(int col=minCol; col<=maxCol; col++) {
+ CellReference ref = new CellReference(row, col, cells[0].isRowAbsolute(), cells[0].isColAbsolute());
+ ref.setSheetName(cells[0].getSheetName());
+ refs.add(ref);
+ }
+ }
+ return (CellReference[])refs.toArray(new CellReference[refs.size()]);
+ }
public String toString() {
StringBuffer retval = new StringBuffer();
assertTrue("row is abs",cf.isRowAbsolute());
assertTrue("col is abs",cf.isColAbsolute());
assertTrue("string is $B$2",cf.toString().equals("$B$2"));
+
+ CellReference[] refs = ar.getAllReferencedCells();
+ assertEquals(4, refs.length);
+
+ assertEquals(0, refs[0].getRow());
+ assertEquals(0, refs[0].getCol());
+ assertNull(refs[0].getSheetName());
+
+ assertEquals(0, refs[1].getRow());
+ assertEquals(1, refs[1].getCol());
+ assertNull(refs[1].getSheetName());
+
+ assertEquals(1, refs[2].getRow());
+ assertEquals(0, refs[2].getCol());
+ assertNull(refs[2].getSheetName());
+
+ assertEquals(1, refs[3].getRow());
+ assertEquals(1, refs[3].getCol());
+ assertNull(refs[3].getSheetName());
}
/**
* Reported by Arne.Clauss@gedas.de
*/
public void testReferenceWithSheet() {
- String ref = "Tabelle1!$B$5";
+ String ref = "Tabelle1!B5";
AreaReference myAreaReference = new AreaReference(ref);
CellReference[] myCellReference = myAreaReference.getCells();
-
+
+ assertEquals(1, myCellReference.length);
assertNotNull("cell reference not null : "+myCellReference[0]);
assertEquals("Not Column B", (short)1,myCellReference[0].getCol());
assertEquals("Not Row 5", 4,myCellReference[0].getRow());
+ assertEquals("Shouldn't be absolute", false, myCellReference[0].isRowAbsolute());
+ assertEquals("Shouldn't be absolute", false, myCellReference[0].isColAbsolute());
+
+ assertEquals(1, myAreaReference.getAllReferencedCells().length);
+
+
+ ref = "Tabelle1!$B$5:$B$7";
+ myAreaReference = new AreaReference(ref);
+ myCellReference = myAreaReference.getCells();
+ assertEquals(2, myCellReference.length);
+
+ assertEquals("Tabelle1", myCellReference[0].getSheetName());
+ assertEquals(4, myCellReference[0].getRow());
+ assertEquals(1, myCellReference[0].getCol());
+ assertTrue(myCellReference[0].isRowAbsolute());
+ assertTrue(myCellReference[0].isColAbsolute());
+
+ assertEquals("Tabelle1", myCellReference[1].getSheetName());
+ assertEquals(6, myCellReference[1].getRow());
+ assertEquals(1, myCellReference[1].getCol());
+ assertTrue(myCellReference[1].isRowAbsolute());
+ assertTrue(myCellReference[1].isColAbsolute());
+
+ // And all that make it up
+ myCellReference = myAreaReference.getAllReferencedCells();
+ assertEquals(3, myCellReference.length);
+
+ assertEquals("Tabelle1", myCellReference[0].getSheetName());
+ assertEquals(4, myCellReference[0].getRow());
+ assertEquals(1, myCellReference[0].getCol());
+ assertTrue(myCellReference[0].isRowAbsolute());
+ assertTrue(myCellReference[0].isColAbsolute());
+
+ assertEquals("Tabelle1", myCellReference[1].getSheetName());
+ assertEquals(5, myCellReference[1].getRow());
+ assertEquals(1, myCellReference[1].getCol());
+ assertTrue(myCellReference[1].isRowAbsolute());
+ assertTrue(myCellReference[1].isColAbsolute());
+
+ assertEquals("Tabelle1", myCellReference[2].getSheetName());
+ assertEquals(6, myCellReference[2].getRow());
+ assertEquals(1, myCellReference[2].getCol());
+ assertTrue(myCellReference[2].isRowAbsolute());
+ assertTrue(myCellReference[2].isColAbsolute());
}
private static class HSSFWB extends HSSFWorkbook {