From 3587e22c61562b841297e34ed0065047a73c87e9 Mon Sep 17 00:00:00 2001 From: Javen O'Neal Date: Fri, 4 Dec 2015 06:36:27 +0000 Subject: [PATCH] bug 58570: add get/setActiveCell to Sheet interface, add Cell.getAddress, git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1717900 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/hssf/usermodel/HSSFCell.java | 31 +++++++++---------- .../org/apache/poi/ss/usermodel/Cell.java | 9 ++++++ .../org/apache/poi/ss/usermodel/Sheet.java | 16 ++++++++++ .../apache/poi/xssf/streaming/SXSSFCell.java | 16 +++++++--- .../apache/poi/xssf/streaming/SXSSFSheet.java | 16 ++++++++++ .../apache/poi/xssf/usermodel/XSSFCell.java | 14 +++++++-- .../apache/poi/xssf/usermodel/XSSFSheet.java | 21 +++++++++++-- .../poi/xssf/usermodel/TestXSSFSheet.java | 6 ++-- .../apache/poi/ss/usermodel/BaseTestCell.java | 18 +++++++++++ .../poi/ss/usermodel/BaseTestSheet.java | 25 +++++++++++++++ 10 files changed, 143 insertions(+), 29 deletions(-) diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java b/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java index ad3635de6c..15540bc0c9 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 e3dcae0bb1..ca7738a32a 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 b759224501..56758c6c08 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 6a4cba4c7c..d4036c6fbf 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 4c86a75944..3b4bd76e13 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 040ccc7eed..b4a17688bf 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 1d927ec672..d1a652bb97 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 336901a13d..1ae86e4347 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 de405f77b9..1d20d6c225 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 0f0c92c2a1..4929659f50 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(); + } } -- 2.39.5