Method: RubyExcel::Sheet#averageif
- Defined in:
- lib/rubyexcel/sheet.rb
#averageif(find_header, avg_header) { ... } ⇒ Object
Average the values in a Column by searching another Column
145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/rubyexcel/sheet.rb', line 145 def averageif( find_header, avg_header ) return to_enum( :sumif ) unless block_given? find_col, avg_col = ch( find_header ), ch( avg_header ) sum = find_col.each_cell_wh.inject([0,0]) do |sum,ce| if yield( ce.value ) sum[0] += avg_col[ ce.row ] sum[1] += 1 sum else sum end end sum.first.to_f / sum.last end |