Go language library for reading and writing Microsoft Excel™ (XLAM / XLSM / XLSX / XLTM / XLTX) spreadsheets
修订版 | eee6607e477f229d2459628324cbfae5549f611a (tree) |
---|---|
时间 | 2022-06-29 00:18:48 |
作者 | ![]() |
Commiter | xuri |
ref #65, new formula functions: DMAX and DMIN
@@ -433,6 +433,8 @@ type formulaFuncs struct { | ||
433 | 433 | // DELTA |
434 | 434 | // DEVSQ |
435 | 435 | // DISC |
436 | +// DMAX | |
437 | +// DMIN | |
436 | 438 | // DOLLARDE |
437 | 439 | // DOLLARFR |
438 | 440 | // DURATION |
@@ -18098,7 +18100,7 @@ func (fn *formulaFuncs) dcount(name string, argsList *list.List) formulaArg { | ||
18098 | 18100 | return newNumberFormulaArg(count) |
18099 | 18101 | } |
18100 | 18102 | |
18101 | -// DOUNT function returns the number of cells containing numeric values, in a | |
18103 | +// DCOUNT function returns the number of cells containing numeric values, in a | |
18102 | 18104 | // field (column) of a database for selected records only. The records to be |
18103 | 18105 | // included in the count are those that satisfy a set of one or more |
18104 | 18106 | // user-specified criteria. The syntax of the function is: |
@@ -18119,3 +18121,47 @@ func (fn *formulaFuncs) DCOUNT(argsList *list.List) formulaArg { | ||
18119 | 18121 | func (fn *formulaFuncs) DCOUNTA(argsList *list.List) formulaArg { |
18120 | 18122 | return fn.dcount("DCOUNTA", argsList) |
18121 | 18123 | } |
18124 | + | |
18125 | +// dmaxmin is an implementation of the formula functions DMAX and DMIN. | |
18126 | +func (fn *formulaFuncs) dmaxmin(name string, argsList *list.List) formulaArg { | |
18127 | + if argsList.Len() != 3 { | |
18128 | + return newErrorFormulaArg(formulaErrorVALUE, fmt.Sprintf("%s requires 3 arguments", name)) | |
18129 | + } | |
18130 | + database := argsList.Front().Value.(formulaArg) | |
18131 | + field := argsList.Front().Next().Value.(formulaArg) | |
18132 | + criteria := argsList.Back().Value.(formulaArg) | |
18133 | + db := newCalcDatabase(database, field, criteria) | |
18134 | + if db == nil { | |
18135 | + return newErrorFormulaArg(formulaErrorVALUE, formulaErrorVALUE) | |
18136 | + } | |
18137 | + args := list.New() | |
18138 | + for db.next() { | |
18139 | + args.PushBack(db.value()) | |
18140 | + } | |
18141 | + if name == "DMAX" { | |
18142 | + return fn.MAX(args) | |
18143 | + } | |
18144 | + return fn.MIN(args) | |
18145 | +} | |
18146 | + | |
18147 | +// DMAX function finds the maximum value in a field (column) in a database for | |
18148 | +// selected records only. The records to be included in the calculation are | |
18149 | +// defined by a set of one or more user-specified criteria. The syntax of the | |
18150 | +// function is: | |
18151 | +// | |
18152 | +// DMAX(database,field,criteria) | |
18153 | +// | |
18154 | +func (fn *formulaFuncs) DMAX(argsList *list.List) formulaArg { | |
18155 | + return fn.dmaxmin("DMAX", argsList) | |
18156 | +} | |
18157 | + | |
18158 | +// DMIN function finds the minimum value in a field (column) in a database for | |
18159 | +// selected records only. The records to be included in the calculation are | |
18160 | +// defined by a set of one or more user-specified criteria. The syntax of the | |
18161 | +// function is: | |
18162 | +// | |
18163 | +// DMIN(database,field,criteria) | |
18164 | +// | |
18165 | +func (fn *formulaFuncs) DMIN(argsList *list.List) formulaArg { | |
18166 | + return fn.dmaxmin("DMIN", argsList) | |
18167 | +} |
@@ -4603,7 +4603,7 @@ func TestCalcCOVAR(t *testing.T) { | ||
4603 | 4603 | } |
4604 | 4604 | } |
4605 | 4605 | |
4606 | -func TestCalcDCOUNTandDCOUNTA(t *testing.T) { | |
4606 | +func TestCalcDCOUNTandDCOUNTAandDMAXandDMIN(t *testing.T) { | |
4607 | 4607 | cellData := [][]interface{}{ |
4608 | 4608 | {"Tree", "Height", "Age", "Yield", "Profit", "Height"}, |
4609 | 4609 | {"=Apple", ">1000%", nil, nil, nil, "<16"}, |
@@ -4631,6 +4631,10 @@ func TestCalcDCOUNTandDCOUNTA(t *testing.T) { | ||
4631 | 4631 | "=DCOUNTA(A4:E10,\"Profit\",A1:F2)": "2", |
4632 | 4632 | "=DCOUNTA(A4:E10,\"Tree\",A1:F2)": "2", |
4633 | 4633 | "=DCOUNTA(A4:E10,\"Age\",A2:F3)": "0", |
4634 | + "=DMAX(A4:E10,\"Tree\",A1:F3)": "0", | |
4635 | + "=DMAX(A4:E10,\"Profit\",A1:F3)": "96", | |
4636 | + "=DMIN(A4:E10,\"Tree\",A1:F3)": "0", | |
4637 | + "=DMIN(A4:E10,\"Profit\",A1:F3)": "45", | |
4634 | 4638 | } |
4635 | 4639 | for formula, expected := range formulaList { |
4636 | 4640 | assert.NoError(t, f.SetCellFormula("Sheet1", "A11", formula)) |
@@ -4651,6 +4655,10 @@ func TestCalcDCOUNTandDCOUNTA(t *testing.T) { | ||
4651 | 4655 | "=DCOUNTA(A4:E10,NA(),A1:F2)": "#VALUE!", |
4652 | 4656 | "=DCOUNTA(A4:E4,,A1:F2)": "#VALUE!", |
4653 | 4657 | "=DCOUNTA(A4:E10,\"x\",A2:F3)": "#VALUE!", |
4658 | + "=DMAX()": "DMAX requires 3 arguments", | |
4659 | + "=DMAX(A4:E10,\"x\",A1:F3)": "#VALUE!", | |
4660 | + "=DMIN()": "DMIN requires 3 arguments", | |
4661 | + "=DMIN(A4:E10,\"x\",A1:F3)": "#VALUE!", | |
4654 | 4662 | } |
4655 | 4663 | for formula, expected := range calcError { |
4656 | 4664 | assert.NoError(t, f.SetCellFormula("Sheet1", "A11", formula)) |