diff --git a/src/java/org/apache/poi/hssf/model/Workbook.java b/src/java/org/apache/poi/hssf/model/Workbook.java index d87fc2c632..5b38935f4c 100644 --- a/src/java/org/apache/poi/hssf/model/Workbook.java +++ b/src/java/org/apache/poi/hssf/model/Workbook.java @@ -603,6 +603,9 @@ public class Workbook implements Model boundsheets.remove(sheetnum); fixTabIdRecord(); } + + // If we decide that we need to fix up + // NameRecords, do it here } /** diff --git a/src/java/org/apache/poi/hssf/record/formula/Area3DPtg.java b/src/java/org/apache/poi/hssf/record/formula/Area3DPtg.java index 60911edc72..e8f72e9937 100644 --- a/src/java/org/apache/poi/hssf/record/formula/Area3DPtg.java +++ b/src/java/org/apache/poi/hssf/record/formula/Area3DPtg.java @@ -269,8 +269,15 @@ public class Area3DPtg extends OperandPtg implements AreaI { StringBuffer retval = new StringBuffer(); String sheetName = Ref3DPtg.getSheetName(book, field_1_index_extern_sheet); if(sheetName != null) { - SheetNameFormatter.appendFormat(retval, sheetName); - retval.append( '!' ); + if(sheetName.length() == 0) { + // What excel does if sheet has been deleted + sheetName = "#REF"; + retval.append(sheetName); + } else { + // Normal + SheetNameFormatter.appendFormat(retval, sheetName); + retval.append( '!' ); + } } // Now the normal area bit diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFName.java b/src/java/org/apache/poi/hssf/usermodel/HSSFName.java index e24ca23ea8..240be13f58 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFName.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFName.java @@ -18,7 +18,6 @@ package org.apache.poi.hssf.usermodel; import org.apache.poi.hssf.model.Workbook; -import org.apache.poi.hssf.record.BoundSheetRecord; import org.apache.poi.hssf.record.NameRecord; import org.apache.poi.hssf.util.RangeAddress; diff --git a/src/testcases/org/apache/poi/hssf/data/30978-deleted.xls b/src/testcases/org/apache/poi/hssf/data/30978-deleted.xls new file mode 100755 index 0000000000..1daca5bff7 Binary files /dev/null and b/src/testcases/org/apache/poi/hssf/data/30978-deleted.xls differ diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java index b9873fa4b6..2fca481481 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java @@ -30,6 +30,7 @@ import org.apache.poi.hssf.model.Sheet; import org.apache.poi.hssf.record.NameRecord; import org.apache.poi.hssf.record.Record; import org.apache.poi.hssf.record.RecordInputStream; +import org.apache.poi.hssf.record.formula.Area3DPtg; import org.apache.poi.util.TempFile; /** * @@ -402,6 +403,39 @@ public final class TestHSSFWorkbook extends TestCase { assertTrue(e.getMessage().startsWith("Actual serialized sheet size")); } } + + /** + * Checks that us and HSSFName play nicely with named ranges + * that point to deleted sheets + */ + public void testNamesToDeleteSheets() throws Exception { + HSSFWorkbook b = openSample("30978-deleted.xls"); + assertEquals(1, b.getNumberOfNames()); + + // Sheet 2 deleted + assertEquals("Sheet1", b.getSheetName(0)); + assertEquals("Sheet3", b.getSheetName(1)); + + // First at low level + NameRecord nr = b.getWorkbook().getNameRecord(0); + assertEquals("On2", nr.getNameText()); + assertEquals(1, nr.getExternSheetNumber()); + assertEquals(1, nr.getNameDefinition().size()); + + Area3DPtg ptg = (Area3DPtg)nr.getNameDefinition().get(0); + assertEquals(1, ptg.getExternSheetIndex()); + assertEquals(0, ptg.getFirstColumn()); + assertEquals(0, ptg.getFirstRow()); + assertEquals(0, ptg.getLastColumn()); + assertEquals(2, ptg.getLastRow()); + + // Now at high level + HSSFName n = b.getNameAt(0); + assertEquals("On2", n.getNameName()); + assertEquals("", n.getSheetName()); + assertEquals("#REF$A$1:$A$3", n.getReference()); + } + /** * result returned by getRecordSize() differs from result returned by serialize() */