diff --git a/src/java/org/apache/poi/hssf/record/NameRecord.java b/src/java/org/apache/poi/hssf/record/NameRecord.java index 904133ce64..2f7f81899b 100644 --- a/src/java/org/apache/poi/hssf/record/NameRecord.java +++ b/src/java/org/apache/poi/hssf/record/NameRecord.java @@ -411,11 +411,12 @@ public final class NameRecord extends ContinuableRecord { * @return extern sheet index */ public int getExternSheetNumber(){ - if (field_13_name_definition.getEncodedSize() < 1) { + Ptg[] tokens = field_13_name_definition.getTokens(); + if (tokens.length == 0) { return 0; } - Ptg ptg = field_13_name_definition.getTokens()[0]; + Ptg ptg = tokens[0]; if (ptg.getClass() == Area3DPtg.class){ return ((Area3DPtg) ptg).getExternSheetIndex(); diff --git a/src/testcases/org/apache/poi/hssf/record/TestNameRecord.java b/src/testcases/org/apache/poi/hssf/record/TestNameRecord.java index 28bfb35099..231b6c92cb 100644 --- a/src/testcases/org/apache/poi/hssf/record/TestNameRecord.java +++ b/src/testcases/org/apache/poi/hssf/record/TestNameRecord.java @@ -17,15 +17,19 @@ package org.apache.poi.hssf.record; +import java.io.IOException; + +import junit.framework.TestCase; + import org.apache.poi.hssf.usermodel.HSSFName; import org.apache.poi.hssf.usermodel.HSSFWorkbook; +import org.apache.poi.ss.formula.ptg.Area3DPtg; import org.apache.poi.ss.formula.ptg.ArrayPtg; +import org.apache.poi.ss.formula.ptg.NamePtg; import org.apache.poi.ss.formula.ptg.Ptg; +import org.apache.poi.ss.formula.ptg.Ref3DPtg; import org.apache.poi.util.HexRead; -import junit.framework.AssertionFailedError; -import junit.framework.TestCase; - /** * Tests the NameRecord serializes/deserializes correctly * @@ -60,7 +64,8 @@ public final class TestNameRecord extends TestCase { byte[] data2 = nr.serialize(); TestcaseRecordInputStream.confirmRecordEncoding(NameRecord.sid, data, data2); } - public void testFormulaRelAbs_bug46174() { + + public void testFormulaRelAbs_bug46174() throws IOException { // perhaps this testcase belongs on TestHSSFName HSSFWorkbook wb = new HSSFWorkbook(); HSSFName name = wb.createName(); @@ -68,11 +73,13 @@ public final class TestNameRecord extends TestCase { name.setNameName("test"); name.setRefersToFormula("Sheet1!$B$3"); if (name.getRefersToFormula().equals("Sheet1!B3")) { - throw new AssertionFailedError("Identified bug 46174"); + fail("Identified bug 46174"); } assertEquals("Sheet1!$B$3", name.getRefersToFormula()); + wb.close(); } - public void testFormulaGeneral() { + + public void testFormulaGeneral() throws IOException { // perhaps this testcase belongs on TestHSSFName HSSFWorkbook wb = new HSSFWorkbook(); HSSFName name = wb.createName(); @@ -82,6 +89,8 @@ public final class TestNameRecord extends TestCase { assertEquals("Sheet1!A1+Sheet1!A2", name.getRefersToFormula()); name.setRefersToFormula("5*6"); assertEquals("5*6", name.getRefersToFormula()); + + wb.close(); } /** @@ -687,4 +696,22 @@ public final class TestNameRecord extends TestCase { assertEquals("1.T20.001", vals[0][0]); assertEquals("1.T20.010", vals[vals.length - 1][0]); } + + public void testBug57923() { + NameRecord record = new NameRecord(); + assertEquals(0, record.getExternSheetNumber()); + + record.setNameDefinition(new Ptg[] {}); + assertEquals(0, record.getExternSheetNumber()); + + record.setNameDefinition(new Ptg[] {new NamePtg(1)}); + assertEquals(0, record.getExternSheetNumber()); + + record.setNameDefinition(new Ptg[] {new Area3DPtg("area", 1)}); + assertEquals(1, record.getExternSheetNumber()); + + record.setNameDefinition(new Ptg[] {new Ref3DPtg("A1", 1)}); + assertEquals(1, record.getExternSheetNumber()); + } + }