Skip to content

Commit

Permalink
This closes #1851, and closes #1856 fix formula calculation result ro…
Browse files Browse the repository at this point in the history
…und issue (#1860)

- The SetSheetName function now support case sensitivity
- Update unit tests
  • Loading branch information
realzuojianxiang committed Mar 22, 2024
1 parent 9e884c7 commit 5975d87
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 15 deletions.
18 changes: 9 additions & 9 deletions calc.go
Expand Up @@ -831,16 +831,18 @@ 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
}
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
}

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
26 changes: 22 additions & 4 deletions calc_test.go
Expand Up @@ -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})
Expand Down
2 changes: 1 addition & 1 deletion sheet.go
Expand Up @@ -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()
Expand Down
5 changes: 4 additions & 1 deletion sheet_test.go
Expand Up @@ -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))
Expand Down

0 comments on commit 5975d87

Please sign in to comment.