This closes #1851, and closes #1856 fix formula calculation result round issue (#1860)

- The SetSheetName function now support case sensitivity
- Update unit tests
This commit is contained in:
realzuojianxiang 2024-03-22 16:09:45 +08:00 committed by GitHub
parent 9e884c798b
commit 5975d87f7e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 35 additions and 14 deletions

18
calc.go
View File

@ -831,8 +831,8 @@ func (f *File) CalcCellValue(sheet, cell string, opts ...Options) (result string
if !rawCellValue {
styleIdx, _ = f.GetCellStyle(sheet, cell)
}
result = token.Value()
if isNum, precision, decimal := isNumeric(result); isNum && !rawCellValue {
if token.Type == ArgNumber && !token.Boolean {
_, precision, decimal := isNumeric(token.Value())
if precision > 15 {
result, err = f.formattedValue(&xlsxC{S: styleIdx, V: strings.ToUpper(strconv.FormatFloat(decimal, 'G', 15, 64))}, rawCellValue, CellTypeNumber)
return
@ -840,7 +840,9 @@ func (f *File) CalcCellValue(sheet, cell string, opts ...Options) (result string
if !strings.HasPrefix(result, "0") {
result, err = f.formattedValue(&xlsxC{S: styleIdx, V: strings.ToUpper(strconv.FormatFloat(decimal, 'f', -1, 64))}, rawCellValue, CellTypeNumber)
}
return
}
result, err = f.formattedValue(&xlsxC{S: styleIdx, V: token.Value()}, rawCellValue, CellTypeInlineString)
return
}
@ -4281,7 +4283,7 @@ func (fn *formulaFuncs) EXP(argsList *list.List) formulaArg {
if number.Type == ArgError {
return number
}
return newStringFormulaArg(strings.ToUpper(fmt.Sprintf("%g", math.Exp(number.Number))))
return newNumberFormulaArg(math.Exp(number.Number))
}
// fact returns the factorial of a supplied number.
@ -4359,7 +4361,7 @@ func (fn *formulaFuncs) FLOOR(argsList *list.List) formulaArg {
val--
}
}
return newStringFormulaArg(strings.ToUpper(fmt.Sprintf("%g", val*significance.Number)))
return newNumberFormulaArg(val * significance.Number)
}
// FLOORdotMATH function rounds a supplied number down to a supplied multiple
@ -11570,12 +11572,10 @@ func (fn *formulaFuncs) ISNA(argsList *list.List) formulaArg {
if argsList.Len() != 1 {
return newErrorFormulaArg(formulaErrorVALUE, "ISNA requires 1 argument")
}
token := argsList.Front().Value.(formulaArg)
result := "FALSE"
if token.Type == ArgError && token.String == formulaErrorNA {
result = "TRUE"
if token := argsList.Front().Value.(formulaArg); token.Type == ArgError && token.String == formulaErrorNA {
return newBoolFormulaArg(true)
}
return newStringFormulaArg(result)
return newBoolFormulaArg(false)
}
// ISNONTEXT function tests if a supplied value is text. If not, the

View File

@ -6311,13 +6311,31 @@ func TestFormulaRawCellValueOption(t *testing.T) {
raw bool
expected string
}{
{"=\"10e3\"", false, "10000"},
{"=VALUE(\"1.0E-07\")", false, "0.00"},
{"=VALUE(\"1.0E-07\")", true, "0.0000001"},
{"=\"text\"", false, "$text"},
{"=\"text\"", true, "text"},
{"=\"10e3\"", false, "$10e3"},
{"=\"10e3\"", true, "10e3"},
{"=\"10\" & \"e3\"", false, "10000"},
{"=\"10\" & \"e3\"", false, "$10e3"},
{"=\"10\" & \"e3\"", true, "10e3"},
{"=\"1111111111111111\"", false, "1.11111111111111E+15"},
{"=10e3", false, "10000.00"},
{"=10e3", true, "10000"},
{"=\"1111111111111111\"", false, "$1111111111111111"},
{"=\"1111111111111111\"", true, "1111111111111111"},
{"=1111111111111111", false, "1111111111111110.00"},
{"=1111111111111111", true, "1.11111111111111E+15"},
{"=1444.00000000003", false, "1444.00"},
{"=1444.00000000003", true, "1444.00000000003"},
{"=1444.000000000003", false, "1444.00"},
{"=1444.000000000003", true, "1444"},
{"=ROUND(1444.00000000000003,2)", false, "1444.00"},
{"=ROUND(1444.00000000000003,2)", true, "1444"},
}
exp := "0.00;0.00;;$@"
styleID, err := f.NewStyle(&Style{CustomNumFmt: &exp})
assert.NoError(t, err)
assert.NoError(t, f.SetCellStyle("Sheet1", "A1", "A1", styleID))
for _, test := range rawTest {
assert.NoError(t, f.SetCellFormula("Sheet1", "A1", test.value))
val, err := f.CalcCellValue("Sheet1", "A1", Options{RawCellValue: test.raw})

View File

@ -363,7 +363,7 @@ func (f *File) SetSheetName(source, target string) error {
if err = checkSheetName(target); err != nil {
return err
}
if strings.EqualFold(target, source) {
if target == source {
return err
}
wb, _ := f.workbookReader()

View File

@ -465,8 +465,11 @@ func TestSetSheetName(t *testing.T) {
// Test set worksheet with the same name
assert.NoError(t, f.SetSheetName("Sheet1", "Sheet1"))
assert.Equal(t, "Sheet1", f.GetSheetName(0))
// Test set worksheet with the different name
assert.NoError(t, f.SetSheetName("Sheet1", "sheet1"))
assert.Equal(t, "sheet1", f.GetSheetName(0))
// Test set sheet name with invalid sheet name
assert.EqualError(t, f.SetSheetName("Sheet:1", "Sheet1"), ErrSheetNameInvalid.Error())
assert.Equal(t, f.SetSheetName("Sheet:1", "Sheet1"), ErrSheetNameInvalid)
// Test set worksheet name with existing defined name and auto filter
assert.NoError(t, f.AutoFilter("Sheet1", "A1:A2", nil))