From 66ec0e4a515993d642723cf818fbcc7938b37564 Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Mon, 24 Sep 2007 12:57:38 +0000 Subject: [PATCH] Allow HSSFSheet to get at its parent workbook, and add HSSFWorkbook.getSheetIndex(HSSFSheet) git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@578795 13f79535-47bb-0310-9956-ffa450edef68 --- src/documentation/content/xdocs/changes.xml | 3 +- src/documentation/content/xdocs/status.xml | 3 +- .../apache/poi/hssf/usermodel/HSSFSheet.java | 19 +++--- .../poi/hssf/usermodel/HSSFWorkbook.java | 22 +++++-- .../hssf/usermodel/TestHSSFSheetOrder.java | 59 ++++++++++++++++--- 5 files changed, 85 insertions(+), 21 deletions(-) diff --git a/src/documentation/content/xdocs/changes.xml b/src/documentation/content/xdocs/changes.xml index 97fce850c2..bd3f196e37 100644 --- a/src/documentation/content/xdocs/changes.xml +++ b/src/documentation/content/xdocs/changes.xml @@ -36,7 +36,8 @@ - Move POIDocument out of Scratchpad, and update HSSFWorkbook to use it + Add a getSheetIndex(HSSFSheet) method to HSSFWorkbook, and allow a HSSFSheet to get at its parent HSSFWorkbook + Move POIDocument out of Scratchpad, and update HSSFWorkbook to use it 43399 - [PATCH] - Fix for Cell References for rows > 32678 43410 - [PATCH] - Improved Formula Parser support for numbers and ranges When writing HSLF files out, optionally preserve all OLE2 nodes (default is just the HSLF related nodes) diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index e5cde9195d..074b386de0 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -33,7 +33,8 @@ - Move POIDocument out of Scratchpad, and update HSSFWorkbook to use it + Add a getSheetIndex(HSSFSheet) method to HSSFWorkbook, and allow a HSSFSheet to get at its parent HSSFWorkbook + Move POIDocument out of Scratchpad, and update HSSFWorkbook to use it 43399 - [PATCH] - Fix for Cell References for rows > 32678 43410 - [PATCH] - Improved Formula Parser support for numbers and ranges When writing HSLF files out, optionally preserve all OLE2 nodes (default is just the HSLF related nodes) diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java b/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java index bf5246cb28..9d7c287cb8 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java @@ -84,6 +84,7 @@ public class HSSFSheet private Sheet sheet; private TreeMap rows; private Workbook book; + private HSSFWorkbook workbook; private int firstrow; private int lastrow; private static POILogger log = POILogFactory.getLogger(HSSFSheet.class); @@ -92,36 +93,38 @@ public class HSSFSheet * Creates new HSSFSheet - called by HSSFWorkbook to create a sheet from * scratch. You should not be calling this from application code (its protected anyhow). * - * @param book - lowlevel Workbook object associated with the sheet. + * @param workbook - The HSSF Workbook object associated with the sheet. * @see org.apache.poi.hssf.usermodel.HSSFWorkbook#createSheet() */ - protected HSSFSheet(Workbook book) + protected HSSFSheet(HSSFWorkbook workbook) { sheet = Sheet.createSheet(); rows = new TreeMap(); // new ArrayList(INITIAL_CAPACITY); - this.book = book; + this.workbook = workbook; + this.book = workbook.getWorkbook(); } /** * Creates an HSSFSheet representing the given Sheet object. Should only be * called by HSSFWorkbook when reading in an exisiting file. * - * @param book - lowlevel Workbook object associated with the sheet. + * @param workbook - The HSSF Workbook object associated with the sheet. * @param sheet - lowlevel Sheet object this sheet will represent * @see org.apache.poi.hssf.usermodel.HSSFWorkbook#createSheet() */ - protected HSSFSheet(Workbook book, Sheet sheet) + protected HSSFSheet(HSSFWorkbook workbook, Sheet sheet) { this.sheet = sheet; rows = new TreeMap(); - this.book = book; + this.workbook = workbook; + this.book = workbook.getWorkbook(); setPropertiesFromSheet(sheet); } - HSSFSheet cloneSheet(Workbook book) { - return new HSSFSheet(book, sheet.cloneSheet()); + HSSFSheet cloneSheet(HSSFWorkbook workbook) { + return new HSSFSheet(workbook, sheet.cloneSheet()); } diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java b/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java index 8748270998..691dc1e4d0 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java @@ -223,7 +223,7 @@ public class HSSFWorkbook extends POIDocument break; } - HSSFSheet hsheet = new HSSFSheet(workbook, sheet); + HSSFSheet hsheet = new HSSFSheet(this, sheet); sheets.add(hsheet); @@ -462,6 +462,20 @@ public class HSSFWorkbook extends POIDocument return retval; } + /** Returns the index of the given sheet + * @param sheet the sheet to look up + * @return index of the sheet (0 based) + */ + public int getSheetIndex(HSSFSheet sheet) + { + for(int i=0; i