Skip to content

Commit

Permalink
fn: COUNTA
Browse files Browse the repository at this point in the history
  • Loading branch information
xuri committed Jun 3, 2020
1 parent b62950a commit b6dd764
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
34 changes: 31 additions & 3 deletions calc.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ func calcAdd(opdStack *Stack) error {
return nil
}

// calcAdd evaluate subtraction arithmetic operations.
// calcSubtract evaluate subtraction arithmetic operations.
func calcSubtract(opdStack *Stack) error {
if opdStack.Len() < 2 {
return errors.New("formula not valid")
Expand All @@ -333,7 +333,7 @@ func calcSubtract(opdStack *Stack) error {
return nil
}

// calcAdd evaluate multiplication arithmetic operations.
// calcMultiply evaluate multiplication arithmetic operations.
func calcMultiply(opdStack *Stack) error {
if opdStack.Len() < 2 {
return errors.New("formula not valid")
Expand All @@ -353,7 +353,7 @@ func calcMultiply(opdStack *Stack) error {
return nil
}

// calcAdd evaluate division arithmetic operations.
// calcDivide evaluate division arithmetic operations.
func calcDivide(opdStack *Stack) error {
if opdStack.Len() < 2 {
return errors.New("formula not valid")
Expand Down Expand Up @@ -2840,6 +2840,34 @@ func (fn *formulaFuncs) TRUNC(argsList *list.List) (result string, err error) {

// Statistical functions

// COUNTA function returns the number of non-blanks within a supplied set of
// cells or values. The syntax of the function is:
//
// COUNTA(value1,[value2],...)
//
func (fn *formulaFuncs) COUNTA(argsList *list.List) (result string, err error) {
var count int
for token := argsList.Front(); token != nil; token = token.Next() {
arg := token.Value.(formulaArg)
switch arg.Type {
case ArgString:
if arg.String != "" {
count++
}
case ArgMatrix:
for _, row := range arg.Matrix {
for _, value := range row {
if value.String != "" {
count++
}
}
}
}
}
result = fmt.Sprintf("%d", count)
return
}

// MEDIAN function returns the statistical median (the middle value) of a list
// of supplied numbers. The syntax of the function is:
//
Expand Down
3 changes: 3 additions & 0 deletions calc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,9 @@ func TestCalcCellValue(t *testing.T) {
"=TRUNC(-99.999,2)": "-99.99",
"=TRUNC(-99.999,-1)": "-90",
// Statistical functions
// COUNTA
`=COUNTA()`: "0",
`=COUNTA(A1:A5,B2:B5,"text",1,2)`: "8",
// MEDIAN
"=MEDIAN(A1:A5,12)": "2",
"=MEDIAN(A1:A5)": "1.5",
Expand Down

0 comments on commit b6dd764

Please sign in to comment.