diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java b/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java index ad3635de6..15540bc0c 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java @@ -48,6 +48,7 @@ import org.apache.poi.ss.usermodel.Comment; import org.apache.poi.ss.usermodel.FormulaError; import org.apache.poi.ss.usermodel.Hyperlink; import org.apache.poi.ss.usermodel.RichTextString; +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.ss.util.NumberToTextConverter; @@ -223,18 +224,10 @@ public class HSSFCell implements Cell { /** * @return the (zero based) index of the row containing this cell */ + @Override public int getRowIndex() { return _record.getRow(); } - /** - * Set the cell's number within the row (0 based). - * @param num short the cell number - * @deprecated (Jan 2008) Doesn't update the row's idea of what cell this is, use {@link HSSFRow#moveCell(HSSFCell, short)} instead - */ - public void setCellNum(short num) - { - _record.setColumn(num); - } /** * Updates the cell record's idea of what @@ -246,17 +239,20 @@ public class HSSFCell implements Cell { _record.setColumn(num); } - /** - * @deprecated (Oct 2008) use {@link #getColumnIndex()} - */ - public short getCellNum() { - return (short) getColumnIndex(); - } - + @Override public int getColumnIndex() { return _record.getColumn() & 0xFFFF; } + /** + * {@inheritDoc} + */ + @Override + public CellAddress getAddress() { + return new CellAddress(this); + } + + /** * Set the cells type (numeric, formula or string). * If the cell currently contains a value, the value will @@ -950,8 +946,9 @@ public class HSSFCell implements Cell { } /** - * Sets this cell as the active cell for the worksheet + * {@inheritDoc} */ + @Override public void setAsActiveCell() { int row=_record.getRow(); diff --git a/src/java/org/apache/poi/ss/usermodel/Cell.java b/src/java/org/apache/poi/ss/usermodel/Cell.java index e3dcae0bb..ca7738a32 100644 --- a/src/java/org/apache/poi/ss/usermodel/Cell.java +++ b/src/java/org/apache/poi/ss/usermodel/Cell.java @@ -21,6 +21,7 @@ import java.util.Calendar; import java.util.Date; import org.apache.poi.ss.formula.FormulaParseException; +import org.apache.poi.ss.util.CellAddress; import org.apache.poi.ss.util.CellRangeAddress; /** @@ -349,6 +350,14 @@ public interface Cell { */ void setAsActiveCell(); + /** + * Gets the address of this cell + * + * @return A1 style address of this cell + * @since 3.14beta2 + */ + CellAddress getAddress(); + /** * Assign a comment to this cell * diff --git a/src/java/org/apache/poi/ss/usermodel/Sheet.java b/src/java/org/apache/poi/ss/usermodel/Sheet.java index b75922450..56758c6c0 100644 --- a/src/java/org/apache/poi/ss/usermodel/Sheet.java +++ b/src/java/org/apache/poi/ss/usermodel/Sheet.java @@ -1118,4 +1118,20 @@ public interface Sheet extends Iterable { * @return Hyperlinks for the sheet */ public List getHyperlinkList(); + + /** + * Return location of the active cell, e.g. A1. + * + * @return the location of the active cell. + * @since 3.14beta2 + */ + public CellAddress getActiveCell(); + + /** + * Sets location of the active cell + * + * @param cellRef the location of the active cell, e.g. A1. + * @since 3.14beta2 + */ + public void setActiveCell(CellAddress addr); } diff --git a/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFCell.java b/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFCell.java index 6a4cba4c7..d4036c6fb 100644 --- a/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFCell.java +++ b/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFCell.java @@ -33,6 +33,7 @@ import org.apache.poi.ss.usermodel.FormulaError; import org.apache.poi.ss.usermodel.Hyperlink; import org.apache.poi.ss.usermodel.RichTextString; import org.apache.poi.ss.usermodel.Row; +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.util.LocaleUtil; @@ -83,6 +84,14 @@ public class SXSSFCell implements Cell { return _row.getRowNum(); } + /** + * {@inheritDoc} + */ + @Override + public CellAddress getAddress() { + return new CellAddress(this); + } + /** * Returns the sheet this cell belongs to * @@ -564,15 +573,12 @@ public class SXSSFCell implements Cell { } /** - * Sets this cell as the active cell for the worksheet + * {@inheritDoc} */ - @NotImplemented @Override public void setAsActiveCell() { - throw new RuntimeException("NotImplemented"); - //TODO: What needs to be done here? Is there a "the active cell" at the sheet or even the workbook level? - //getRow().setAsActiveCell(this); + getSheet().setActiveCell(getAddress()); } /** diff --git a/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFSheet.java b/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFSheet.java index 4c86a7594..3b4bd76e1 100644 --- a/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFSheet.java +++ b/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFSheet.java @@ -1874,4 +1874,20 @@ public class SXSSFSheet implements Sheet, Cloneable public int getColumnOutlineLevel(int columnIndex) { return _sh.getColumnOutlineLevel(columnIndex); } + + /** + * {@inheritDoc} + */ + @Override + public CellAddress getActiveCell() { + return _sh.getActiveCell(); + } + + /** + * {@inheritDoc} + */ + @Override + public void setActiveCell(CellAddress addr) { + _sh.setActiveCell(addr); + } } diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java index 040ccc7ee..b4a17688b 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java @@ -586,11 +586,19 @@ public final class XSSFCell implements Cell { public String getReference() { String ref = _cell.getR(); if(ref == null) { - return new CellAddress(this).formatAsString(); + return getAddress().formatAsString(); } return ref; } + /** + * {@inheritDoc} + */ + @Override + public CellAddress getAddress() { + return new CellAddress(this); + } + /** * Return the cell's style. * @@ -816,11 +824,11 @@ public final class XSSFCell implements Cell { } /** - * Sets this cell as the active cell for the worksheet. + * {@inheritDoc} */ @Override public void setAsActiveCell() { - getSheet().setActiveCell(getReference()); + getSheet().setActiveCell(getAddress()); } /** diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java index 1d927ec67..d1a652bb9 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java @@ -3110,14 +3110,20 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { * * @return the location of the active cell. */ - public String getActiveCell() { - return getSheetTypeSelection().getActiveCell(); + @Override + public CellAddress getActiveCell() { + String address = getSheetTypeSelection().getActiveCell(); + if (address == null) { + return null; + } + return new CellAddress(address); } /** * Sets location of the active cell * * @param cellRef the location of the active cell, e.g. A1.. + * @deprecated 3.14beta2 (circa 2015-12-05). Use {@link #setActiveCell(CellAddress)} instead. */ public void setActiveCell(String cellRef) { CTSelection ctsel = getSheetTypeSelection(); @@ -3125,6 +3131,17 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { ctsel.setSqref(Arrays.asList(cellRef)); } + /** + * {@inheritDoc} + */ + @Override + public void setActiveCell(CellAddress address) { + String ref = address.formatAsString(); + CTSelection ctsel = getSheetTypeSelection(); + ctsel.setActiveCell(ref); + ctsel.setSqref(Arrays.asList(ref)); + } + /** * Does this sheet have any comments on it? We need to know, * so we can decide about writing it to disk or not diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java index 336901a13..1ae86e434 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java @@ -50,6 +50,7 @@ import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.util.AreaReference; +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.ss.util.CellUtil; @@ -235,9 +236,10 @@ public final class TestXSSFSheet extends BaseTestSheet { public void getActiveCell() throws IOException { XSSFWorkbook workbook = new XSSFWorkbook(); XSSFSheet sheet = workbook.createSheet(); - sheet.setActiveCell("R5"); + CellAddress R5 = new CellAddress("R5"); + sheet.setActiveCell(R5); - assertEquals("R5", sheet.getActiveCell()); + assertEquals(R5, sheet.getActiveCell()); workbook.close(); } diff --git a/src/testcases/org/apache/poi/ss/usermodel/BaseTestCell.java b/src/testcases/org/apache/poi/ss/usermodel/BaseTestCell.java index de405f77b..1d20d6c22 100644 --- a/src/testcases/org/apache/poi/ss/usermodel/BaseTestCell.java +++ b/src/testcases/org/apache/poi/ss/usermodel/BaseTestCell.java @@ -936,4 +936,22 @@ public abstract class BaseTestCell { } wb.close(); } + + /** + * Tests that the setAsActiveCell and getActiveCell function pairs work together + */ + @Test + public void setAsActiveCell() throws IOException { + Workbook wb = _testDataProvider.createWorkbook(); + Sheet sheet = wb.createSheet(); + Row row = sheet.createRow(0); + Cell A1 = row.createCell(0); + Cell B1 = row.createCell(1); + + A1.setAsActiveCell(); + assertEquals(A1.getAddress(), sheet.getActiveCell()); + + B1.setAsActiveCell(); + assertEquals(B1.getAddress(), sheet.getActiveCell()); + } } diff --git a/src/testcases/org/apache/poi/ss/usermodel/BaseTestSheet.java b/src/testcases/org/apache/poi/ss/usermodel/BaseTestSheet.java index 0f0c92c2a..4929659f5 100644 --- a/src/testcases/org/apache/poi/ss/usermodel/BaseTestSheet.java +++ b/src/testcases/org/apache/poi/ss/usermodel/BaseTestSheet.java @@ -1152,4 +1152,29 @@ public abstract class BaseTestSheet { assertTrue(sheet.getMergedRegions().isEmpty()); wb.close(); } + + /** + * Tests that the setAsActiveCell and getActiveCell function pairs work together + */ + @Test + public void setActiveCell() throws IOException { + Workbook wb1 = _testDataProvider.createWorkbook(); + Sheet sheet = wb1.createSheet(); + CellAddress B42 = new CellAddress("B42"); + + // active cell behavior is undefined if not set. + // HSSFSheet defaults to A1 active cell, while XSSFSheet defaults to null. + if (sheet.getActiveCell() != null && !sheet.getActiveCell().equals(CellAddress.A1)) { + fail("If not set, active cell should default to null or A1"); + } + + sheet.setActiveCell(B42); + + Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1); + + assertEquals(B42, sheet.getActiveCell()); + + wb1.close(); + wb2.close(); + } }