- Function `SetCellValue()` time.Duration support added, relate issue #176;

- go test updated
This commit is contained in:
Ri Xu 2018-01-05 09:39:31 +08:00
parent e13ccce89a
commit 52b1a8e896
No known key found for this signature in database
GPG Key ID: BA5E5BB1C948EDF7
3 changed files with 31 additions and 16 deletions

38
cell.go
View File

@ -39,6 +39,7 @@ func (f *File) mergeCellsParser(xlsx *xlsxWorksheet, axis string) string {
// float64
// string
// []byte
// time.Duration
// time.Time
// nil
//
@ -46,6 +47,30 @@ func (f *File) mergeCellsParser(xlsx *xlsxWorksheet, axis string) string {
// set numbers format by SetCellStyle() method.
func (f *File) SetCellValue(sheet, axis string, value interface{}) {
switch t := value.(type) {
case float32:
f.SetCellDefault(sheet, axis, strconv.FormatFloat(float64(value.(float32)), 'f', -1, 32))
case float64:
f.SetCellDefault(sheet, axis, strconv.FormatFloat(float64(value.(float64)), 'f', -1, 64))
case string:
f.SetCellStr(sheet, axis, t)
case []byte:
f.SetCellStr(sheet, axis, string(t))
case time.Duration:
f.SetCellDefault(sheet, axis, strconv.FormatFloat(float64(value.(time.Duration).Seconds()/86400), 'f', -1, 32))
f.setDefaultTimeStyle(sheet, axis, 21)
case time.Time:
f.SetCellDefault(sheet, axis, strconv.FormatFloat(float64(timeToExcelTime(timeToUTCTime(value.(time.Time)))), 'f', -1, 64))
f.setDefaultTimeStyle(sheet, axis, 22)
case nil:
f.SetCellStr(sheet, axis, "")
default:
f.setCellIntValue(sheet, axis, value)
}
}
// setCellIntValue provides function to set int value of a cell.
func (f *File) setCellIntValue(sheet, axis string, value interface{}) {
switch value.(type) {
case int:
f.SetCellInt(sheet, axis, value.(int))
case int8:
@ -66,19 +91,6 @@ func (f *File) SetCellValue(sheet, axis string, value interface{}) {
f.SetCellInt(sheet, axis, int(value.(uint32)))
case uint64:
f.SetCellInt(sheet, axis, int(value.(uint64)))
case float32:
f.SetCellDefault(sheet, axis, strconv.FormatFloat(float64(value.(float32)), 'f', -1, 32))
case float64:
f.SetCellDefault(sheet, axis, strconv.FormatFloat(float64(value.(float64)), 'f', -1, 64))
case string:
f.SetCellStr(sheet, axis, t)
case []byte:
f.SetCellStr(sheet, axis, string(t))
case time.Time:
f.SetCellDefault(sheet, axis, strconv.FormatFloat(float64(timeToExcelTime(timeToUTCTime(value.(time.Time)))), 'f', -1, 64))
f.setDefaultTimeStyle(sheet, axis)
case nil:
f.SetCellStr(sheet, axis, "")
default:
f.SetCellStr(sheet, axis, fmt.Sprintf("%v", value))
}

View File

@ -70,10 +70,11 @@ func OpenReader(r io.Reader) (*File, error) {
}
// setDefaultTimeStyle provides function to set default numbers format for
// time.Time type cell value by given worksheet name and cell coordinates.
func (f *File) setDefaultTimeStyle(sheet, axis string) {
// time.Time type cell value by given worksheet name, cell coordinates and
// number format code.
func (f *File) setDefaultTimeStyle(sheet, axis string, format int) {
if f.GetCellStyle(sheet, axis) == 0 {
style, _ := f.NewStyle(`{"number_format": 22}`)
style, _ := f.NewStyle(`{"number_format": ` + strconv.Itoa(format) + `}`)
f.SetCellStyle(sheet, axis, axis, style)
}
}

View File

@ -83,6 +83,8 @@ func TestOpenFile(t *testing.T) {
xlsx.SetCellValue("Sheet2", "F16", true)
xlsx.SetCellValue("Sheet2", "G2", nil)
xlsx.SetCellValue("Sheet2", "G4", time.Now())
// 02:46:40
xlsx.SetCellValue("Sheet2", "G5", time.Duration(1e13))
// Test completion column.
xlsx.SetCellValue("Sheet2", "M2", nil)
// Test read cell value with given axis large than exists row.