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
This commit is contained in:
Artem Kustikov 2020-12-12 11:17:00 +03:00 committed by GitHub
parent 95d8920c8e
commit 61057c58d3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 4 deletions

4
.gitignore vendored
View File

@ -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

View File

@ -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])

View File

@ -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)
}