diff --git a/src/documentation/content/xdocs/changes.xml b/src/documentation/content/xdocs/changes.xml index e837cb027c..65fe1e25c4 100644 --- a/src/documentation/content/xdocs/changes.xml +++ b/src/documentation/content/xdocs/changes.xml @@ -37,6 +37,7 @@ + 24207 - added HSSFName.isDeleted() to check if the name points to cell that no longer exists 40414 - fixed selected/active sheet after removing sheet from workbook 44523 - fixed workbook sheet selection and focus 45000 - Fixed NPE in ListLevel when numberText is null diff --git a/src/documentation/content/xdocs/hssf/quick-guide.xml b/src/documentation/content/xdocs/hssf/quick-guide.xml index 66da604892..28e2374338 100644 --- a/src/documentation/content/xdocs/hssf/quick-guide.xml +++ b/src/documentation/content/xdocs/hssf/quick-guide.xml @@ -1225,7 +1225,19 @@ Examples: // Do something with this corner cell } } - + +

+ 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 +

+ + if(hssfName.isDeleted()){ + //named range points to a deleted cell. + } else { + AreaReference ref = new AreaReference(hssfName.getReference()); + } +
Cell Comments diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index 0d87990cdb..28019e68fe 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,6 +34,7 @@ + 24207 - added HSSFName.isDeleted() to check if the name points to cell that no longer exists 40414 - fixed selected/active sheet after removing sheet from workbook 44523 - fixed workbook sheet selection and focus 45000 - Fixed NPE in ListLevel when numberText is null diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFName.java b/src/java/org/apache/poi/hssf/usermodel/HSSFName.java index 91b3fcd0f1..e24ca23ea8 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFName.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFName.java @@ -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); + } } diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestNamedRange.java b/src/testcases/org/apache/poi/hssf/usermodel/TestNamedRange.java index 98500960da..b3d08bf724 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestNamedRange.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestNamedRange.java @@ -533,4 +533,28 @@ public final class TestNamedRange extends TestCase { String contents = c.getStringCellValue(); 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){ + ; + } + + } + }