Skip to content

Commit

Permalink
This is a breaking change, remove partial internal error log print, t…
Browse files Browse the repository at this point in the history
…hrow XML deserialize error

- Add error return value for the `GetComments`, `GetDefaultFont` and `SetDefaultFont` functions
- Update unit tests
  • Loading branch information
xuri committed Nov 11, 2022
1 parent 58b5dae commit bd5dd17
Show file tree
Hide file tree
Showing 30 changed files with 655 additions and 297 deletions.
16 changes: 8 additions & 8 deletions adjust_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

func TestAdjustMergeCells(t *testing.T) {
f := NewFile()
// testing adjustAutoFilter with illegal cell reference.
// Test adjustAutoFilter with illegal cell reference.
assert.EqualError(t, f.adjustMergeCells(&xlsxWorksheet{
MergeCells: &xlsxMergeCells{
Cells: []*xlsxMergeCell{
Expand Down Expand Up @@ -57,7 +57,7 @@ func TestAdjustMergeCells(t *testing.T) {
},
}, columns, 1, -1))

// testing adjustMergeCells
// Test adjustMergeCells.
var cases []struct {
label string
ws *xlsxWorksheet
Expand All @@ -68,7 +68,7 @@ func TestAdjustMergeCells(t *testing.T) {
expectRect []int
}

// testing insert
// Test insert.
cases = []struct {
label string
ws *xlsxWorksheet
Expand Down Expand Up @@ -139,7 +139,7 @@ func TestAdjustMergeCells(t *testing.T) {
assert.Equal(t, c.expectRect, c.ws.MergeCells.Cells[0].rect, c.label)
}

// testing delete
// Test delete,
cases = []struct {
label string
ws *xlsxWorksheet
Expand Down Expand Up @@ -227,7 +227,7 @@ func TestAdjustMergeCells(t *testing.T) {
assert.Equal(t, c.expect, c.ws.MergeCells.Cells[0].Ref, c.label)
}

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

f = NewFile()
assert.NoError(t, f.AddTable(sheetName, "A1", "D5", ""))
// Test adjust table with non-table part
// Test adjust table with non-table part.
f.Pkg.Delete("xl/tables/table1.xml")
assert.NoError(t, f.RemoveRow(sheetName, 1))
// Test adjust table with unsupported charset
// Test adjust table with unsupported charset.
f.Pkg.Store("xl/tables/table1.xml", MacintoshCyrillicCharset)
assert.NoError(t, f.RemoveRow(sheetName, 1))
// Test adjust table with invalid table range reference
// Test adjust table with invalid table range reference.
f.Pkg.Store("xl/tables/table1.xml", []byte(`<table ref="-" />`))
assert.NoError(t, f.RemoveRow(sheetName, 1))
}
Expand Down
20 changes: 10 additions & 10 deletions calcchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,19 @@ import (
"bytes"
"encoding/xml"
"io"
"log"
)

// calcChainReader provides a function to get the pointer to the structure
// after deserialization of xl/calcChain.xml.
func (f *File) calcChainReader() *xlsxCalcChain {
var err error

func (f *File) calcChainReader() (*xlsxCalcChain, error) {
if f.CalcChain == nil {
f.CalcChain = new(xlsxCalcChain)
if err = f.xmlNewDecoder(bytes.NewReader(namespaceStrictToTransitional(f.readXML(defaultXMLPathCalcChain)))).
if err := f.xmlNewDecoder(bytes.NewReader(namespaceStrictToTransitional(f.readXML(defaultXMLPathCalcChain)))).
Decode(f.CalcChain); err != nil && err != io.EOF {
log.Printf("xml decode error: %s", err)
return f.CalcChain, err
}
}

return f.CalcChain
return f.CalcChain, nil
}

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

// deleteCalcChain provides a function to remove cell reference on the
// calculation chain.
func (f *File) deleteCalcChain(index int, cell string) {
calc := f.calcChainReader()
func (f *File) deleteCalcChain(index int, cell string) error {
calc, err := f.calcChainReader()
if err != nil {
return err
}
if calc != nil {
calc.C = xlsxCalcChainCollection(calc.C).Filter(func(c xlsxCalcChainC) bool {
return !((c.I == index && c.R == cell) || (c.I == index && cell == "") || (c.I == 0 && c.R == cell))
Expand All @@ -64,6 +63,7 @@ func (f *File) deleteCalcChain(index int, cell string) {
}
}
}
return err
}

type xlsxCalcChainCollection []xlsxCalcChainC
Expand Down
26 changes: 23 additions & 3 deletions calcchain_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package excelize

import "testing"
import (
"testing"

"github.com/stretchr/testify/assert"
)

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

func TestDeleteCalcChain(t *testing.T) {
Expand All @@ -15,5 +21,19 @@ func TestDeleteCalcChain(t *testing.T) {
f.ContentTypes.Overrides = append(f.ContentTypes.Overrides, xlsxOverride{
PartName: "/xl/calcChain.xml",
})
f.deleteCalcChain(1, "A1")
assert.NoError(t, f.deleteCalcChain(1, "A1"))

f.CalcChain = nil
f.Pkg.Store(defaultXMLPathCalcChain, MacintoshCyrillicCharset)
assert.EqualError(t, f.deleteCalcChain(1, "A1"), "XML syntax error on line 1: invalid UTF-8")

f.CalcChain = nil
f.Pkg.Store(defaultXMLPathCalcChain, MacintoshCyrillicCharset)
assert.EqualError(t, f.SetCellFormula("Sheet1", "A1", ""), "XML syntax error on line 1: invalid UTF-8")

formulaType, ref := STCellFormulaTypeShared, "C1:C5"
assert.NoError(t, f.SetCellFormula("Sheet1", "C1", "=A1+B1", FormulaOpts{Ref: &ref, Type: &formulaType}))
f.CalcChain = nil
f.Pkg.Store(defaultXMLPathCalcChain, MacintoshCyrillicCharset)
assert.EqualError(t, f.SetCellValue("Sheet1", "C1", true), "XML syntax error on line 1: invalid UTF-8")
}
Loading

0 comments on commit bd5dd17

Please sign in to comment.