mirror of https://github.com/apache/poi.git
PR:15537 - set active cell in sheet; submitted by Brian Sanders
git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@352960 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
2edee14e2a
commit
0d32c9b526
|
@ -81,6 +81,7 @@ import org.apache.poi.hssf.record
|
|||
* @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
|
||||
|
@ -108,6 +109,7 @@ public class Sheet implements Model
|
|||
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
|
||||
|
@ -252,6 +254,10 @@ public class Sheet implements Model
|
|||
{
|
||||
retval.printSetup = (PrintSetupRecord) rec;
|
||||
}
|
||||
else if ( rec.getSid() == SelectionRecord.sid )
|
||||
{
|
||||
retval.selection = (SelectionRecord) rec;
|
||||
}
|
||||
|
||||
if (rec != null)
|
||||
{
|
||||
|
@ -376,7 +382,9 @@ public class Sheet implements Model
|
|||
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");
|
||||
|
@ -1935,6 +1943,66 @@ public class Sheet implements Model
|
|||
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()
|
||||
{
|
||||
|
|
|
@ -95,6 +95,7 @@ import java.util.Calendar;
|
|||
*
|
||||
* @author Andrew C. Oliver (acoliver at apache dot org)
|
||||
* @author Dan Sherman (dsherman at isisph.com)
|
||||
* @author Brian Sanders (kestrel at burdell dot org) Active Cell support
|
||||
* @version 1.0-pre
|
||||
*/
|
||||
|
||||
|
@ -973,4 +974,13 @@ public class HSSFCell
|
|||
throw new RuntimeException("You cannot reference columns with an index of less then 0.");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets this cell as the active cell for the worksheet
|
||||
*/
|
||||
public void setAsActiveCell()
|
||||
{
|
||||
this.sheet.setActiveCellRow(this.row);
|
||||
this.sheet.setActiveCellCol(this.cellNum);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -176,6 +176,54 @@ extends TestCase {
|
|||
.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
|
||||
|
|
Loading…
Reference in New Issue