From 1fcfc8f0b00f7335da68b2301080b7ac2632d20f Mon Sep 17 00:00:00 2001 From: Imhven Date: Sun, 19 Feb 2023 16:40:12 +0800 Subject: [PATCH 1/2] test: add CalcFormulaValue test case --- calc.go | 9 +++++++-- calc_test.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/calc.go b/calc.go index 6c0a3b0..46d7852 100644 --- a/calc.go +++ b/calc.go @@ -815,7 +815,7 @@ func (f *File) calcCellValue(ctx *calcContext, sheet, cell string) (result formu return } -func (f *File) CalcFormulaValue(ctx *calcContext, sheet, formula string) (result string, err error) { +func (f *File) CalcFormulaValue(sheet, formula string) (result string, err error) { var ( token formulaArg ) @@ -824,7 +824,12 @@ func (f *File) CalcFormulaValue(ctx *calcContext, sheet, formula string) (result if tokens == nil { return } - if token, err = f.evalInfixExp(ctx, sheet, "", tokens); err != nil { + if token, err = f.evalInfixExp(&calcContext{ + entry: fmt.Sprintf("%s!%s", sheet, ""), + maxCalcIterations: getOptions().MaxCalcIterations, + iterations: make(map[string]uint), + iterationsCache: make(map[string]formulaArg), + }, sheet, "", tokens); err != nil { return } result = token.Value() diff --git a/calc_test.go b/calc_test.go index c740e6b..13e2b8c 100644 --- a/calc_test.go +++ b/calc_test.go @@ -5888,3 +5888,50 @@ func TestCalcCellResolver(t *testing.T) { assert.Equal(t, expected, result, formula) } } + +func TestFile_CalcFormulaValue(t *testing.T) { + cellData := [][]interface{}{ + {1, 4, nil, "Month", "Team", "Sales"}, + {2, 5, nil, "Jan", "North 1", 36693}, + {3, nil, nil, "Jan", "North 2", 22100}, + {0, nil, nil, "Jan", "South 1", 53321}, + {nil, nil, nil, "Jan", "South 2", 34440}, + {nil, nil, nil, "Feb", "North 1", 29889}, + {nil, nil, nil, "Feb", "North 2", 50090}, + {nil, nil, nil, "Feb", "South 1", 32080}, + {nil, nil, nil, "Feb", "South 2", 45500}, + } + type args struct { + sheet string + formula string + } + tests := []struct { + name string + f *File + args args + wantResult string + wantErr bool + }{ + { + name: "test CalcFormulaValue", + f: prepareCalcData(cellData), + args: args{ + sheet: "Sheet1", + formula: "A1+B1", + }, + wantResult: "5", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + gotResult, err := tt.f.CalcFormulaValue(tt.args.sheet, tt.args.formula) + if (err != nil) != tt.wantErr { + t.Errorf("File.CalcFormulaValue() error = %v, wantErr %v", err, tt.wantErr) + return + } + if gotResult != tt.wantResult { + t.Errorf("File.CalcFormulaValue() = %v, want %v", gotResult, tt.wantResult) + } + }) + } +} From d83096cdb79773263224c9d8b033b4ad6283c1c9 Mon Sep 17 00:00:00 2001 From: Imhven Date: Sun, 19 Feb 2023 16:53:59 +0800 Subject: [PATCH 2/2] test: add Power CalcFormulaValue test case --- calc_test.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/calc_test.go b/calc_test.go index 13e2b8c..73357cb 100644 --- a/calc_test.go +++ b/calc_test.go @@ -5921,6 +5921,15 @@ func TestFile_CalcFormulaValue(t *testing.T) { }, wantResult: "5", }, + { + name: "test Power CalcFormulaValue", + f: prepareCalcData(cellData), + args: args{ + sheet: "Sheet1", + formula: "A1^2+LN(B1)", + }, + wantResult: "2.386294361119891", + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) {