From 93533c64ffedbb950e72616055cf6aed256966b9 Mon Sep 17 00:00:00 2001 From: xuri Date: Sat, 2 Jul 2022 13:43:31 +0800 Subject: [PATCH] ref #65, new formula functions: DPRODUCT, DSTDEV, DSTDEVP, DSUM, DVAR, and DVARP --- calc.go | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++ calc_test.go | 26 ++++++++++++++--- 2 files changed, 104 insertions(+), 4 deletions(-) diff --git a/calc.go b/calc.go index 8476f8d411..25c2e180d6 100644 --- a/calc.go +++ b/calc.go @@ -438,7 +438,13 @@ type formulaFuncs struct { // DMIN // DOLLARDE // DOLLARFR +// DPRODUCT +// DSTDEV +// DSTDEVP +// DSUM // DURATION +// DVAR +// DVARP // EFFECT // EDATE // ENCODEURL @@ -18090,6 +18096,18 @@ func (fn *formulaFuncs) database(name string, argsList *list.List) formulaArg { return fn.MAX(args) case "DMIN": return fn.MIN(args) + case "DPRODUCT": + return fn.PRODUCT(args) + case "DSTDEV": + return fn.STDEV(args) + case "DSTDEVP": + return fn.STDEVP(args) + case "DSUM": + return fn.SUM(args) + case "DVAR": + return fn.VAR(args) + case "DVARP": + return fn.VARP(args) default: return fn.AVERAGE(args) } @@ -18176,3 +18194,67 @@ func (fn *formulaFuncs) DMAX(argsList *list.List) formulaArg { func (fn *formulaFuncs) DMIN(argsList *list.List) formulaArg { return fn.database("DMIN", argsList) } + +// DPRODUCT function calculates the product of a field (column) in a database +// for selected records, that satisfy user-specified criteria. The syntax of +// the function is: +// +// DPRODUCT(database,field,criteria) +// +func (fn *formulaFuncs) DPRODUCT(argsList *list.List) formulaArg { + return fn.database("DPRODUCT", argsList) +} + +// DSTDEV function calculates the sample standard deviation of a field +// (column) in a database for selected records only. The records to be +// included in the calculation are defined by a set of one or more +// user-specified criteria. The syntax of the function is: +// +// DSTDEV(database,field,criteria) +// +func (fn *formulaFuncs) DSTDEV(argsList *list.List) formulaArg { + return fn.database("DSTDEV", argsList) +} + +// DSTDEVP function calculates the standard deviation of a field (column) in a +// database for selected records only. The records to be included in the +// calculation are defined by a set of one or more user-specified criteria. +// The syntax of the function is: +// +// DSTDEVP(database,field,criteria) +// +func (fn *formulaFuncs) DSTDEVP(argsList *list.List) formulaArg { + return fn.database("DSTDEVP", argsList) +} + +// DSUM function calculates the sum of a field (column) in a database for +// selected records, that satisfy user-specified criteria. The syntax of the +// function is: +// +// DSUM(database,field,criteria) +// +func (fn *formulaFuncs) DSUM(argsList *list.List) formulaArg { + return fn.database("DSUM", argsList) +} + +// DVAR function calculates the sample variance of a field (column) in a +// database for selected records only. The records to be included in the +// calculation are defined by a set of one or more user-specified criteria. +// The syntax of the function is: +// +// DVAR(database,field,criteria) +// +func (fn *formulaFuncs) DVAR(argsList *list.List) formulaArg { + return fn.database("DVAR", argsList) +} + +// DVARP function calculates the variance (for an entire population), of the +// values in a field (column) in a database for selected records only. The +// records to be included in the calculation are defined by a set of one or +// more user-specified criteria. The syntax of the function is: +// +// DVARP(database,field,criteria) +// +func (fn *formulaFuncs) DVARP(argsList *list.List) formulaArg { + return fn.database("DVARP", argsList) +} diff --git a/calc_test.go b/calc_test.go index 8ad3c775f7..4436b6081f 100644 --- a/calc_test.go +++ b/calc_test.go @@ -4621,6 +4621,7 @@ func TestCalcDatabase(t *testing.T) { assert.NoError(t, f.SetCellFormula("Sheet1", "A3", "=\"=Pear\"")) assert.NoError(t, f.SetCellFormula("Sheet1", "C8", "=NA()")) formulaList := map[string]string{ + "=DAVERAGE(A4:E10,\"Profit\",A1:F3)": "73.25", "=DCOUNT(A4:E10,\"Age\",A1:F2)": "1", "=DCOUNT(A4:E10,,A1:F2)": "2", "=DCOUNT(A4:E10,\"Profit\",A1:F2)": "2", @@ -4635,7 +4636,12 @@ func TestCalcDatabase(t *testing.T) { "=DMAX(A4:E10,\"Profit\",A1:F3)": "96", "=DMIN(A4:E10,\"Tree\",A1:F3)": "0", "=DMIN(A4:E10,\"Profit\",A1:F3)": "45", - "=DAVERAGE(A4:E10,\"Profit\",A1:F3)": "73.25", + "=DPRODUCT(A4:E10,\"Profit\",A1:F3)": "24948000", + "=DSTDEV(A4:E10,\"Profit\",A1:F3)": "21.077238908358", + "=DSTDEVP(A4:E10,\"Profit\",A1:F3)": "18.2534243362718", + "=DSUM(A4:E10,\"Profit\",A1:F3)": "293", + "=DVAR(A4:E10,\"Profit\",A1:F3)": "444.25", + "=DVARP(A4:E10,\"Profit\",A1:F3)": "333.1875", } for formula, expected := range formulaList { assert.NoError(t, f.SetCellFormula("Sheet1", "A11", formula)) @@ -4644,6 +4650,9 @@ func TestCalcDatabase(t *testing.T) { assert.Equal(t, expected, result, formula) } calcError := map[string]string{ + "=DAVERAGE()": "DAVERAGE requires 3 arguments", + "=DAVERAGE(A4:E10,\"x\",A1:F3)": "#VALUE!", + "=DAVERAGE(A4:E10,\"Tree\",A1:F3)": "#DIV/0!", "=DCOUNT()": "DCOUNT requires at least 2 arguments", "=DCOUNT(A4:E10,\"Age\",A1:F2,\"\")": "DCOUNT allows at most 3 arguments", "=DCOUNT(A4,\"Age\",A1:F2)": "#VALUE!", @@ -4660,9 +4669,18 @@ func TestCalcDatabase(t *testing.T) { "=DMAX(A4:E10,\"x\",A1:F3)": "#VALUE!", "=DMIN()": "DMIN requires 3 arguments", "=DMIN(A4:E10,\"x\",A1:F3)": "#VALUE!", - "=DAVERAGE()": "DAVERAGE requires 3 arguments", - "=DAVERAGE(A4:E10,\"x\",A1:F3)": "#VALUE!", - "=DAVERAGE(A4:E10,\"Tree\",A1:F3)": "#DIV/0!", + "=DPRODUCT()": "DPRODUCT requires 3 arguments", + "=DPRODUCT(A4:E10,\"x\",A1:F3)": "#VALUE!", + "=DSTDEV()": "DSTDEV requires 3 arguments", + "=DSTDEV(A4:E10,\"x\",A1:F3)": "#VALUE!", + "=DSTDEVP()": "DSTDEVP requires 3 arguments", + "=DSTDEVP(A4:E10,\"x\",A1:F3)": "#VALUE!", + "=DSUM()": "DSUM requires 3 arguments", + "=DSUM(A4:E10,\"x\",A1:F3)": "#VALUE!", + "=DVAR()": "DVAR requires 3 arguments", + "=DVAR(A4:E10,\"x\",A1:F3)": "#VALUE!", + "=DVARP()": "DVARP requires 3 arguments", + "=DVARP(A4:E10,\"x\",A1:F3)": "#VALUE!", } for formula, expected := range calcError { assert.NoError(t, f.SetCellFormula("Sheet1", "A11", formula))