Slight tweak for named ranges pointing at deleted sheets, related to bug #30978

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@668153 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2008-06-16 13:20:42 +00:00
parent 7cf39af9a2
commit acfbb1648c
5 changed files with 46 additions and 3 deletions

View File

@ -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
}
/**

View File

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

View File

@ -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;

Binary file not shown.

View File

@ -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()
*/