Skip to content

Commit bd5dd17

Browse files
committedNov 11, 2022
This is a breaking change, remove partial internal error log print, throw XML deserialize error
- Add error return value for the `GetComments`, `GetDefaultFont` and `SetDefaultFont` functions - Update unit tests
1 parent 58b5dae commit bd5dd17

30 files changed

+655
-297
lines changed
 

‎adjust_test.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010

1111
func TestAdjustMergeCells(t *testing.T) {
1212
f := NewFile()
13-
// testing adjustAutoFilter with illegal cell reference.
13+
// Test adjustAutoFilter with illegal cell reference.
1414
assert.EqualError(t, f.adjustMergeCells(&xlsxWorksheet{
1515
MergeCells: &xlsxMergeCells{
1616
Cells: []*xlsxMergeCell{
@@ -57,7 +57,7 @@ func TestAdjustMergeCells(t *testing.T) {
5757
},
5858
}, columns, 1, -1))
5959

60-
// testing adjustMergeCells
60+
// Test adjustMergeCells.
6161
var cases []struct {
6262
label string
6363
ws *xlsxWorksheet
@@ -68,7 +68,7 @@ func TestAdjustMergeCells(t *testing.T) {
6868
expectRect []int
6969
}
7070

71-
// testing insert
71+
// Test insert.
7272
cases = []struct {
7373
label string
7474
ws *xlsxWorksheet
@@ -139,7 +139,7 @@ func TestAdjustMergeCells(t *testing.T) {
139139
assert.Equal(t, c.expectRect, c.ws.MergeCells.Cells[0].rect, c.label)
140140
}
141141

142-
// testing delete
142+
// Test delete,
143143
cases = []struct {
144144
label string
145145
ws *xlsxWorksheet
@@ -227,7 +227,7 @@ func TestAdjustMergeCells(t *testing.T) {
227227
assert.Equal(t, c.expect, c.ws.MergeCells.Cells[0].Ref, c.label)
228228
}
229229

230-
// testing delete one row/column
230+
// Test delete one row or column
231231
cases = []struct {
232232
label string
233233
ws *xlsxWorksheet
@@ -324,13 +324,13 @@ func TestAdjustTable(t *testing.T) {
324324

325325
f = NewFile()
326326
assert.NoError(t, f.AddTable(sheetName, "A1", "D5", ""))
327-
// Test adjust table with non-table part
327+
// Test adjust table with non-table part.
328328
f.Pkg.Delete("xl/tables/table1.xml")
329329
assert.NoError(t, f.RemoveRow(sheetName, 1))
330-
// Test adjust table with unsupported charset
330+
// Test adjust table with unsupported charset.
331331
f.Pkg.Store("xl/tables/table1.xml", MacintoshCyrillicCharset)
332332
assert.NoError(t, f.RemoveRow(sheetName, 1))
333-
// Test adjust table with invalid table range reference
333+
// Test adjust table with invalid table range reference.
334334
f.Pkg.Store("xl/tables/table1.xml", []byte(`<table ref="-" />`))
335335
assert.NoError(t, f.RemoveRow(sheetName, 1))
336336
}

‎calcchain.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,19 @@ import (
1515
"bytes"
1616
"encoding/xml"
1717
"io"
18-
"log"
1918
)
2019

2120
// calcChainReader provides a function to get the pointer to the structure
2221
// after deserialization of xl/calcChain.xml.
23-
func (f *File) calcChainReader() *xlsxCalcChain {
24-
var err error
25-
22+
func (f *File) calcChainReader() (*xlsxCalcChain, error) {
2623
if f.CalcChain == nil {
2724
f.CalcChain = new(xlsxCalcChain)
28-
if err = f.xmlNewDecoder(bytes.NewReader(namespaceStrictToTransitional(f.readXML(defaultXMLPathCalcChain)))).
25+
if err := f.xmlNewDecoder(bytes.NewReader(namespaceStrictToTransitional(f.readXML(defaultXMLPathCalcChain)))).
2926
Decode(f.CalcChain); err != nil && err != io.EOF {
30-
log.Printf("xml decode error: %s", err)
27+
return f.CalcChain, err
3128
}
3229
}
33-
34-
return f.CalcChain
30+
return f.CalcChain, nil
3531
}
3632

3733
// calcChainWriter provides a function to save xl/calcChain.xml after
@@ -45,8 +41,11 @@ func (f *File) calcChainWriter() {
4541

4642
// deleteCalcChain provides a function to remove cell reference on the
4743
// calculation chain.
48-
func (f *File) deleteCalcChain(index int, cell string) {
49-
calc := f.calcChainReader()
44+
func (f *File) deleteCalcChain(index int, cell string) error {
45+
calc, err := f.calcChainReader()
46+
if err != nil {
47+
return err
48+
}
5049
if calc != nil {
5150
calc.C = xlsxCalcChainCollection(calc.C).Filter(func(c xlsxCalcChainC) bool {
5251
return !((c.I == index && c.R == cell) || (c.I == index && cell == "") || (c.I == 0 && c.R == cell))
@@ -64,6 +63,7 @@ func (f *File) deleteCalcChain(index int, cell string) {
6463
}
6564
}
6665
}
66+
return err
6767
}
6868

6969
type xlsxCalcChainCollection []xlsxCalcChainC

‎calcchain_test.go

+23-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
package excelize
22

3-
import "testing"
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
48

59
func TestCalcChainReader(t *testing.T) {
610
f := NewFile()
11+
// Test read calculation chain with unsupported charset.
712
f.CalcChain = nil
813
f.Pkg.Store(defaultXMLPathCalcChain, MacintoshCyrillicCharset)
9-
f.calcChainReader()
14+
_, err := f.calcChainReader()
15+
assert.EqualError(t, err, "XML syntax error on line 1: invalid UTF-8")
1016
}
1117

1218
func TestDeleteCalcChain(t *testing.T) {
@@ -15,5 +21,19 @@ func TestDeleteCalcChain(t *testing.T) {
1521
f.ContentTypes.Overrides = append(f.ContentTypes.Overrides, xlsxOverride{
1622
PartName: "/xl/calcChain.xml",
1723
})
18-
f.deleteCalcChain(1, "A1")
24+
assert.NoError(t, f.deleteCalcChain(1, "A1"))
25+
26+
f.CalcChain = nil
27+
f.Pkg.Store(defaultXMLPathCalcChain, MacintoshCyrillicCharset)
28+
assert.EqualError(t, f.deleteCalcChain(1, "A1"), "XML syntax error on line 1: invalid UTF-8")
29+
30+
f.CalcChain = nil
31+
f.Pkg.Store(defaultXMLPathCalcChain, MacintoshCyrillicCharset)
32+
assert.EqualError(t, f.SetCellFormula("Sheet1", "A1", ""), "XML syntax error on line 1: invalid UTF-8")
33+
34+
formulaType, ref := STCellFormulaTypeShared, "C1:C5"
35+
assert.NoError(t, f.SetCellFormula("Sheet1", "C1", "=A1+B1", FormulaOpts{Ref: &ref, Type: &formulaType}))
36+
f.CalcChain = nil
37+
f.Pkg.Store(defaultXMLPathCalcChain, MacintoshCyrillicCharset)
38+
assert.EqualError(t, f.SetCellValue("Sheet1", "C1", true), "XML syntax error on line 1: invalid UTF-8")
1939
}

0 commit comments

Comments
 (0)
Please sign in to comment.