• R/O
  • HTTP
  • SSH
  • HTTPS

excelize: 提交

Go language library for reading and writing Microsoft Excel™ (XLAM / XLSM / XLSX / XLTM / XLTX) spreadsheets


Commit MetaInfo

修订版eee6607e477f229d2459628324cbfae5549f611a (tree)
时间2022-06-29 00:18:48
作者xuri <xuri.me@gmai...>
Commiterxuri

Log Message

ref #65, new formula functions: DMAX and DMIN

更改概述

差异

--- a/calc.go
+++ b/calc.go
@@ -433,6 +433,8 @@ type formulaFuncs struct {
433433 // DELTA
434434 // DEVSQ
435435 // DISC
436+// DMAX
437+// DMIN
436438 // DOLLARDE
437439 // DOLLARFR
438440 // DURATION
@@ -18098,7 +18100,7 @@ func (fn *formulaFuncs) dcount(name string, argsList *list.List) formulaArg {
1809818100 return newNumberFormulaArg(count)
1809918101 }
1810018102
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
1810218104 // field (column) of a database for selected records only. The records to be
1810318105 // included in the count are those that satisfy a set of one or more
1810418106 // user-specified criteria. The syntax of the function is:
@@ -18119,3 +18121,47 @@ func (fn *formulaFuncs) DCOUNT(argsList *list.List) formulaArg {
1811918121 func (fn *formulaFuncs) DCOUNTA(argsList *list.List) formulaArg {
1812018122 return fn.dcount("DCOUNTA", argsList)
1812118123 }
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+}
--- a/calc_test.go
+++ b/calc_test.go
@@ -4603,7 +4603,7 @@ func TestCalcCOVAR(t *testing.T) {
46034603 }
46044604 }
46054605
4606-func TestCalcDCOUNTandDCOUNTA(t *testing.T) {
4606+func TestCalcDCOUNTandDCOUNTAandDMAXandDMIN(t *testing.T) {
46074607 cellData := [][]interface{}{
46084608 {"Tree", "Height", "Age", "Yield", "Profit", "Height"},
46094609 {"=Apple", ">1000%", nil, nil, nil, "<16"},
@@ -4631,6 +4631,10 @@ func TestCalcDCOUNTandDCOUNTA(t *testing.T) {
46314631 "=DCOUNTA(A4:E10,\"Profit\",A1:F2)": "2",
46324632 "=DCOUNTA(A4:E10,\"Tree\",A1:F2)": "2",
46334633 "=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",
46344638 }
46354639 for formula, expected := range formulaList {
46364640 assert.NoError(t, f.SetCellFormula("Sheet1", "A11", formula))
@@ -4651,6 +4655,10 @@ func TestCalcDCOUNTandDCOUNTA(t *testing.T) {
46514655 "=DCOUNTA(A4:E10,NA(),A1:F2)": "#VALUE!",
46524656 "=DCOUNTA(A4:E4,,A1:F2)": "#VALUE!",
46534657 "=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!",
46544662 }
46554663 for formula, expected := range calcError {
46564664 assert.NoError(t, f.SetCellFormula("Sheet1", "A11", formula))
Show on old repository browser