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;
/**
* @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
_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
}
/**
- * Sets this cell as the active cell for the worksheet
+ * {@inheritDoc}
*/
+ @Override
public void setAsActiveCell()
{
int row=_record.getRow();
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;
/**
*/
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
*
* @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);
}
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;
return _row.getRowNum();
}
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public CellAddress getAddress() {
+ return new CellAddress(this);
+ }
+
/**
* Returns the sheet this cell belongs to
*
}
/**
- * 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());
}
/**
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);
+ }
}
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.
*
}
/**
- * Sets this cell as the active cell for the worksheet.
+ * {@inheritDoc}
*/
@Override
public void setAsActiveCell() {
- getSheet().setActiveCell(getReference());
+ getSheet().setActiveCell(getAddress());
}
/**
*
* @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();
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
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;
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();
}
}
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());
+ }
}
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();
+ }
}