diff --git a/.gitignore b/.gitignore index 7e82ee290c..ce92812619 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,9 @@ ~$*.xlsx test/Test*.xlsx test/Test*.xlsm +# generated files +test/BadEncrypt.xlsx +test/BadWorkbook.SaveAsEmptyStruct.xlsx test/*.png *.out *.test diff --git a/cell.go b/cell.go index c797974764..22adefd454 100644 --- a/cell.go +++ b/cell.go @@ -766,11 +766,11 @@ func (f *File) formattedValue(s int, v string) string { if s >= len(styleSheet.CellXfs.Xf) { return v } - if styleSheet.CellXfs.Xf[s].NumFmtID == nil { - return v + var numFmtID int + if styleSheet.CellXfs.Xf[s].NumFmtID != nil { + numFmtID = *styleSheet.CellXfs.Xf[s].NumFmtID } - 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 23c8474ddb..8d3f77482d 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,7 +320,6 @@ 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) numFmtID := 5 f.Styles.CellXfs.Xf = append(f.Styles.CellXfs.Xf, xlsxXf{ NumFmtID: &numFmtID, @@ -329,31 +328,16 @@ func TestFormattedValue(t *testing.T) { assert.Equal(t, "43528", v) // formatted value with invalid number format ID - assert.NoError(t, err) f.Styles.CellXfs.Xf = append(f.Styles.CellXfs.Xf, xlsxXf{ NumFmtID: nil, }) v = f.formattedValue(3, "43528") - assert.Equal(t, "43528", v) -} -func TestInvalidNumberFormatsLoad(t *testing.T) { - f, err := OpenFile(filepath.Join("test", "Items.xlsx")) - assert.NoError(t, err) - - rows, err := f.Rows("Sheet 1") - assert.NoError(t, err) - - var rowCount int - for rows.Next() { - rowCount++ - row, err := rows.Columns() - assert.NoError(t, err) - if rowCount > 1 { - assert.Equal(t, "0.5", row[13]) - assert.Equal(t, "12/08/2020", row[14]) - } - } - - assert.Equal(t, 20, rowCount) + // 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) } diff --git a/crypt_test.go b/crypt_test.go index d98b97b577..6f712c1944 100644 --- a/crypt_test.go +++ b/crypt_test.go @@ -19,7 +19,7 @@ import ( func TestEncrypt(t *testing.T) { f, err := OpenFile(filepath.Join("test", "encryptSHA1.xlsx"), Options{Password: "password"}) assert.NoError(t, err) - assert.EqualError(t, f.SaveAs(filepath.Join("test", "TestBadEncrypt.xlsx"), Options{Password: "password"}), "not support encryption currently") + assert.EqualError(t, f.SaveAs(filepath.Join("test", "BadEncrypt.xlsx"), Options{Password: "password"}), "not support encryption currently") } func TestEncryptionMechanism(t *testing.T) { diff --git a/excelize_test.go b/excelize_test.go index 89e3749a6f..1b4887275b 100644 --- a/excelize_test.go +++ b/excelize_test.go @@ -257,7 +257,7 @@ func TestBrokenFile(t *testing.T) { t.Run("SaveAsEmptyStruct", func(t *testing.T) { // Test write file with broken file struct with given path. - assert.NoError(t, f.SaveAs(filepath.Join("test", "TestBadWorkbook.SaveAsEmptyStruct.xlsx"))) + assert.NoError(t, f.SaveAs(filepath.Join("test", "BadWorkbook.SaveAsEmptyStruct.xlsx"))) }) t.Run("OpenBadWorkbook", func(t *testing.T) { diff --git a/test/Items.xlsx b/test/Items.xlsx deleted file mode 100644 index e2b233fb6d..0000000000 Binary files a/test/Items.xlsx and /dev/null differ