Skip to content

Commit 61057c5

Browse files
authored
Number format read fix (#741)
* fix UT-generated file names to be ignored * fix cell value load with invalid number format ID * fix PR issues
1 parent 95d8920 commit 61057c5

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
~$*.xlsx
22
test/Test*.xlsx
33
test/Test*.xlsm
4+
# generated files
5+
test/BadEncrypt.xlsx
6+
test/BadWorkbook.SaveAsEmptyStruct.xlsx
7+
test/*.png
48
*.out
59
*.test
610
.idea

cell.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -762,10 +762,15 @@ func (f *File) formattedValue(s int, v string) string {
762762
return v
763763
}
764764
styleSheet := f.stylesReader()
765+
765766
if s >= len(styleSheet.CellXfs.Xf) {
766767
return v
767768
}
768-
numFmtID := *styleSheet.CellXfs.Xf[s].NumFmtID
769+
var numFmtID int
770+
if styleSheet.CellXfs.Xf[s].NumFmtID != nil {
771+
numFmtID = *styleSheet.CellXfs.Xf[s].NumFmtID
772+
}
773+
769774
ok := builtInNumFmtFunc[numFmtID]
770775
if ok != nil {
771776
return ok(v, builtInNumFmt[numFmtID])

cell_test.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ func TestSetCellRichText(t *testing.T) {
301301
assert.EqualError(t, f.SetCellRichText("Sheet1", "A", richTextRun), `cannot convert cell "A" to coordinates: invalid cell name "A"`)
302302
}
303303

304-
func TestFormattedValue(t *testing.T) {
304+
func TestFormattedValue2(t *testing.T) {
305305
f := NewFile()
306306
v := f.formattedValue(0, "43528")
307307
assert.Equal(t, "43528", v)
@@ -320,12 +320,24 @@ func TestFormattedValue(t *testing.T) {
320320
assert.Equal(t, "03/04/2019", v)
321321

322322
// formatted value with no built-in number format ID
323-
assert.NoError(t, err)
324-
f.Styles.NumFmts = nil
325323
numFmtID := 5
326324
f.Styles.CellXfs.Xf = append(f.Styles.CellXfs.Xf, xlsxXf{
327325
NumFmtID: &numFmtID,
328326
})
327+
v = f.formattedValue(2, "43528")
328+
assert.Equal(t, "43528", v)
329+
330+
// formatted value with invalid number format ID
331+
f.Styles.CellXfs.Xf = append(f.Styles.CellXfs.Xf, xlsxXf{
332+
NumFmtID: nil,
333+
})
334+
v = f.formattedValue(3, "43528")
335+
336+
// formatted value with empty number format
337+
f.Styles.NumFmts = nil
338+
f.Styles.CellXfs.Xf = append(f.Styles.CellXfs.Xf, xlsxXf{
339+
NumFmtID: &numFmtID,
340+
})
329341
v = f.formattedValue(1, "43528")
330342
assert.Equal(t, "43528", v)
331343
}

0 commit comments

Comments
 (0)