diff options
| -rw-r--r-- | col_test.go | 3 | ||||
| -rw-r--r-- | sheet.go | 9 | ||||
| -rw-r--r-- | sheet_test.go | 31 | ||||
| -rw-r--r-- | styles.go | 22 | ||||
| -rw-r--r-- | styles_test.go | 29 | 
5 files changed, 76 insertions, 18 deletions
| diff --git a/col_test.go b/col_test.go index 80fc676..da46f78 100644 --- a/col_test.go +++ b/col_test.go @@ -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"`) @@ -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 { diff --git a/sheet_test.go b/sheet_test.go index 93a4ab6..d33ba99 100644 --- a/sheet_test.go +++ b/sheet_test.go @@ -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() { @@ -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,14 +2232,20 @@ func getNumFmtID(styleSheet *xlsxStyleSheet, style *Style) (numFmtID int) {  	if _, ok := builtInNumFmt[style.NumFmt]; ok {  		return style.NumFmt  	} -	if styleSheet.NumFmts == nil { -		return +	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 { -		for _, numFmt := range styleSheet.NumFmts.NumFmt { -			if numFmt.FormatCode == fmtCode { -				numFmtID = numFmt.NumFmtID -				return +		numFmtID = style.NumFmt +		if styleSheet.NumFmts != nil { +			for _, numFmt := range styleSheet.NumFmts.NumFmt { +				if numFmt.FormatCode == fmtCode { +					numFmtID = numFmt.NumFmtID +					return +				}  			}  		}  	} diff --git a/styles_test.go b/styles_test.go index 69266ea..720340f 100644 --- a/styles_test.go +++ b/styles_test.go @@ -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) { @@ -344,4 +371,4 @@ func TestGetNumFmtID(t *testing.T) {  	assert.NotEqual(t, id1, id2)  	assert.NoError(t, f.SaveAs(filepath.Join("test", "TestStyleNumFmt.xlsx"))) -}
\ No newline at end of file +} | 
