diff --git a/.gitignore b/.gitignore index 685d2bfd..ce928126 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,10 @@ ~$*.xlsx test/Test*.xlsx test/Test*.xlsm +# generated files +test/BadEncrypt.xlsx +test/BadWorkbook.SaveAsEmptyStruct.xlsx +test/*.png *.out *.test .idea diff --git a/cell.go b/cell.go index 1edfd24b..22adefd4 100644 --- a/cell.go +++ b/cell.go @@ -762,10 +762,15 @@ func (f *File) formattedValue(s int, v string) string { return v } styleSheet := f.stylesReader() + if s >= len(styleSheet.CellXfs.Xf) { return v } - numFmtID := *styleSheet.CellXfs.Xf[s].NumFmtID + var numFmtID int + if styleSheet.CellXfs.Xf[s].NumFmtID != nil { + numFmtID = *styleSheet.CellXfs.Xf[s].NumFmtID + } + ok := builtInNumFmtFunc[numFmtID] if ok != nil { return ok(v, builtInNumFmt[numFmtID]) diff --git a/cell_test.go b/cell_test.go index c934876e..8d3f7748 100644 --- a/cell_test.go +++ b/cell_test.go @@ -301,7 +301,7 @@ func TestSetCellRichText(t *testing.T) { assert.EqualError(t, f.SetCellRichText("Sheet1", "A", richTextRun), `cannot convert cell "A" to coordinates: invalid cell name "A"`) } -func TestFormattedValue(t *testing.T) { +func TestFormattedValue2(t *testing.T) { f := NewFile() v := f.formattedValue(0, "43528") assert.Equal(t, "43528", v) @@ -320,12 +320,24 @@ func TestFormattedValue(t *testing.T) { assert.Equal(t, "03/04/2019", v) // formatted value with no built-in number format ID - assert.NoError(t, err) - f.Styles.NumFmts = nil numFmtID := 5 f.Styles.CellXfs.Xf = append(f.Styles.CellXfs.Xf, xlsxXf{ NumFmtID: &numFmtID, }) + v = f.formattedValue(2, "43528") + assert.Equal(t, "43528", v) + + // formatted value with invalid number format ID + f.Styles.CellXfs.Xf = append(f.Styles.CellXfs.Xf, xlsxXf{ + NumFmtID: nil, + }) + v = f.formattedValue(3, "43528") + + // formatted value with empty number format + f.Styles.NumFmts = nil + f.Styles.CellXfs.Xf = append(f.Styles.CellXfs.Xf, xlsxXf{ + NumFmtID: &numFmtID, + }) v = f.formattedValue(1, "43528") assert.Equal(t, "43528", v) }