From 8fbab474443393b8b996487cf7ade300a72d2e07 Mon Sep 17 00:00:00 2001 From: Ri Xu Date: Fri, 5 May 2017 14:40:28 +0800 Subject: - Formatted cell data support, fix issue #48; - Function `SetCellValue()` support `time.Time` data type parameter, relate issue #49; - go doc and go test updated --- cell.go | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) (limited to 'cell.go') diff --git a/cell.go b/cell.go index c40012a..59a9947 100644 --- a/cell.go +++ b/cell.go @@ -6,8 +6,10 @@ import ( "strings" ) -// GetCellValue provides function to get value from cell by given sheet index -// and axis in XLSX file. +// GetCellValue provides function to get formatted value from cell by given +// sheet index and axis in XLSX file. If it is possible to apply a format to the +// cell value, it will do so, if not then an error will be returned, along with +// the raw value of the cell. func (f *File) GetCellValue(sheet, axis string) string { xlsx := f.workSheetReader(sheet) axis = strings.ToUpper(axis) @@ -44,17 +46,29 @@ func (f *File) GetCellValue(sheet, axis string) string { xlsxSI := 0 xlsxSI, _ = strconv.Atoi(r.V) xml.Unmarshal([]byte(f.readXML("xl/sharedStrings.xml")), &shardStrings) - return shardStrings.SI[xlsxSI].T + return f.formattedValue(r.S, shardStrings.SI[xlsxSI].T) case "str": - return r.V + return f.formattedValue(r.S, r.V) default: - return r.V + return f.formattedValue(r.S, r.V) } } } return "" } +// formattedValue provides function to returns a value after formatted. If it is +// possible to apply a format to the cell value, it will do so, if not then an +// error will be returned, along with the raw value of the cell. +func (f *File) formattedValue(s int, v string) string { + if s == 0 { + return v + } + var styleSheet xlsxStyleSheet + xml.Unmarshal([]byte(f.readXML("xl/styles.xml")), &styleSheet) + return builtInNumFmtFunc[styleSheet.CellXfs.Xf[s].NumFmtID](styleSheet.CellXfs.Xf[s].NumFmtID, v) +} + // GetCellFormula provides function to get formula from cell by given sheet // index and axis in XLSX file. func (f *File) GetCellFormula(sheet, axis string) string { -- cgit v1.2.1