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
This commit is contained in:
Javen O'Neal 2015-12-04 06:36:27 +00:00
parent 293ef9fe17
commit 9c19c23291
10 changed files with 143 additions and 29 deletions

View File

@ -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();

View File

@ -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 <code>A1</code> style address of this cell
* @since 3.14beta2
*/
CellAddress getAddress();
/**
* Assign a comment to this cell
*

View File

@ -1118,4 +1118,20 @@ public interface Sheet extends Iterable<Row> {
* @return Hyperlinks for the sheet
*/
public List<? extends Hyperlink> getHyperlinkList();
/**
* Return location of the active cell, e.g. <code>A1</code>.
*
* @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. <code>A1</code>.
* @since 3.14beta2
*/
public void setActiveCell(CellAddress addr);
}

View File

@ -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());
}
/**

View File

@ -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);
}
}

View File

@ -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());
}
/**

View File

@ -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. <code>A1</code>..
* @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

View File

@ -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();
}

View File

@ -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());
}
}

View File

@ -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();
}
}