* @author Glen Stampoultzis (glens at apache.org)
* @author Shawn Laubach (slaubach at apache dot org) Gridlines, Headers, Footers, and PrintSetup
* @author Jason Height (jheight at chariot dot net dot au) Clone support
+ * @author Brian Sanders (kestrel at burdell dot org) Active Cell support
*
* @see org.apache.poi.hssf.model.Workbook
* @see org.apache.poi.hssf.usermodel.HSSFSheet
protected FooterRecord footer = null;
protected PrintGridlinesRecord printGridlines = null;
protected MergeCellsRecord merged = null;
+ protected SelectionRecord selection = null;
protected int mergedloc = 0;
private static POILogger log = POILogFactory.getLogger(Sheet.class);
private ArrayList columnSizes = null; // holds column info
{
retval.printSetup = (PrintSetupRecord) rec;
}
+ else if ( rec.getSid() == SelectionRecord.sid )
+ {
+ retval.selection = (SelectionRecord) rec;
+ }
if (rec != null)
{
records.add(retval.dims);
records.add(retval.createWindowTwo());
retval.setLoc(records.size() - 1);
- records.add(retval.createSelection());
+ retval.selection =
+ (SelectionRecord) retval.createSelection();
+ records.add(retval.selection);
records.add(retval.createEOF());
retval.records = records;
log.log(log.DEBUG, "Sheet createsheet from scratch exit");
retval.setNumRefs(( short ) 0x0);
return retval;
}
+
+ /**
+ * Returns the active row
+ *
+ * @see org.apache.poi.hssf.record.SelectionRecord
+ * @return row the active row index
+ */
+ public int getActiveCellRow()
+ {
+ if (selection == null)
+ {
+ return 0;
+ }
+ return selection.getActiveCellRow();
+ }
+
+ /**
+ * Sets the active row
+ *
+ * @param row the row index
+ * @see org.apache.poi.hssf.record.SelectionRecord
+ */
+ public void setActiveCellRow(int row)
+ {
+ //shouldn't have a sheet w/o a SelectionRecord, but best to guard anyway
+ if (selection != null)
+ {
+ selection.setActiveCellRow(row);
+ }
+ }
+
+ /**
+ * Returns the active column
+ *
+ * @see org.apache.poi.hssf.record.SelectionRecord
+ * @return row the active column index
+ */
+ public short getActiveCellCol()
+ {
+ if (selection == null)
+ {
+ return (short) 0;
+ }
+ return selection.getActiveCellCol();
+ }
+
+ /**
+ * Sets the active column
+ *
+ * @param col the column index
+ * @see org.apache.poi.hssf.record.SelectionRecord
+ */
+ public void setActiveCellCol(short col)
+ {
+ //shouldn't have a sheet w/o a SelectionRecord, but best to guard anyway
+ if (selection != null)
+ {
+ selection.setActiveCellCol(col);
+ }
+ }
protected Record createMergedCells()
{
.getDateCellValue().getTime());
stream.close();
}
+
+ /**
+ * Tests that the active cell can be correctly read and set
+ */
+ public void testActiveCell() throws Exception
+ {
+ //read in sample
+ String dir = System.getProperty("HSSF.testdata.path");
+ File sample = new File(dir + "/Simple.xls");
+ assertTrue("Simple.xls exists and is readable", sample.canRead());
+ FileInputStream fis = new FileInputStream(sample);
+ HSSFWorkbook book = new HSSFWorkbook(fis);
+ fis.close();
+
+ //check initial position
+ HSSFSheet umSheet = book.getSheetAt(0);
+ Sheet s = umSheet.getSheet();
+ assertEquals("Initial active cell should be in col 0",
+ (short) 0, s.getActiveCellCol());
+ assertEquals("Initial active cell should be on row 1",
+ 1, s.getActiveCellRow());
+
+ //modify position through HSSFCell
+ HSSFCell cell = umSheet.createRow(3).createCell((short) 2);
+ cell.setAsActiveCell();
+ assertEquals("After modify, active cell should be in col 2",
+ (short) 2, s.getActiveCellCol());
+ assertEquals("After modify, active cell should be on row 3",
+ 3, s.getActiveCellRow());
+
+ //write book to temp file; read and verify that position is serialized
+ File temp = File.createTempFile("testActiveCell", ".xls");
+ FileOutputStream fos = new FileOutputStream(temp);
+ book.write(fos);
+ fos.close();
+
+ fis = new FileInputStream(temp);
+ book = new HSSFWorkbook(fis);
+ fis.close();
+ temp.delete();
+ umSheet = book.getSheetAt(0);
+ s = umSheet.getSheet();
+
+ assertEquals("After serialize, active cell should be in col 2",
+ (short) 2, s.getActiveCellCol());
+ assertEquals("After serialize, active cell should be on row 3",
+ 3, s.getActiveCellRow());
+ }
public static void main(String [] args) {
System.out