- Add nil pointer guard in cell format - Add tests to verify the nil checks in formattedValue Co-authored-by: Zach Clark <zachmclark@gmail.com>
This commit is contained in:
parent
4998b7b929
commit
75c912ca95
5
cell.go
5
cell.go
|
@ -1292,6 +1292,9 @@ func (f *File) formattedValue(s int, v string, raw bool) string {
|
||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
styleSheet := f.stylesReader()
|
styleSheet := f.stylesReader()
|
||||||
|
if styleSheet.CellXfs == nil {
|
||||||
|
return v
|
||||||
|
}
|
||||||
if s >= len(styleSheet.CellXfs.Xf) {
|
if s >= len(styleSheet.CellXfs.Xf) {
|
||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
|
@ -1306,7 +1309,7 @@ func (f *File) formattedValue(s int, v string, raw bool) string {
|
||||||
if ok := builtInNumFmtFunc[numFmtID]; ok != nil {
|
if ok := builtInNumFmtFunc[numFmtID]; ok != nil {
|
||||||
return ok(v, builtInNumFmt[numFmtID], date1904)
|
return ok(v, builtInNumFmt[numFmtID], date1904)
|
||||||
}
|
}
|
||||||
if styleSheet == nil || styleSheet.NumFmts == nil {
|
if styleSheet.NumFmts == nil {
|
||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
for _, xlsxFmt := range styleSheet.NumFmts.NumFmt {
|
for _, xlsxFmt := range styleSheet.NumFmts.NumFmt {
|
||||||
|
|
29
cell_test.go
29
cell_test.go
|
@ -744,6 +744,35 @@ func TestFormattedValue2(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestFormattedValueNilXfs(t *testing.T) {
|
||||||
|
// Set the CellXfs to nil and verify that the formattedValue function does not crash.
|
||||||
|
f := NewFile()
|
||||||
|
f.Styles.CellXfs = nil
|
||||||
|
assert.Equal(t, "43528", f.formattedValue(3, "43528", false))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFormattedValueNilNumFmts(t *testing.T) {
|
||||||
|
// Set the NumFmts value to nil and verify that the formattedValue function does not crash.
|
||||||
|
f := NewFile()
|
||||||
|
f.Styles.NumFmts = nil
|
||||||
|
assert.Equal(t, "43528", f.formattedValue(3, "43528", false))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFormattedValueNilWorkbook(t *testing.T) {
|
||||||
|
// Set the Workbook value to nil and verify that the formattedValue function does not crash.
|
||||||
|
f := NewFile()
|
||||||
|
f.WorkBook = nil
|
||||||
|
assert.Equal(t, "43528", f.formattedValue(3, "43528", false))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFormattedValueNilWorkbookPr(t *testing.T) {
|
||||||
|
// Set the WorkBook.WorkbookPr value to nil and verify that the formattedValue function does not
|
||||||
|
// crash.
|
||||||
|
f := NewFile()
|
||||||
|
f.WorkBook.WorkbookPr = nil
|
||||||
|
assert.Equal(t, "43528", f.formattedValue(3, "43528", false))
|
||||||
|
}
|
||||||
|
|
||||||
func TestSharedStringsError(t *testing.T) {
|
func TestSharedStringsError(t *testing.T) {
|
||||||
f, err := OpenFile(filepath.Join("test", "Book1.xlsx"), Options{UnzipXMLSizeLimit: 128})
|
f, err := OpenFile(filepath.Join("test", "Book1.xlsx"), Options{UnzipXMLSizeLimit: 128})
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
Loading…
Reference in New Issue