This closes #2015, fix a v2.9.0 regression bug introduced by commit 7715c1462a

- Fix corrupted workbook generated by open the workbook generated by stream writer
- Update unit tests
This commit is contained in:
xuri 2024-10-25 08:52:59 +08:00
parent af190c7fdc
commit 0d5d1c53b2
No known key found for this signature in database
GPG Key ID: BA5E5BB1C948EDF7
5 changed files with 14 additions and 5 deletions

9
lib.go
View File

@ -652,11 +652,16 @@ func getRootElement(d *xml.Decoder) []xml.Attr {
case xml.StartElement: case xml.StartElement:
tokenIdx++ tokenIdx++
if tokenIdx == 1 { if tokenIdx == 1 {
var ns bool
for i := 0; i < len(startElement.Attr); i++ { for i := 0; i < len(startElement.Attr); i++ {
if startElement.Attr[i].Value == NameSpaceSpreadSheet.Value { if startElement.Attr[i].Value == NameSpaceSpreadSheet.Value &&
startElement.Attr[i] = NameSpaceSpreadSheet startElement.Attr[i].Name == NameSpaceSpreadSheet.Name {
ns = true
} }
} }
if !ns {
startElement.Attr = append(startElement.Attr, NameSpaceSpreadSheet)
}
return startElement.Attr return startElement.Attr
} }
} }

View File

@ -289,6 +289,10 @@ func TestBytesReplace(t *testing.T) {
func TestGetRootElement(t *testing.T) { func TestGetRootElement(t *testing.T) {
assert.Len(t, getRootElement(xml.NewDecoder(strings.NewReader(""))), 0) assert.Len(t, getRootElement(xml.NewDecoder(strings.NewReader(""))), 0)
// Test get workbook root element which all workbook XML namespace has prefix
f := NewFile()
d := f.xmlNewDecoder(bytes.NewReader([]byte(`<x:workbook xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main"></x:workbook>`)))
assert.Len(t, getRootElement(d), 3)
} }
func TestSetIgnorableNameSpace(t *testing.T) { func TestSetIgnorableNameSpace(t *testing.T) {

View File

@ -516,7 +516,7 @@ func TestDeleteWorkbookPivotCache(t *testing.T) {
f := NewFile() f := NewFile()
// Test delete workbook pivot table cache with unsupported workbook charset // Test delete workbook pivot table cache with unsupported workbook charset
f.WorkBook = nil f.WorkBook = nil
f.Pkg.Store("xl/workbook.xml", MacintoshCyrillicCharset) f.Pkg.Store(defaultXMLPathWorkbook, MacintoshCyrillicCharset)
assert.EqualError(t, f.deleteWorkbookPivotCache(PivotTableOptions{pivotCacheXML: "pivotCache/pivotCacheDefinition1.xml"}), "XML syntax error on line 1: invalid UTF-8") assert.EqualError(t, f.deleteWorkbookPivotCache(PivotTableOptions{pivotCacheXML: "pivotCache/pivotCacheDefinition1.xml"}), "XML syntax error on line 1: invalid UTF-8")
// Test delete workbook pivot table cache with unsupported workbook relationships charset // Test delete workbook pivot table cache with unsupported workbook relationships charset

View File

@ -580,7 +580,7 @@ func TestMoveSheet(t *testing.T) {
// Test move sheet with unsupported workbook charset // Test move sheet with unsupported workbook charset
f.WorkBook = nil f.WorkBook = nil
f.Pkg.Store("xl/workbook.xml", MacintoshCyrillicCharset) f.Pkg.Store(defaultXMLPathWorkbook, MacintoshCyrillicCharset)
assert.EqualError(t, f.MoveSheet("Sheet2", "Sheet1"), "XML syntax error on line 1: invalid UTF-8") assert.EqualError(t, f.MoveSheet("Sheet2", "Sheet1"), "XML syntax error on line 1: invalid UTF-8")
} }

View File

@ -597,7 +597,7 @@ func TestAddWorkbookSlicerCache(t *testing.T) {
// Test add a workbook slicer cache with unsupported charset workbook // Test add a workbook slicer cache with unsupported charset workbook
f := NewFile() f := NewFile()
f.WorkBook = nil f.WorkBook = nil
f.Pkg.Store("xl/workbook.xml", MacintoshCyrillicCharset) f.Pkg.Store(defaultXMLPathWorkbook, MacintoshCyrillicCharset)
assert.EqualError(t, f.addWorkbookSlicerCache(1, ExtURISlicerCachesX15), "XML syntax error on line 1: invalid UTF-8") assert.EqualError(t, f.addWorkbookSlicerCache(1, ExtURISlicerCachesX15), "XML syntax error on line 1: invalid UTF-8")
assert.NoError(t, f.Close()) assert.NoError(t, f.Close())
} }