Skip to content

Commit

Permalink
ref qax-os#65, new formula functions: DMAX and DMIN
Browse files Browse the repository at this point in the history
  • Loading branch information
xuri committed Jun 28, 2022
1 parent 927658f commit 2831072
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
48 changes: 47 additions & 1 deletion calc.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,8 @@ type formulaFuncs struct {
// DELTA
// DEVSQ
// DISC
// DMAX
// DMIN
// DOLLARDE
// DOLLARFR
// DURATION
Expand Down Expand Up @@ -18098,7 +18100,7 @@ func (fn *formulaFuncs) dcount(name string, argsList *list.List) formulaArg {
return newNumberFormulaArg(count)
}

// DOUNT function returns the number of cells containing numeric values, in a
// DCOUNT function returns the number of cells containing numeric values, in a
// field (column) of a database for selected records only. The records to be
// included in the count are those that satisfy a set of one or more
// user-specified criteria. The syntax of the function is:
Expand All @@ -18119,3 +18121,47 @@ func (fn *formulaFuncs) DCOUNT(argsList *list.List) formulaArg {
func (fn *formulaFuncs) DCOUNTA(argsList *list.List) formulaArg {
return fn.dcount("DCOUNTA", argsList)
}

// dmaxmin is an implementation of the formula functions DMAX and DMIN.
func (fn *formulaFuncs) dmaxmin(name string, argsList *list.List) formulaArg {
if argsList.Len() != 3 {
return newErrorFormulaArg(formulaErrorVALUE, fmt.Sprintf("%s requires 3 arguments", name))
}
database := argsList.Front().Value.(formulaArg)
field := argsList.Front().Next().Value.(formulaArg)
criteria := argsList.Back().Value.(formulaArg)
db := newCalcDatabase(database, field, criteria)
if db == nil {
return newErrorFormulaArg(formulaErrorVALUE, formulaErrorVALUE)
}
args := list.New()
for db.next() {
args.PushBack(db.value())
}
if name == "DMAX" {
return fn.MAX(args)
}
return fn.MIN(args)
}

// DMAX function finds the maximum value 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:
//
// DMAX(database,field,criteria)
//
func (fn *formulaFuncs) DMAX(argsList *list.List) formulaArg {
return fn.dmaxmin("DMAX", argsList)
}

// DMIN function finds the minimum value 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:
//
// DMIN(database,field,criteria)
//
func (fn *formulaFuncs) DMIN(argsList *list.List) formulaArg {
return fn.dmaxmin("DMIN", argsList)
}
10 changes: 9 additions & 1 deletion calc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4603,7 +4603,7 @@ func TestCalcCOVAR(t *testing.T) {
}
}

func TestCalcDCOUNTandDCOUNTA(t *testing.T) {
func TestCalcDCOUNTandDCOUNTAandDMAXandDMIN(t *testing.T) {
cellData := [][]interface{}{
{"Tree", "Height", "Age", "Yield", "Profit", "Height"},
{"=Apple", ">1000%", nil, nil, nil, "<16"},
Expand Down Expand Up @@ -4631,6 +4631,10 @@ func TestCalcDCOUNTandDCOUNTA(t *testing.T) {
"=DCOUNTA(A4:E10,\"Profit\",A1:F2)": "2",
"=DCOUNTA(A4:E10,\"Tree\",A1:F2)": "2",
"=DCOUNTA(A4:E10,\"Age\",A2:F3)": "0",
"=DMAX(A4:E10,\"Tree\",A1:F3)": "0",
"=DMAX(A4:E10,\"Profit\",A1:F3)": "96",
"=DMIN(A4:E10,\"Tree\",A1:F3)": "0",
"=DMIN(A4:E10,\"Profit\",A1:F3)": "45",
}
for formula, expected := range formulaList {
assert.NoError(t, f.SetCellFormula("Sheet1", "A11", formula))
Expand All @@ -4651,6 +4655,10 @@ func TestCalcDCOUNTandDCOUNTA(t *testing.T) {
"=DCOUNTA(A4:E10,NA(),A1:F2)": "#VALUE!",
"=DCOUNTA(A4:E4,,A1:F2)": "#VALUE!",
"=DCOUNTA(A4:E10,\"x\",A2:F3)": "#VALUE!",
"=DMAX()": "DMAX requires 3 arguments",
"=DMAX(A4:E10,\"x\",A1:F3)": "#VALUE!",
"=DMIN()": "DMIN requires 3 arguments",
"=DMIN(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 2831072

Please sign in to comment.