Skip to content

Commit 917e6e1

Browse files
authored
This roundup time value when a millisecond great than 500 to fix the accuracy issue
- Correction example in the documentation of set cell formula - Rename the internal function `parseOptions` to `getOptions` - Update unit tests
1 parent 4f0025a commit 917e6e1

8 files changed

+24
-15
lines changed

calc.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -769,7 +769,7 @@ type formulaFuncs struct {
769769
// ZTEST
770770
func (f *File) CalcCellValue(sheet, cell string, opts ...Options) (result string, err error) {
771771
var (
772-
rawCellValue = parseOptions(opts...).RawCellValue
772+
rawCellValue = getOptions(opts...).RawCellValue
773773
styleIdx int
774774
token formulaArg
775775
)

cell.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func (f *File) GetCellValue(sheet, cell string, opts ...Options) (string, error)
7171
if err != nil {
7272
return "", true, err
7373
}
74-
val, err := c.getValueFrom(f, sst, parseOptions(opts...).RawCellValue)
74+
val, err := c.getValueFrom(f, sst, getOptions(opts...).RawCellValue)
7575
return val, true, err
7676
})
7777
}
@@ -640,12 +640,12 @@ type FormulaOpts struct {
640640
//
641641
// err := f.SetCellFormula("Sheet1", "A3", "=SUM(A1,B1)")
642642
//
643-
// Example 2, set one-dimensional vertical constant array (row array) formula
643+
// Example 2, set one-dimensional vertical constant array (column array) formula
644644
// "1,2,3" for the cell "A3" on "Sheet1":
645645
//
646-
// err := f.SetCellFormula("Sheet1", "A3", "={1,2,3}")
646+
// err := f.SetCellFormula("Sheet1", "A3", "={1;2;3}")
647647
//
648-
// Example 3, set one-dimensional horizontal constant array (column array)
648+
// Example 3, set one-dimensional horizontal constant array (row array)
649649
// formula '"a","b","c"' for the cell "A3" on "Sheet1":
650650
//
651651
// err := f.SetCellFormula("Sheet1", "A3", "={\"a\",\"b\",\"c\"}")
@@ -654,7 +654,7 @@ type FormulaOpts struct {
654654
// the cell "A3" on "Sheet1":
655655
//
656656
// formulaType, ref := excelize.STCellFormulaTypeArray, "A3:A3"
657-
// err := f.SetCellFormula("Sheet1", "A3", "={1,2,\"a\",\"b\"}",
657+
// err := f.SetCellFormula("Sheet1", "A3", "={1,2;\"a\",\"b\"}",
658658
// excelize.FormulaOpts{Ref: &ref, Type: &formulaType})
659659
//
660660
// Example 5, set range array formula "A1:A2" for the cell "A3" on "Sheet1":

col.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func (cols *Cols) Rows(opts ...Options) ([]string, error) {
9292
if cols.stashCol >= cols.curCol {
9393
return rowIterator.cells, rowIterator.err
9494
}
95-
cols.rawCellValue = parseOptions(opts...).RawCellValue
95+
cols.rawCellValue = getOptions(opts...).RawCellValue
9696
if cols.sst, rowIterator.err = cols.f.sharedStringsReader(); rowIterator.err != nil {
9797
return rowIterator.cells, rowIterator.err
9898
}

date.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,11 @@ func timeFromExcelTime(excelTime float64, date1904 bool) time.Time {
156156
date = excel1900Epoc
157157
}
158158
durationPart := time.Duration(nanosInADay * floatPart)
159-
return date.AddDate(0, 0, wholeDaysPart).Add(durationPart).Truncate(time.Second)
159+
date = date.AddDate(0, 0, wholeDaysPart).Add(durationPart)
160+
if date.Nanosecond()/1e6 > 500 {
161+
return date.Round(time.Second)
162+
}
163+
return date.Truncate(time.Second)
160164
}
161165

162166
// ExcelDateToTime converts a float-based excel date representation to a time.Time.

excelize.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ func OpenReader(r io.Reader, opts ...Options) (*File, error) {
161161
return nil, err
162162
}
163163
f := newFile()
164-
f.options = parseOptions(opts...)
164+
f.options = getOptions(opts...)
165165
if err = f.checkOpenReaderOptions(); err != nil {
166166
return nil, err
167167
}
@@ -198,9 +198,9 @@ func OpenReader(r io.Reader, opts ...Options) (*File, error) {
198198
return f, err
199199
}
200200

201-
// parseOptions provides a function to parse the optional settings for open
201+
// getOptions provides a function to parse the optional settings for open
202202
// and reading spreadsheet.
203-
func parseOptions(opts ...Options) *Options {
203+
func getOptions(opts ...Options) *Options {
204204
options := &Options{}
205205
for _, opt := range opts {
206206
options = &opt

numfmt_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ func TestNumFmt(t *testing.T) {
4949
{"43543.086539351854", "AM/PM hh:mm:ss a/p", "AM 02:04:37 a"},
5050
{"43528", "YYYY", "2019"},
5151
{"43528", "", "43528"},
52-
{"43528.2123", "YYYY-MM-DD hh:mm:ss", "2019-03-04 05:05:42"},
53-
{"43528.2123", "YYYY-MM-DD hh:mm:ss;YYYY-MM-DD hh:mm:ss", "2019-03-04 05:05:42"},
54-
{"43528.2123", "M/D/YYYY h:m:s", "3/4/2019 5:5:42"},
52+
{"43528.2123", "YYYY-MM-DD hh:mm:ss", "2019-03-04 05:05:43"},
53+
{"43528.2123", "YYYY-MM-DD hh:mm:ss;YYYY-MM-DD hh:mm:ss", "2019-03-04 05:05:43"},
54+
{"43528.2123", "M/D/YYYY h:m:s", "3/4/2019 5:5:43"},
5555
{"43528.003958333335", "m/d/yyyy h:m:s", "3/4/2019 0:5:42"},
5656
{"43528.003958333335", "M/D/YYYY h:mm:s", "3/4/2019 0:05:42"},
5757
{"0.64583333333333337", "h:mm:ss am/pm", "3:30:00 pm"},

rows.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ func (rows *Rows) Columns(opts ...Options) ([]string, error) {
138138
}
139139
var rowIterator rowXMLIterator
140140
var token xml.Token
141-
rows.rawCellValue = parseOptions(opts...).RawCellValue
141+
rows.rawCellValue = getOptions(opts...).RawCellValue
142142
if rows.sst, rowIterator.err = rows.f.sharedStringsReader(); rowIterator.err != nil {
143143
return rowIterator.cells, rowIterator.err
144144
}

rows_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -1052,6 +1052,8 @@ func TestNumberFormats(t *testing.T) {
10521052
assert.NoError(t, err)
10531053
numFmt10, err := f.NewStyle(&Style{NumFmt: 10})
10541054
assert.NoError(t, err)
1055+
numFmt21, err := f.NewStyle(&Style{NumFmt: 21})
1056+
assert.NoError(t, err)
10551057
numFmt37, err := f.NewStyle(&Style{NumFmt: 37})
10561058
assert.NoError(t, err)
10571059
numFmt38, err := f.NewStyle(&Style{NumFmt: 38})
@@ -1093,6 +1095,9 @@ func TestNumberFormats(t *testing.T) {
10931095
{"A30", numFmt40, -8.8888666665555493e+19, "(88,888,666,665,555,500,000.00)"},
10941096
{"A31", numFmt40, 8.8888666665555487, "8.89 "},
10951097
{"A32", numFmt40, -8.8888666665555487, "(8.89)"},
1098+
{"A33", numFmt21, 44729.999988368058, "23:59:59"},
1099+
{"A34", numFmt21, 44944.375005787035, "09:00:00"},
1100+
{"A35", numFmt21, 44944.375005798611, "09:00:01"},
10961101
} {
10971102
cell, styleID, value, expected := cases[0].(string), cases[1].(int), cases[2], cases[3].(string)
10981103
assert.NoError(t, f.SetCellStyle("Sheet1", cell, cell, styleID))

0 commit comments

Comments
 (0)