]> source.dussan.org Git - poi.git/commitdiff
Additional fix for 45720 - bug in HSSFWorkbook.findExistingBuiltinNameRecordIdx
authorJosh Micich <josh@apache.org>
Mon, 8 Sep 2008 19:49:03 +0000 (19:49 +0000)
committerJosh Micich <josh@apache.org>
Mon, 8 Sep 2008 19:49:03 +0000 (19:49 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@693221 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/hssf/model/Workbook.java
src/java/org/apache/poi/hssf/record/NameRecord.java
src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java
src/testcases/org/apache/poi/hssf/data/testRRaC.xls [new file with mode: 0644]
src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java

index 7db37b253af6cb0069e18fd4eac6e36e8bb38926..fad9e4d0ba4186f2566314488cf679f89d360788 100644 (file)
@@ -2466,6 +2466,10 @@ public final class Workbook implements Model {
         int aggLoc = sheet.aggregateDrawingRecords(drawingManager, false);
         if(aggLoc != -1) {
             EscherAggregate agg = (EscherAggregate) sheet.findFirstRecordBySid(EscherAggregate.sid);
+            EscherContainerRecord escherContainer = agg.getEscherContainer();
+            if (escherContainer == null) {
+                return;
+            }
 
             EscherDggRecord dgg = drawingManager.getDgg();
 
@@ -2475,7 +2479,7 @@ public final class Workbook implements Model {
             dgg.setDrawingsSaved(dgg.getDrawingsSaved() + 1);
 
             EscherDgRecord dg = null;
-            for(Iterator it = agg.getEscherContainer().getChildRecords().iterator(); it.hasNext();) {
+            for(Iterator it = escherContainer.getChildRecords().iterator(); it.hasNext();) {
                 Object er = it.next();
                 if(er instanceof EscherDgRecord) {
                     dg = (EscherDgRecord)er;
index b12a3c6ad764b8078c2023ac21d4161111647211..18e2126d54c9ab0afc4ce9d53e25180e10a218cf 100644 (file)
@@ -82,8 +82,9 @@ public final class NameRecord extends Record {
 
        private short             field_1_option_flag;
        private byte              field_2_keyboard_shortcut;
-       private short             field_5_index_to_sheet;     // unused: see field_6
-       /** the one based sheet number.  Zero if this is a global name */
+       /** One-based extern index of sheet (resolved via LinkTable). Zero if this is a global name  */
+       private short             field_5_externSheetIndex_plus1;
+       /** the one based sheet number.  */
        private int               field_6_sheetNumber;
        private boolean           field_11_nameIsMultibyte;
        private byte              field_12_built_in_code;
@@ -144,7 +145,7 @@ public final class NameRecord extends Record {
 
        /**
         * For named ranges, and built-in names
-        * @return the 1-based sheet number.  Zero if this is a global name
+        * @return the 1-based sheet number. 
         */
        public int getSheetNumber()
        {
@@ -384,7 +385,7 @@ public final class NameRecord extends Record {
                LittleEndian.putByte(data, 7 + offset, getNameTextLength());
                // Note -
                LittleEndian.putUShort(data, 8 + offset, Ptg.getEncodedSizeWithoutArrayData(field_13_name_definition));
-               LittleEndian.putUShort(data, 10 + offset, field_5_index_to_sheet);
+               LittleEndian.putUShort(data, 10 + offset, field_5_externSheetIndex_plus1);
                LittleEndian.putUShort(data, 12 + offset, field_6_sheetNumber);
                LittleEndian.putByte(data, 14 + offset, field_7_length_custom_menu);
                LittleEndian.putByte(data, 15 + offset, field_8_length_description_text);
@@ -557,7 +558,7 @@ public final class NameRecord extends Record {
                field_2_keyboard_shortcut           = in.readByte();
                int field_3_length_name_text        = in.readByte();
                int field_4_length_name_definition  = in.readShort();
-               field_5_index_to_sheet              = in.readShort();
+               field_5_externSheetIndex_plus1      = in.readShort();
                field_6_sheetNumber                 = in.readUShort();
                int field_7_length_custom_menu      = in.readUByte();
                int field_8_length_description_text = in.readUByte();
@@ -649,8 +650,8 @@ public final class NameRecord extends Record {
                sb.append("    .option flags           = ").append(HexDump.shortToHex(field_1_option_flag)).append("\n");
                sb.append("    .keyboard shortcut      = ").append(HexDump.byteToHex(field_2_keyboard_shortcut)).append("\n");
                sb.append("    .length of the name     = ").append(getNameTextLength()).append("\n");
-               sb.append("    .unused                 = ").append( field_5_index_to_sheet ).append("\n");
-               sb.append("    .index to sheet (1-based, 0=Global) = ").append( field_6_sheetNumber ).append("\n");
+               sb.append("    .extSheetIx(1-based, 0=Global)= ").append( field_5_externSheetIndex_plus1 ).append("\n");
+               sb.append("    .sheetTabIx             = ").append(field_6_sheetNumber ).append("\n");
                sb.append("    .Menu text length       = ").append(field_14_custom_menu_text.length()).append("\n");
                sb.append("    .Description text length= ").append(field_15_description_text.length()).append("\n");
                sb.append("    .Help topic text length = ").append(field_16_help_topic_text.length()).append("\n");
index a227c43d11b8e1b832a1457e6ad1cebcc20c2f1b..3dd31d7a045dd87a2ceca29a61fac879fd5cda86 100644 (file)
@@ -1042,13 +1042,7 @@ public class HSSFWorkbook extends POIDocument
             if (!r.isBuiltInName() || r.getBuiltInName() != builtinCode) {
                 continue;
             }
-            if(r.getSheetNumber() == 0) {
-                //ignore "GLOBAL" name records
-                continue;
-            }
-            int externIndex = r.getSheetNumber() -1;
-            int nameRecordSheetIndex = workbook.getSheetIndexFromExternSheetIndex(externIndex);
-            if (nameRecordSheetIndex == sheetIndex) {
+            if (r.getSheetNumber() -1 == sheetIndex) {
                 return defNameIndex;
             }
         }
diff --git a/src/testcases/org/apache/poi/hssf/data/testRRaC.xls b/src/testcases/org/apache/poi/hssf/data/testRRaC.xls
new file mode 100644 (file)
index 0000000..25939e8
Binary files /dev/null and b/src/testcases/org/apache/poi/hssf/data/testRRaC.xls differ
index 1717aeff655565e81133eb4158273cba25033723..6fb08f4ee30857a7000c72005e08c321f615a202 100644 (file)
@@ -527,4 +527,33 @@ public final class TestHSSFWorkbook extends TestCase {
             }
         }
     }
+    
+    /**
+     * Test to make sure that NameRecord.getSheetNumber() is interpreted as a
+     * 1-based sheet tab index (not a 1-based extern sheet index)
+     */
+    public void testFindBuiltInNameRecord() {
+        // testRRaC has multiple (3) built-in name records
+        // The second print titles name record has getSheetNumber()==4
+        HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("testRRaC.xls");
+        NameRecord nr;
+        assertEquals(3, wb.getWorkbook().getNumNames());
+        nr = wb.getWorkbook().getNameRecord(2);
+        // TODO - render full row and full column refs properly
+        assertEquals("Sheet2!$A$1:$IV$1", nr.getAreaReference(wb)); // 1:1
+        
+        try {
+            wb.setRepeatingRowsAndColumns(3, 4, 5, 8, 11);
+        } catch (RuntimeException e) {
+            if (e.getMessage().equals("Builtin (7) already exists for sheet (4)")) {
+                // there was a problem in the code which locates the existing print titles name record 
+                throw new RuntimeException("Identified bug 45720b");
+            }
+            throw e;
+        }
+        wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
+        assertEquals(3, wb.getWorkbook().getNumNames());
+        nr = wb.getWorkbook().getNameRecord(2);
+        assertEquals("Sheet2!E:F,Sheet2!$A$9:$IV$12", nr.getAreaReference(wb)); // E:F,9:12
+    }
 }