Skip to content

Commit

Permalink
ref qax-os#65, new formula functions: DPRODUCT, DSTDEV, DSTDEVP, DSUM…
Browse files Browse the repository at this point in the history
…, DVAR, and DVARP
  • Loading branch information
xuri committed Jul 2, 2022
1 parent 7feb16b commit 93533c6
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 4 deletions.
82 changes: 82 additions & 0 deletions calc.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,13 @@ type formulaFuncs struct {
// DMIN
// DOLLARDE
// DOLLARFR
// DPRODUCT
// DSTDEV
// DSTDEVP
// DSUM
// DURATION
// DVAR
// DVARP
// EFFECT
// EDATE
// ENCODEURL
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
26 changes: 22 additions & 4 deletions calc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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))
Expand All @@ -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!",
Expand All @@ -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))
Expand Down

0 comments on commit 93533c6

Please sign in to comment.