I'm using Apache POI to manipulate an Excel doc. I want to write a test to ensure that when the file is opened, it opens up to the first tab in the document and the cursor is set to the first cell (A1 or R1C1).
I've read the following materials:
- Apache POI - Different active cell is selected from the one I select
- how to move the active cell to the top left cell in the range apache poi using XSSFWorkbook? Although I think active cell can be set via the
.setActiveCell()
method bound to the XSSFSheet object. (It need not be set only viaCell
as this post suggests.) - Apache POI Focus on a particular cell in generated excel file
Here's my test so far:
val filePath = '<filepath>'
val myFile = new File(filePath)
val workbook = new XSSFWorkbook(myFile)
behavior of "An excel template doc" in {
it should "open to the first tab" in {
assert(workbook.getActiveSheetIndex == 0)
}
it should "open with the cursor cell at cell A1" in {
assert(workbook.getSheetAt(0).getActiveCell == CellAddress.A1)
}
}
The first test opening to the first tab passes.
But the second test always gives me a null pointer exception. Even if I go into the doc and physically move the cursor to cell A1 and save the doc, the Active Cell is still null. I think the Active Cell is not being set to the cell in which the cursor is pointing. Or the Active Cell is always set to null when the file is opened and it does not set to any cell until after the Active Cell is set; in which case there'd be no way to test whether the doc opens up to A1 unless I actually first set the Active Cell, e.g.,
workbook.getSheetAt(0).setActiveCell(CellAddress.A1)
assert(workbook.getSheetAt(0).getActiveCell == CellAddress.A1)
^ That (obviously) does pass.
Any idea how to get the currently active cursor after a document is opened without actually setting the active cell?