Simplify code and update unit test
Improve unit test coverage for the functions: `NewStyle`, `SetActiveSheet`, `SearchSheet` and `deleteAndAdjustDefinedNames` Simplify code and add comments for the function: `deleteAndAdjustDefinedNames`
This commit is contained in:
parent
aa359f1c74
commit
577a07f08c
|
@ -118,10 +118,7 @@ func TestGetColsError(t *testing.T) {
|
|||
_, err = f.GetCols("Sheet1")
|
||||
assert.EqualError(t, err, `strconv.Atoi: parsing "A": invalid syntax`)
|
||||
|
||||
f = NewFile()
|
||||
f.Sheet.Delete("xl/worksheets/sheet1.xml")
|
||||
f.Pkg.Store("xl/worksheets/sheet1.xml", []byte(`<worksheet><sheetData><row r="2"><c r="A" t="str"><v>B</v></c></row></sheetData></worksheet>`))
|
||||
f.checked = nil
|
||||
_, err = f.GetCols("Sheet1")
|
||||
assert.EqualError(t, err, `cannot convert cell "A" to coordinates: invalid cell name "A"`)
|
||||
|
||||
|
|
9
sheet.go
9
sheet.go
|
@ -586,15 +586,12 @@ func (f *File) DeleteSheet(name string) {
|
|||
f.SetActiveSheet(f.GetSheetIndex(activeSheetName))
|
||||
}
|
||||
|
||||
// deleteAndAdjustDefinedNames delete and adjust defined name in the workbook
|
||||
// by given worksheet ID.
|
||||
func deleteAndAdjustDefinedNames(wb *xlsxWorkbook, deleteLocalSheetID int) {
|
||||
if wb == nil {
|
||||
if wb == nil || wb.DefinedNames == nil {
|
||||
return
|
||||
}
|
||||
|
||||
if wb.DefinedNames == nil {
|
||||
return
|
||||
}
|
||||
|
||||
for idx := 0; idx < len(wb.DefinedNames.DefinedName); idx++ {
|
||||
dn := wb.DefinedNames.DefinedName[idx]
|
||||
if dn.LocalSheetID != nil {
|
||||
|
|
|
@ -196,6 +196,24 @@ func TestSearchSheet(t *testing.T) {
|
|||
assert.NoError(t, f.SetCellValue("Sheet1", "A1", true))
|
||||
_, err = f.SearchSheet("Sheet1", "")
|
||||
assert.NoError(t, err)
|
||||
|
||||
f = NewFile()
|
||||
f.Sheet.Delete("xl/worksheets/sheet1.xml")
|
||||
f.Pkg.Store("xl/worksheets/sheet1.xml", []byte(`<worksheet><sheetData><row r="A"><c r="2" t="str"><v>A</v></c></row></sheetData></worksheet>`))
|
||||
f.checked = nil
|
||||
result, err = f.SearchSheet("Sheet1", "A")
|
||||
assert.EqualError(t, err, "strconv.Atoi: parsing \"A\": invalid syntax")
|
||||
assert.Equal(t, []string(nil), result)
|
||||
|
||||
f.Pkg.Store("xl/worksheets/sheet1.xml", []byte(`<worksheet><sheetData><row r="2"><c r="A" t="str"><v>A</v></c></row></sheetData></worksheet>`))
|
||||
result, err = f.SearchSheet("Sheet1", "A")
|
||||
assert.EqualError(t, err, "cannot convert cell \"A\" to coordinates: invalid cell name \"A\"")
|
||||
assert.Equal(t, []string(nil), result)
|
||||
|
||||
f.Pkg.Store("xl/worksheets/sheet1.xml", []byte(`<worksheet><sheetData><row r="0"><c r="A1" t="str"><v>A</v></c></row></sheetData></worksheet>`))
|
||||
result, err = f.SearchSheet("Sheet1", "A")
|
||||
assert.EqualError(t, err, "invalid cell coordinates [1, 0]")
|
||||
assert.Equal(t, []string(nil), result)
|
||||
}
|
||||
|
||||
func TestSetPageLayout(t *testing.T) {
|
||||
|
@ -370,6 +388,14 @@ func TestSetActiveSheet(t *testing.T) {
|
|||
f = NewFile()
|
||||
f.SetActiveSheet(-1)
|
||||
assert.Equal(t, f.GetActiveSheetIndex(), 0)
|
||||
|
||||
f = NewFile()
|
||||
f.WorkBook.BookViews = nil
|
||||
idx := f.NewSheet("Sheet2")
|
||||
ws, ok = f.Sheet.Load("xl/worksheets/sheet2.xml")
|
||||
assert.True(t, ok)
|
||||
ws.(*xlsxWorksheet).SheetViews = &xlsxSheetViews{SheetView: []xlsxSheetView{}}
|
||||
f.SetActiveSheet(idx)
|
||||
}
|
||||
|
||||
func TestSetSheetName(t *testing.T) {
|
||||
|
@ -414,6 +440,11 @@ func TestDeleteSheet(t *testing.T) {
|
|||
assert.NoError(t, f.SaveAs(filepath.Join("test", "TestDeleteSheet2.xlsx")))
|
||||
}
|
||||
|
||||
func TestDeleteAndAdjustDefinedNames(t *testing.T) {
|
||||
deleteAndAdjustDefinedNames(nil, 0)
|
||||
deleteAndAdjustDefinedNames(&xlsxWorkbook{}, 0)
|
||||
}
|
||||
|
||||
func BenchmarkNewSheet(b *testing.B) {
|
||||
b.RunParallel(func(pb *testing.PB) {
|
||||
for pb.Next() {
|
||||
|
|
12
styles.go
12
styles.go
|
@ -2053,8 +2053,8 @@ func (f *File) NewStyle(style interface{}) (int, error) {
|
|||
|
||||
var getXfIDFuncs = map[string]func(int, xlsxXf, *Style) bool{
|
||||
"numFmt": func(numFmtID int, xf xlsxXf, style *Style) bool {
|
||||
if style.NumFmt == 0 && style.CustomNumFmt == nil && numFmtID == -1 {
|
||||
return xf.NumFmtID != nil || *xf.NumFmtID == 0
|
||||
if style.CustomNumFmt == nil && numFmtID == -1 {
|
||||
return xf.NumFmtID != nil && *xf.NumFmtID == 0
|
||||
}
|
||||
if style.NegRed || style.Lang != "" || style.DecimalPlaces != 2 {
|
||||
return false
|
||||
|
@ -2232,10 +2232,15 @@ func getNumFmtID(styleSheet *xlsxStyleSheet, style *Style) (numFmtID int) {
|
|||
if _, ok := builtInNumFmt[style.NumFmt]; ok {
|
||||
return style.NumFmt
|
||||
}
|
||||
if styleSheet.NumFmts == nil {
|
||||
for lang, numFmt := range langNumFmt {
|
||||
if _, ok := numFmt[style.NumFmt]; ok && lang == style.Lang {
|
||||
numFmtID = style.NumFmt
|
||||
return
|
||||
}
|
||||
}
|
||||
if fmtCode, ok := currencyNumFmt[style.NumFmt]; ok {
|
||||
numFmtID = style.NumFmt
|
||||
if styleSheet.NumFmts != nil {
|
||||
for _, numFmt := range styleSheet.NumFmts.NumFmt {
|
||||
if numFmt.FormatCode == fmtCode {
|
||||
numFmtID = numFmt.NumFmtID
|
||||
|
@ -2243,6 +2248,7 @@ func getNumFmtID(styleSheet *xlsxStyleSheet, style *Style) (numFmtID int) {
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
|
|
|
@ -252,6 +252,33 @@ func TestNewStyle(t *testing.T) {
|
|||
rows, err := f.GetRows("Sheet1")
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, [][]string{{"1.23E+00", "1.23E+00"}}, rows)
|
||||
|
||||
f = NewFile()
|
||||
// Test currency number format
|
||||
customNumFmt := "[$$-409]#,##0.00"
|
||||
style1, err := f.NewStyle(&Style{CustomNumFmt: &customNumFmt})
|
||||
assert.NoError(t, err)
|
||||
style2, err := f.NewStyle(&Style{NumFmt: 165})
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, style1, style2)
|
||||
|
||||
style3, err := f.NewStyle(&Style{NumFmt: 166})
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 2, style3)
|
||||
|
||||
f = NewFile()
|
||||
f.Styles.NumFmts = nil
|
||||
f.Styles.CellXfs.Xf = nil
|
||||
style4, err := f.NewStyle(&Style{NumFmt: 160, Lang: "unknown"})
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 1, style4)
|
||||
|
||||
f = NewFile()
|
||||
f.Styles.NumFmts = nil
|
||||
f.Styles.CellXfs.Xf = nil
|
||||
style5, err := f.NewStyle(&Style{NumFmt: 160, Lang: "zh-cn"})
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 1, style5)
|
||||
}
|
||||
|
||||
func TestGetDefaultFont(t *testing.T) {
|
||||
|
|
Loading…
Reference in New Issue