From edad0e388a11cc62993a9ccb3108438afe1a8ecf Mon Sep 17 00:00:00 2001 From: Avik Sengupta Date: Wed, 25 Dec 2002 17:27:08 +0000 Subject: [PATCH] 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 --- src/java/org/apache/poi/hssf/model/Sheet.java | 70 ++++++++++++++++++- .../apache/poi/hssf/usermodel/HSSFCell.java | 10 +++ .../poi/hssf/usermodel/TestHSSFCell.java | 48 +++++++++++++ 3 files changed, 127 insertions(+), 1 deletion(-) diff --git a/src/java/org/apache/poi/hssf/model/Sheet.java b/src/java/org/apache/poi/hssf/model/Sheet.java index 89f0704cf..2ad05c11b 100644 --- a/src/java/org/apache/poi/hssf/model/Sheet.java +++ b/src/java/org/apache/poi/hssf/model/Sheet.java @@ -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() { diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java b/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java index 2a38d6aa8..0bde7c22e 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java @@ -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); + } } diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFCell.java b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFCell.java index 23a2004a5..3484c0b42 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFCell.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFCell.java @@ -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