From 490f3063c2cb35a94d64f6a6859cce7b9dee276d Mon Sep 17 00:00:00 2001 From: xuri Date: Sun, 26 Sep 2021 00:07:40 +0800 Subject: This closes #1026, time parse accuracy issue and typo fixed --- cell.go | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) (limited to 'cell.go') diff --git a/cell.go b/cell.go index 902f5b7..b95db9c 100644 --- a/cell.go +++ b/cell.go @@ -1043,13 +1043,18 @@ func (f *File) getCellStringFunc(sheet, axis string, fn func(x *xlsxWorksheet, c // 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, raw bool) string { + precise := v + isNum, precision := isNumeric(v) + if isNum && precision > 10 { + precise, _ = roundPrecision(v) + } if s == 0 || raw { - return v + return precise } styleSheet := f.stylesReader() if s >= len(styleSheet.CellXfs.Xf) { - return v + return precise } var numFmtID int if styleSheet.CellXfs.Xf[s].NumFmtID != nil { @@ -1061,18 +1066,23 @@ func (f *File) formattedValue(s int, v string, raw bool) string { return ok(v, builtInNumFmt[numFmtID]) } if styleSheet == nil || styleSheet.NumFmts == nil { - return v + return precise } for _, xlsxFmt := range styleSheet.NumFmts.NumFmt { if xlsxFmt.NumFmtID == numFmtID { format := strings.ToLower(xlsxFmt.FormatCode) - if strings.Contains(format, "y") || strings.Contains(format, "m") || strings.Contains(strings.Replace(format, "red", "", -1), "d") || strings.Contains(format, "h") { + if isTimeNumFmt(format) { return parseTime(v, format) } - return v + return precise } } - return v + return precise +} + +// isTimeNumFmt determine if the given number format expression is a time number format. +func isTimeNumFmt(format string) bool { + return strings.Contains(format, "y") || strings.Contains(format, "m") || strings.Contains(strings.Replace(format, "red", "", -1), "d") || strings.Contains(format, "h") } // prepareCellStyle provides a function to prepare style index of cell in -- cgit v1.2.1