fixed the Average function and test case when spanning rows/columns

fixed a Vim error(E907) in Min/Max when comparing v:null to a float
fixed CountE/CountNE/PercentE/PercentNE to support spanning rows/columns
added test cases for all the new formula functions
This commit is contained in:
Eric Davis
2020-04-19 00:20:55 -07:00
parent f755285cf3
commit 2e5713ae6a
3 changed files with 139 additions and 18 deletions

View File

@@ -1,68 +1,104 @@
" Private Functions {{{1 " Private Functions {{{1
function! s:TotalCells(list) "{{{2
let result = 0
for item in a:list
if type(item) == type([])
let result += s:TotalCells(item)
else
let result += 1
endif
endfor
return result
endfunction
function! s:Min(list) "{{{2 function! s:Min(list) "{{{2
let result = v:null let found = v:false
let result = 0
for item in a:list for item in a:list
if empty(item) if empty(item)
continue continue
endif endif
if type(item) == type(1) || type(item) == type(1.0) if type(item) == type(1) || type(item) == type(1.0)
if result == v:null || item < result if found == v:false || item < result
let found = v:true
let result = item let result = item
endif endif
elseif type(item) == type('') elseif type(item) == type('')
let val = str2float(item) let val = str2float(item)
if result == v:null || val < result if found == v:false || val < result
let found = v:true
let result = val let result = val
endif endif
elseif type(item) == type([]) elseif type(item) == type([])
let val = s:Sum(item) let val = s:Min(item)
if result == v:null || val < result if found == v:false || val < result
let found = v:true
let result = val let result = val
endif endif
endif endif
endfor endfor
return result == v:null ? 0 : result return result
endfunction endfunction
function! s:Max(list) "{{{2 function! s:Max(list) "{{{2
let result = v:null let found = v:false
let result = 0
for item in a:list for item in a:list
if empty(item) if empty(item)
continue continue
endif endif
if type(item) == type(1) || type(item) == type(1.0) if type(item) == type(1) || type(item) == type(1.0)
if result == v:null || item > result if found == v:false || item > result
let found = v:true
let result = item let result = item
endif endif
elseif type(item) == type('') elseif type(item) == type('')
let val = str2float(item) let val = str2float(item)
if result == v:null || val > result if found == v:false || val > result
let found = v:true
let result = val let result = val
endif endif
elseif type(item) == type([]) elseif type(item) == type([])
let val = s:Sum(item) let val = s:Max(item)
if result == v:null || val > result if found == v:false || val > result
let found = v:true
let result = val let result = val
endif endif
endif endif
endfor endfor
return result == v:null ? 0 : result return result
endfunction endfunction
function! s:CountE(list) "{{{2 function! s:CountE(list) "{{{2
return len(filter(copy(a:list), {_,l -> empty(l)})) let result = 0
for item in a:list
if empty(item)
let result += 1
elseif type(item) == type([])
let result += s:CountE(item)
endif
endfor
return result
endfunction endfunction
function! s:CountNE(list) "{{{2 function! s:CountNE(list) "{{{2
return len(a:list)-s:CountE(a:list) let result = 0
for item in a:list
if type(item) == type([])
let result += s:CountNE(item)
elseif !empty(item)
let result += 1
endif
endfor
return result
endfunction endfunction
function! s:PercentE(list) "{{{2 function! s:PercentE(list) "{{{2
return (s:CountE(a:list)*100)/len(a:list) return (s:CountE(a:list)*100)/s:TotalCells(a:list)
endfunction endfunction
function! s:PercentNE(list) "{{{2 function! s:PercentNE(list) "{{{2
return (s:CountNE(a:list)*100)/len(a:list) return (s:CountNE(a:list)*100)/s:TotalCells(a:list)
endfunction endfunction
function! s:Sum(list) "{{{2 function! s:Sum(list) "{{{2
@@ -80,7 +116,7 @@ function! s:Sum(list) "{{{2
endfunction endfunction
function! s:Average(list) "{{{2 function! s:Average(list) "{{{2
return s:Sum(a:list)/len(a:list) return s:Sum(a:list)/s:TotalCells(a:list)
endfunction endfunction
function! s:AverageNE(list) "{{{2 function! s:AverageNE(list) "{{{2

View File

@@ -0,0 +1,3 @@
| 1 | 2 | 3 |
| | 0 | |
| 4 | 5 | 6 |

View File

@@ -74,11 +74,93 @@ describe 'spreadsheet'
call cursor(1,3) call cursor(1,3)
Expect tablemode#spreadsheet#Average('1:2') == 2.0 Expect tablemode#spreadsheet#Average('1:2') == 2.0
Expect tablemode#spreadsheet#Average('1,1:1,2') == 1.5 Expect tablemode#spreadsheet#Average('1,1:1,2') == 1.5
Expect tablemode#spreadsheet#Average('1,1:2,2') == 5.0 Expect tablemode#spreadsheet#Average('1,1:2,2') == 2.5
call cursor(2,7) call cursor(2,7)
Expect tablemode#spreadsheet#Average('1:2') == 3.0 Expect tablemode#spreadsheet#Average('1:2') == 3.0
Expect tablemode#spreadsheet#Average('2,1:2,2') == 3.5 Expect tablemode#spreadsheet#Average('2,1:2,2') == 3.5
end end
it 'should return the min of cell range'
call cursor(1,3)
Expect tablemode#spreadsheet#Min('1:2') == 1.0
Expect tablemode#spreadsheet#Min('1,1:1,2') == 1.0
Expect tablemode#spreadsheet#Min('1,1:2,2') == 1.0
call cursor(2,7)
Expect tablemode#spreadsheet#Min('1:2') == 2.0
Expect tablemode#spreadsheet#Min('2,1:2,2') == 3.0
end
it 'should return the max of cell range'
call cursor(1,3)
Expect tablemode#spreadsheet#Max('1:2') == 3.0
Expect tablemode#spreadsheet#Max('1,1:1,2') == 2.0
Expect tablemode#spreadsheet#Max('1,1:2,2') == 4.0
call cursor(2,7)
Expect tablemode#spreadsheet#Max('1:2') == 4.0
Expect tablemode#spreadsheet#Max('2,1:2,2') == 4.0
end
end
describe 'Count'
before
new
read t/fixtures/cell/counts.txt
end
it 'should return the count of empty cell range'
call cursor(1,3)
Expect tablemode#spreadsheet#CountE('1:3') == 1
Expect tablemode#spreadsheet#CountE('1,1:1,3') == 0
Expect tablemode#spreadsheet#CountE('2,1:2,3') == 2
Expect tablemode#spreadsheet#CountE('1,1:3,3') == 2
call cursor(3,11)
Expect tablemode#spreadsheet#CountE('1:3') == 1
Expect tablemode#spreadsheet#CountE('3,1:3,3') == 0
end
it 'should return the count of not-empty cell range'
call cursor(1,3)
Expect tablemode#spreadsheet#CountNE('1:3') == 2
Expect tablemode#spreadsheet#CountNE('1,1:1,3') == 3
Expect tablemode#spreadsheet#CountNE('2,1:2,3') == 1
Expect tablemode#spreadsheet#CountNE('1,1:3,3') == 7
call cursor(3,11)
Expect tablemode#spreadsheet#CountNE('1:3') == 2
Expect tablemode#spreadsheet#CountNE('3,1:3,3') == 3
end
it 'should return the percent count of empty cell range'
call cursor(1,3)
Expect tablemode#spreadsheet#PercentE('1:3') == 33
Expect tablemode#spreadsheet#PercentE('1,1:1,3') == 0
Expect tablemode#spreadsheet#PercentE('2,1:2,3') == 66
Expect tablemode#spreadsheet#PercentE('1,1:3,3') == 22
call cursor(3,11)
Expect tablemode#spreadsheet#PercentE('1:3') == 33
Expect tablemode#spreadsheet#PercentE('3,1:3,3') == 0
end
it 'should return the percent count of not-empty cell range'
call cursor(1,3)
Expect tablemode#spreadsheet#PercentNE('1:3') == 66
Expect tablemode#spreadsheet#PercentNE('1,1:1,3') == 100
Expect tablemode#spreadsheet#PercentNE('2,1:2,3') == 33
Expect tablemode#spreadsheet#PercentNE('1,1:3,3') == 77
call cursor(3,11)
Expect tablemode#spreadsheet#PercentNE('1:3') == 66
Expect tablemode#spreadsheet#PercentNE('3,1:3,3') == 100
end
it 'should return the average of not-empty cell range'
call cursor(1,3)
Expect tablemode#spreadsheet#AverageNE('1:3') == 2.5
Expect tablemode#spreadsheet#AverageNE('1,1:1,3') == 2.0
Expect tablemode#spreadsheet#AverageNE('2,1:2,3') == 0.0
Expect tablemode#spreadsheet#AverageNE('1,1:3,3') == 3.0
call cursor(3,11)
Expect tablemode#spreadsheet#AverageNE('1:3') == 4.5
Expect tablemode#spreadsheet#AverageNE('3,1:3,3') == 5.0
end
end end
end end