Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 80 additions & 14 deletions calc.go
Original file line number Diff line number Diff line change
Expand Up @@ -14996,6 +14996,66 @@
return newStringFormulaArg(argsList.Back().Value.(formulaArg).Value())
}

// calcMatch returns the position of the value by given match type, criteria
// and lookup array for the formula function MATCH.
func calcMatchMatrix(vertical bool, matchType int, criteria *formulaCriteria, lookupArray [][]formulaArg) formulaArg {
idx := -1
var result *formulaArg

var calc = func(i int, arg formulaArg) bool {
switch matchType {
case 0:
if ok, _ := formulaCriteriaEval(arg, criteria); ok {
out := newNumberFormulaArg(float64(i + 1))
result = &out
return true
}

Check warning on line 15012 in calc.go

View check run for this annotation

Codecov / codecov/patch

calc.go#L15007-L15012

Added lines #L15007 - L15012 were not covered by tests
case -1:
if ok, _ := formulaCriteriaEval(arg, &formulaCriteria{
Type: criteriaGe, Condition: criteria.Condition,
}); ok {
idx = i
return false
}
if criteria.Condition.Type == ArgNumber {
return true
}
case 1:
if ok, _ := formulaCriteriaEval(arg, &formulaCriteria{
Type: criteriaLe, Condition: criteria.Condition,
}); ok {
idx = i
return false
}

Check warning on line 15029 in calc.go

View check run for this annotation

Codecov / codecov/patch

calc.go#L15027-L15029

Added lines #L15027 - L15029 were not covered by tests
if criteria.Condition.Type == ArgNumber {
return true
}
}
return false

Check warning on line 15034 in calc.go

View check run for this annotation

Codecov / codecov/patch

calc.go#L15034

Added line #L15034 was not covered by tests
}

if vertical {
for i, row := range lookupArray {
if ok := calc(i, row[0]); ok {
break
}
}
} else {
for i, cell := range lookupArray[0] {
if ok := calc(i, cell); ok {
break
}
}
}
if result != nil {
return *result
}

Check warning on line 15052 in calc.go

View check run for this annotation

Codecov / codecov/patch

calc.go#L15051-L15052

Added lines #L15051 - L15052 were not covered by tests
if idx == -1 {
return newErrorFormulaArg(formulaErrorNA, formulaErrorNA)
}
return newNumberFormulaArg(float64(idx + 1))
}

// calcMatch returns the position of the value by given match type, criteria
// and lookup array for the formula function MATCH.
func calcMatch(matchType int, criteria *formulaCriteria, lookupArray []formulaArg) formulaArg {
Expand Down Expand Up @@ -15110,18 +15170,8 @@
// lookupLinearSearch sequentially checks each look value of the lookup array until
// a match is found or the whole list has been searched.
func lookupLinearSearch(vertical bool, lookupValue, lookupArray, matchMode, searchMode formulaArg) (int, bool) {
var tableArray []formulaArg
if vertical {
for _, row := range lookupArray.Matrix {
tableArray = append(tableArray, row[0])
}
} else {
tableArray = lookupArray.Matrix[0]
}
matchIdx, wasExact := -1, false
start:
for i, cell := range tableArray {
lhs := cell
var linearSearch = func(i int, cell, lhs formulaArg) bool {
if lookupValue.Type == ArgNumber {
if lhs = cell.ToNumber(); lhs.Type == ArgError {
lhs = cell
Expand All @@ -15135,12 +15185,28 @@
matchIdx = i
wasExact = true
if searchMode.Number == searchModeLinear {
break start
return true
}
}
if matchMode.Number == matchModeMinGreater || matchMode.Number == matchModeMaxLess {
matchIdx = int(calcMatch(int(matchMode.Number), formulaCriteriaParser(lookupValue), tableArray).Number)
continue
matchIdx = int(calcMatchMatrix(vertical, int(matchMode.Number), formulaCriteriaParser(lookupValue), lookupArray.Matrix).Number)
return false
}
return false
}

if vertical {
for i, row := range lookupArray.Matrix {
lhs := row[0]
if linearSearch(i, lhs, lhs) {
break
}
}
} else {
for i, lhs := range lookupArray.Matrix[0] {
if linearSearch(i, lhs, lhs) {
break
}
}
}
return matchIdx, wasExact
Expand Down