added HSSFName.isDeleted() to check if the name points to cell that no longer exists

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@657355 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Yegor Kozlov 2008-05-17 14:00:30 +00:00
parent 681f04c05e
commit ce8e1c9f62
5 changed files with 48 additions and 1 deletions

View File

@ -37,6 +37,7 @@
<!-- Don't forget to update status.xml too! --> <!-- Don't forget to update status.xml too! -->
<release version="3.1-beta2" date="2008-05-??"> <release version="3.1-beta2" date="2008-05-??">
<action dev="POI-DEVELOPERS" type="fix">24207 - added HSSFName.isDeleted() to check if the name points to cell that no longer exists</action>
<action dev="POI-DEVELOPERS" type="fix">40414 - fixed selected/active sheet after removing sheet from workbook</action> <action dev="POI-DEVELOPERS" type="fix">40414 - fixed selected/active sheet after removing sheet from workbook</action>
<action dev="POI-DEVELOPERS" type="fix">44523 - fixed workbook sheet selection and focus</action> <action dev="POI-DEVELOPERS" type="fix">44523 - fixed workbook sheet selection and focus</action>
<action dev="POI-DEVELOPERS" type="fix">45000 - Fixed NPE in ListLevel when numberText is null</action> <action dev="POI-DEVELOPERS" type="fix">45000 - Fixed NPE in ListLevel when numberText is null</action>

View File

@ -1225,7 +1225,19 @@ Examples:
// Do something with this corner cell // Do something with this corner cell
} }
} }
</source> </source>
<p>
Note, when a cell is deleted, Excel does not delete the attached named range.
As result, workbook can contain named ranges that point to cells that no longer exist.
You should check the validity of a reference before constructing AreaReference
</p>
<source>
if(hssfName.isDeleted()){
//named range points to a deleted cell.
} else {
AreaReference ref = new AreaReference(hssfName.getReference());
}
</source>
</section> </section>
<anchor id="CellComments"/> <anchor id="CellComments"/>
<section><title>Cell Comments</title> <section><title>Cell Comments</title>

View File

@ -34,6 +34,7 @@
<!-- Don't forget to update changes.xml too! --> <!-- Don't forget to update changes.xml too! -->
<changes> <changes>
<release version="3.1-beta2" date="2008-05-??"> <release version="3.1-beta2" date="2008-05-??">
<action dev="POI-DEVELOPERS" type="fix">24207 - added HSSFName.isDeleted() to check if the name points to cell that no longer exists</action>
<action dev="POI-DEVELOPERS" type="fix">40414 - fixed selected/active sheet after removing sheet from workbook</action> <action dev="POI-DEVELOPERS" type="fix">40414 - fixed selected/active sheet after removing sheet from workbook</action>
<action dev="POI-DEVELOPERS" type="fix">44523 - fixed workbook sheet selection and focus</action> <action dev="POI-DEVELOPERS" type="fix">44523 - fixed workbook sheet selection and focus</action>
<action dev="POI-DEVELOPERS" type="fix">45000 - Fixed NPE in ListLevel when numberText is null</action> <action dev="POI-DEVELOPERS" type="fix">45000 - Fixed NPE in ListLevel when numberText is null</action>

View File

@ -139,4 +139,13 @@ public class HSSFName {
} }
/**
* Tests if this name points to a cell that no longer exists
*
* @return true if the name refers to a deleted cell, false otherwise
*/
public boolean isDeleted(){
String ref = getReference();
return "#REF!".endsWith(ref);
}
} }

View File

@ -533,4 +533,28 @@ public final class TestNamedRange extends TestCase {
String contents = c.getStringCellValue(); String contents = c.getStringCellValue();
assertEquals("Contents of cell retrieved by its named reference", contents, cvalue); assertEquals("Contents of cell retrieved by its named reference", contents, cvalue);
} }
public void testDeletedReference() throws Exception {
HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("24207.xls");
assertEquals(2, wb.getNumberOfNames());
HSSFName name1 = wb.getNameAt(0);
assertEquals("a", name1.getNameName());
assertEquals("Sheet1!$A$1", name1.getReference());
AreaReference ref1 = new AreaReference(name1.getReference());
assertTrue("Successfully constructed first reference", true);
HSSFName name2 = wb.getNameAt(1);
assertEquals("b", name2.getNameName());
assertEquals("#REF!", name2.getReference());
assertTrue(name2.isDeleted());
try {
AreaReference ref2 = new AreaReference(name2.getReference());
fail("attempt to supply an invalid reference to AreaReference constructor results in exception");
} catch (Exception e){
;
}
}
} }