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
This commit is contained in:
parent
43b8f0d298
commit
4de18a877c
@ -36,7 +36,8 @@
|
|||||||
</devs>
|
</devs>
|
||||||
|
|
||||||
<release version="3.0.2-FINAL" date="2007-??-??">
|
<release version="3.0.2-FINAL" date="2007-??-??">
|
||||||
<action dev="POI-DEVELOPERS" type="fix">Move POIDocument out of Scratchpad, and update HSSFWorkbook to use it</action>
|
<action dev="POI-DEVELOPERS" type="add">Add a getSheetIndex(HSSFSheet) method to HSSFWorkbook, and allow a HSSFSheet to get at its parent HSSFWorkbook</action>
|
||||||
|
<action dev="POI-DEVELOPERS" type="add">Move POIDocument out of Scratchpad, and update HSSFWorkbook to use it</action>
|
||||||
<action dev="POI-DEVELOPERS" type="fix">43399 - [PATCH] - Fix for Cell References for rows > 32678</action>
|
<action dev="POI-DEVELOPERS" type="fix">43399 - [PATCH] - Fix for Cell References for rows > 32678</action>
|
||||||
<action dev="POI-DEVELOPERS" type="fix">43410 - [PATCH] - Improved Formula Parser support for numbers and ranges</action>
|
<action dev="POI-DEVELOPERS" type="fix">43410 - [PATCH] - Improved Formula Parser support for numbers and ranges</action>
|
||||||
<action dev="POI-DEVELOPERS" type="add">When writing HSLF files out, optionally preserve all OLE2 nodes (default is just the HSLF related nodes)</action>
|
<action dev="POI-DEVELOPERS" type="add">When writing HSLF files out, optionally preserve all OLE2 nodes (default is just the HSLF related nodes)</action>
|
||||||
|
@ -33,7 +33,8 @@
|
|||||||
|
|
||||||
<changes>
|
<changes>
|
||||||
<release version="3.0.2-FINAL" date="2007-??-??">
|
<release version="3.0.2-FINAL" date="2007-??-??">
|
||||||
<action dev="POI-DEVELOPERS" type="fix">Move POIDocument out of Scratchpad, and update HSSFWorkbook to use it</action>
|
<action dev="POI-DEVELOPERS" type="add">Add a getSheetIndex(HSSFSheet) method to HSSFWorkbook, and allow a HSSFSheet to get at its parent HSSFWorkbook</action>
|
||||||
|
<action dev="POI-DEVELOPERS" type="add">Move POIDocument out of Scratchpad, and update HSSFWorkbook to use it</action>
|
||||||
<action dev="POI-DEVELOPERS" type="fix">43399 - [PATCH] - Fix for Cell References for rows > 32678</action>
|
<action dev="POI-DEVELOPERS" type="fix">43399 - [PATCH] - Fix for Cell References for rows > 32678</action>
|
||||||
<action dev="POI-DEVELOPERS" type="fix">43410 - [PATCH] - Improved Formula Parser support for numbers and ranges</action>
|
<action dev="POI-DEVELOPERS" type="fix">43410 - [PATCH] - Improved Formula Parser support for numbers and ranges</action>
|
||||||
<action dev="POI-DEVELOPERS" type="add">When writing HSLF files out, optionally preserve all OLE2 nodes (default is just the HSLF related nodes)</action>
|
<action dev="POI-DEVELOPERS" type="add">When writing HSLF files out, optionally preserve all OLE2 nodes (default is just the HSLF related nodes)</action>
|
||||||
|
@ -84,6 +84,7 @@ public class HSSFSheet
|
|||||||
private Sheet sheet;
|
private Sheet sheet;
|
||||||
private TreeMap rows;
|
private TreeMap rows;
|
||||||
private Workbook book;
|
private Workbook book;
|
||||||
|
private HSSFWorkbook workbook;
|
||||||
private int firstrow;
|
private int firstrow;
|
||||||
private int lastrow;
|
private int lastrow;
|
||||||
private static POILogger log = POILogFactory.getLogger(HSSFSheet.class);
|
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
|
* Creates new HSSFSheet - called by HSSFWorkbook to create a sheet from
|
||||||
* scratch. You should not be calling this from application code (its protected anyhow).
|
* 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()
|
* @see org.apache.poi.hssf.usermodel.HSSFWorkbook#createSheet()
|
||||||
*/
|
*/
|
||||||
|
|
||||||
protected HSSFSheet(Workbook book)
|
protected HSSFSheet(HSSFWorkbook workbook)
|
||||||
{
|
{
|
||||||
sheet = Sheet.createSheet();
|
sheet = Sheet.createSheet();
|
||||||
rows = new TreeMap(); // new ArrayList(INITIAL_CAPACITY);
|
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
|
* Creates an HSSFSheet representing the given Sheet object. Should only be
|
||||||
* called by HSSFWorkbook when reading in an exisiting file.
|
* 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
|
* @param sheet - lowlevel Sheet object this sheet will represent
|
||||||
* @see org.apache.poi.hssf.usermodel.HSSFWorkbook#createSheet()
|
* @see org.apache.poi.hssf.usermodel.HSSFWorkbook#createSheet()
|
||||||
*/
|
*/
|
||||||
|
|
||||||
protected HSSFSheet(Workbook book, Sheet sheet)
|
protected HSSFSheet(HSSFWorkbook workbook, Sheet sheet)
|
||||||
{
|
{
|
||||||
this.sheet = sheet;
|
this.sheet = sheet;
|
||||||
rows = new TreeMap();
|
rows = new TreeMap();
|
||||||
this.book = book;
|
this.workbook = workbook;
|
||||||
|
this.book = workbook.getWorkbook();
|
||||||
setPropertiesFromSheet(sheet);
|
setPropertiesFromSheet(sheet);
|
||||||
}
|
}
|
||||||
|
|
||||||
HSSFSheet cloneSheet(Workbook book) {
|
HSSFSheet cloneSheet(HSSFWorkbook workbook) {
|
||||||
return new HSSFSheet(book, sheet.cloneSheet());
|
return new HSSFSheet(workbook, sheet.cloneSheet());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -223,7 +223,7 @@ public class HSSFWorkbook extends POIDocument
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
HSSFSheet hsheet = new HSSFSheet(workbook, sheet);
|
HSSFSheet hsheet = new HSSFSheet(this, sheet);
|
||||||
|
|
||||||
sheets.add(hsheet);
|
sheets.add(hsheet);
|
||||||
|
|
||||||
@ -462,6 +462,20 @@ public class HSSFWorkbook extends POIDocument
|
|||||||
return retval;
|
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<sheets.size(); i++) {
|
||||||
|
if(sheets.get(i) == sheet) {
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* create an HSSFSheet for this HSSFWorkbook, adds it to the sheets and returns
|
* create an HSSFSheet for this HSSFWorkbook, adds it to the sheets and returns
|
||||||
* the high level representation. Use this to create new sheets.
|
* the high level representation. Use this to create new sheets.
|
||||||
@ -474,7 +488,7 @@ public class HSSFWorkbook extends POIDocument
|
|||||||
|
|
||||||
// if (getNumberOfSheets() == 3)
|
// if (getNumberOfSheets() == 3)
|
||||||
// throw new RuntimeException("You cannot have more than three sheets in HSSF 1.0");
|
// throw new RuntimeException("You cannot have more than three sheets in HSSF 1.0");
|
||||||
HSSFSheet sheet = new HSSFSheet(workbook);
|
HSSFSheet sheet = new HSSFSheet(this);
|
||||||
|
|
||||||
sheets.add(sheet);
|
sheets.add(sheet);
|
||||||
workbook.setSheetName(sheets.size() - 1,
|
workbook.setSheetName(sheets.size() - 1,
|
||||||
@ -495,7 +509,7 @@ public class HSSFWorkbook extends POIDocument
|
|||||||
HSSFSheet srcSheet = (HSSFSheet)sheets.get(sheetNum);
|
HSSFSheet srcSheet = (HSSFSheet)sheets.get(sheetNum);
|
||||||
String srcName = workbook.getSheetName(sheetNum);
|
String srcName = workbook.getSheetName(sheetNum);
|
||||||
if (srcSheet != null) {
|
if (srcSheet != null) {
|
||||||
HSSFSheet clonedSheet = srcSheet.cloneSheet(workbook);
|
HSSFSheet clonedSheet = srcSheet.cloneSheet(this);
|
||||||
WindowTwoRecord windowTwo = (WindowTwoRecord) clonedSheet.getSheet().findFirstRecordBySid(WindowTwoRecord.sid);
|
WindowTwoRecord windowTwo = (WindowTwoRecord) clonedSheet.getSheet().findFirstRecordBySid(WindowTwoRecord.sid);
|
||||||
windowTwo.setSelected(sheets.size() == 1);
|
windowTwo.setSelected(sheets.size() == 1);
|
||||||
windowTwo.setPaged(sheets.size() == 1);
|
windowTwo.setPaged(sheets.size() == 1);
|
||||||
@ -534,7 +548,7 @@ public class HSSFWorkbook extends POIDocument
|
|||||||
if (workbook.doesContainsSheetName( sheetname, sheets.size() ))
|
if (workbook.doesContainsSheetName( sheetname, sheets.size() ))
|
||||||
throw new IllegalArgumentException( "The workbook already contains a sheet of this name" );
|
throw new IllegalArgumentException( "The workbook already contains a sheet of this name" );
|
||||||
|
|
||||||
HSSFSheet sheet = new HSSFSheet(workbook);
|
HSSFSheet sheet = new HSSFSheet(this);
|
||||||
|
|
||||||
sheets.add(sheet);
|
sheets.add(sheet);
|
||||||
workbook.setSheetName(sheets.size() - 1, sheetname);
|
workbook.setSheetName(sheets.size() - 1, sheetname);
|
||||||
|
@ -19,6 +19,9 @@
|
|||||||
|
|
||||||
package org.apache.poi.hssf.usermodel;
|
package org.apache.poi.hssf.usermodel;
|
||||||
|
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
|
||||||
import junit.framework.TestCase;
|
import junit.framework.TestCase;
|
||||||
|
|
||||||
import org.apache.poi.hssf.model.Sheet;
|
import org.apache.poi.hssf.model.Sheet;
|
||||||
@ -52,17 +55,59 @@ public class TestHSSFSheetOrder
|
|||||||
HSSFSheet s = wb.createSheet("Sheet " + i);
|
HSSFSheet s = wb.createSheet("Sheet " + i);
|
||||||
Sheet sheet = s.getSheet();
|
Sheet sheet = s.getSheet();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check the initial order
|
||||||
|
assertEquals(0, wb.getSheetIndex("Sheet 0"));
|
||||||
|
assertEquals(1, wb.getSheetIndex("Sheet 1"));
|
||||||
|
assertEquals(2, wb.getSheetIndex("Sheet 2"));
|
||||||
|
assertEquals(3, wb.getSheetIndex("Sheet 3"));
|
||||||
|
assertEquals(4, wb.getSheetIndex("Sheet 4"));
|
||||||
|
assertEquals(5, wb.getSheetIndex("Sheet 5"));
|
||||||
|
assertEquals(6, wb.getSheetIndex("Sheet 6"));
|
||||||
|
assertEquals(7, wb.getSheetIndex("Sheet 7"));
|
||||||
|
assertEquals(8, wb.getSheetIndex("Sheet 8"));
|
||||||
|
assertEquals(9, wb.getSheetIndex("Sheet 9"));
|
||||||
|
|
||||||
|
// Change
|
||||||
wb.getWorkbook().setSheetOrder("Sheet 6", 0);
|
wb.getWorkbook().setSheetOrder("Sheet 6", 0);
|
||||||
wb.getWorkbook().setSheetOrder("Sheet 3", 7);
|
wb.getWorkbook().setSheetOrder("Sheet 3", 7);
|
||||||
wb.getWorkbook().setSheetOrder("Sheet 1", 9);
|
wb.getWorkbook().setSheetOrder("Sheet 1", 9);
|
||||||
|
|
||||||
|
// Check they're currently right
|
||||||
|
assertEquals(0, wb.getSheetIndex("Sheet 6"));
|
||||||
|
assertEquals(1, wb.getSheetIndex("Sheet 0"));
|
||||||
|
assertEquals(2, wb.getSheetIndex("Sheet 2"));
|
||||||
|
assertEquals(3, wb.getSheetIndex("Sheet 4"));
|
||||||
|
assertEquals(4, wb.getSheetIndex("Sheet 5"));
|
||||||
|
assertEquals(5, wb.getSheetIndex("Sheet 7"));
|
||||||
|
assertEquals(6, wb.getSheetIndex("Sheet 3"));
|
||||||
|
assertEquals(7, wb.getSheetIndex("Sheet 8"));
|
||||||
|
assertEquals(8, wb.getSheetIndex("Sheet 9"));
|
||||||
|
assertEquals(9, wb.getSheetIndex("Sheet 1"));
|
||||||
|
|
||||||
//TODO read it in and see if it is correct.
|
// Read it in and see if it is correct.
|
||||||
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||||
|
wb.write(baos);
|
||||||
|
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
|
||||||
|
HSSFWorkbook wbr = new HSSFWorkbook(bais);
|
||||||
|
|
||||||
|
assertEquals(0, wbr.getSheetIndex("Sheet 6"));
|
||||||
|
assertEquals(1, wbr.getSheetIndex("Sheet 0"));
|
||||||
|
assertEquals(2, wbr.getSheetIndex("Sheet 2"));
|
||||||
|
assertEquals(3, wbr.getSheetIndex("Sheet 4"));
|
||||||
|
assertEquals(4, wbr.getSheetIndex("Sheet 5"));
|
||||||
|
assertEquals(5, wbr.getSheetIndex("Sheet 7"));
|
||||||
|
assertEquals(6, wbr.getSheetIndex("Sheet 3"));
|
||||||
|
assertEquals(7, wbr.getSheetIndex("Sheet 8"));
|
||||||
|
assertEquals(8, wbr.getSheetIndex("Sheet 9"));
|
||||||
|
assertEquals(9, wbr.getSheetIndex("Sheet 1"));
|
||||||
|
|
||||||
|
// Now get the index by the sheet, not the name
|
||||||
|
for(int i=0; i<10; i++) {
|
||||||
|
HSSFSheet s = wbr.getSheetAt(i);
|
||||||
|
assertEquals(i, wbr.getSheetIndex(s));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user