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'
}
}
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) {
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();
}
/**
- * 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();
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.");
}
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;
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();
}
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);
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();
}
}
}
-}
\ No newline at end of file
+}
// 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");
// 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",
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
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();
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());
}
}
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.
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