This closes #1107, stream writer will create a time number format for time type cells

Unit test coverage improved
This commit is contained in:
xuri 2021-12-31 00:00:01 +08:00
parent c5990ea348
commit e37e060d6f
No known key found for this signature in database
GPG Key ID: BA5E5BB1C948EDF7
3 changed files with 33 additions and 27 deletions

View File

@ -228,8 +228,7 @@ func setCellTime(value time.Time) (t string, b string, isNum bool, err error) {
var excelTime float64
_, offset := value.In(value.Location()).Zone()
value = value.Add(time.Duration(offset) * time.Second)
excelTime, err = timeToExcelTime(value)
if err != nil {
if excelTime, err = timeToExcelTime(value); err != nil {
return
}
isNum = excelTime > 0
@ -419,8 +418,7 @@ func setCellStr(value string) (t string, v string, ns xml.Attr) {
}
}
}
t = "str"
v = bstrMarshal(value)
t, v = "str", bstrMarshal(value)
return
}

View File

@ -341,7 +341,7 @@ func (sw *StreamWriter) SetRow(axis string, values []interface{}, opts ...RowOpt
val = v.Value
setCellFormula(&c, v.Formula)
}
if err = setCellValFunc(&c, val); err != nil {
if err = sw.setCellValFunc(&c, val); err != nil {
_, _ = sw.rawData.WriteString(`</row>`)
return err
}
@ -424,7 +424,7 @@ func setCellFormula(c *xlsxC, formula string) {
}
// setCellValFunc provides a function to set value of a cell.
func setCellValFunc(c *xlsxC, val interface{}) (err error) {
func (sw *StreamWriter) setCellValFunc(c *xlsxC, val interface{}) (err error) {
switch val := val.(type) {
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
err = setCellIntFunc(c, val)
@ -439,7 +439,12 @@ func setCellValFunc(c *xlsxC, val interface{}) (err error) {
case time.Duration:
c.T, c.V = setCellDuration(val)
case time.Time:
c.T, c.V, _, err = setCellTime(val)
var isNum bool
c.T, c.V, isNum, err = setCellTime(val)
if isNum && c.S == 0 {
style, _ := sw.File.NewStyle(&Style{NumFmt: 22})
c.S = style
}
case bool:
c.T, c.V = setCellBool(val)
case nil:

View File

@ -58,7 +58,7 @@ func TestStreamWriter(t *testing.T) {
assert.NoError(t, streamWriter.SetRow("A4", []interface{}{Cell{StyleID: styleID}, Cell{Formula: "SUM(A10,B10)"}}), RowOpts{Height: 45, StyleID: styleID})
assert.NoError(t, streamWriter.SetRow("A5", []interface{}{&Cell{StyleID: styleID, Value: "cell"}, &Cell{Formula: "SUM(A10,B10)"}}))
assert.NoError(t, streamWriter.SetRow("A6", []interface{}{time.Now()}))
assert.NoError(t, streamWriter.SetRow("A7", nil, RowOpts{Hidden: true}))
assert.NoError(t, streamWriter.SetRow("A7", nil, RowOpts{Height: 20, Hidden: true, StyleID: styleID}))
assert.EqualError(t, streamWriter.SetRow("A7", nil, RowOpts{Height: MaxRowHeight + 1}), ErrMaxRowHeight.Error())
for rowID := 10; rowID <= 51200; rowID++ {
@ -208,24 +208,27 @@ func TestSetRow(t *testing.T) {
}
func TestSetCellValFunc(t *testing.T) {
f := NewFile()
sw, err := f.NewStreamWriter("Sheet1")
assert.NoError(t, err)
c := &xlsxC{}
assert.NoError(t, setCellValFunc(c, 128))
assert.NoError(t, setCellValFunc(c, int8(-128)))
assert.NoError(t, setCellValFunc(c, int16(-32768)))
assert.NoError(t, setCellValFunc(c, int32(-2147483648)))
assert.NoError(t, setCellValFunc(c, int64(-9223372036854775808)))
assert.NoError(t, setCellValFunc(c, uint(128)))
assert.NoError(t, setCellValFunc(c, uint8(255)))
assert.NoError(t, setCellValFunc(c, uint16(65535)))
assert.NoError(t, setCellValFunc(c, uint32(4294967295)))
assert.NoError(t, setCellValFunc(c, uint64(18446744073709551615)))
assert.NoError(t, setCellValFunc(c, float32(100.1588)))
assert.NoError(t, setCellValFunc(c, float64(100.1588)))
assert.NoError(t, setCellValFunc(c, " Hello"))
assert.NoError(t, setCellValFunc(c, []byte(" Hello")))
assert.NoError(t, setCellValFunc(c, time.Now().UTC()))
assert.NoError(t, setCellValFunc(c, time.Duration(1e13)))
assert.NoError(t, setCellValFunc(c, true))
assert.NoError(t, setCellValFunc(c, nil))
assert.NoError(t, setCellValFunc(c, complex64(5+10i)))
assert.NoError(t, sw.setCellValFunc(c, 128))
assert.NoError(t, sw.setCellValFunc(c, int8(-128)))
assert.NoError(t, sw.setCellValFunc(c, int16(-32768)))
assert.NoError(t, sw.setCellValFunc(c, int32(-2147483648)))
assert.NoError(t, sw.setCellValFunc(c, int64(-9223372036854775808)))
assert.NoError(t, sw.setCellValFunc(c, uint(128)))
assert.NoError(t, sw.setCellValFunc(c, uint8(255)))
assert.NoError(t, sw.setCellValFunc(c, uint16(65535)))
assert.NoError(t, sw.setCellValFunc(c, uint32(4294967295)))
assert.NoError(t, sw.setCellValFunc(c, uint64(18446744073709551615)))
assert.NoError(t, sw.setCellValFunc(c, float32(100.1588)))
assert.NoError(t, sw.setCellValFunc(c, float64(100.1588)))
assert.NoError(t, sw.setCellValFunc(c, " Hello"))
assert.NoError(t, sw.setCellValFunc(c, []byte(" Hello")))
assert.NoError(t, sw.setCellValFunc(c, time.Now().UTC()))
assert.NoError(t, sw.setCellValFunc(c, time.Duration(1e13)))
assert.NoError(t, sw.setCellValFunc(c, true))
assert.NoError(t, sw.setCellValFunc(c, nil))
assert.NoError(t, sw.setCellValFunc(c, complex64(5+10i)))
}