Skip to content

Commit 0d5d1c5

Browse files
committed
This closes qax-os#2015, fix a v2.9.0 regression bug introduced by commit 7715c14
- Fix corrupted workbook generated by open the workbook generated by stream writer - Update unit tests
1 parent af190c7 commit 0d5d1c5

File tree

5 files changed

+14
-5
lines changed

5 files changed

+14
-5
lines changed

lib.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -652,11 +652,16 @@ func getRootElement(d *xml.Decoder) []xml.Attr {
652652
case xml.StartElement:
653653
tokenIdx++
654654
if tokenIdx == 1 {
655+
var ns bool
655656
for i := 0; i < len(startElement.Attr); i++ {
656-
if startElement.Attr[i].Value == NameSpaceSpreadSheet.Value {
657-
startElement.Attr[i] = NameSpaceSpreadSheet
657+
if startElement.Attr[i].Value == NameSpaceSpreadSheet.Value &&
658+
startElement.Attr[i].Name == NameSpaceSpreadSheet.Name {
659+
ns = true
658660
}
659661
}
662+
if !ns {
663+
startElement.Attr = append(startElement.Attr, NameSpaceSpreadSheet)
664+
}
660665
return startElement.Attr
661666
}
662667
}

lib_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,10 @@ func TestBytesReplace(t *testing.T) {
289289

290290
func TestGetRootElement(t *testing.T) {
291291
assert.Len(t, getRootElement(xml.NewDecoder(strings.NewReader(""))), 0)
292+
// Test get workbook root element which all workbook XML namespace has prefix
293+
f := NewFile()
294+
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>`)))
295+
assert.Len(t, getRootElement(d), 3)
292296
}
293297

294298
func TestSetIgnorableNameSpace(t *testing.T) {

pivotTable_test.go

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

522522
// Test delete workbook pivot table cache with unsupported workbook relationships charset

sheet_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ func TestMoveSheet(t *testing.T) {
580580

581581
// Test move sheet with unsupported workbook charset
582582
f.WorkBook = nil
583-
f.Pkg.Store("xl/workbook.xml", MacintoshCyrillicCharset)
583+
f.Pkg.Store(defaultXMLPathWorkbook, MacintoshCyrillicCharset)
584584
assert.EqualError(t, f.MoveSheet("Sheet2", "Sheet1"), "XML syntax error on line 1: invalid UTF-8")
585585
}
586586

slicer_test.go

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

0 commit comments

Comments
 (0)