Module: ExcelFunctions
- Included in:
- Excel
- Defined in:
- lib/excel_functions.rb,
lib/excel_functions/lookup.rb,
lib/excel_functions/version.rb,
lib/excel_functions/financial.rb
Overview
file containing the financial functions
Constant Summary collapse
- VERSION =
"0.0.4"
Instance Method Summary collapse
-
#lookup(value, lookup_array, output_array = nil) ⇒ Object
The LOOKUP function searches for the value in the lookup_range and returns the value from the same position in the result_range.
-
#npv(rate, *values) ⇒ Object
Calculates the net present value of an investment by using a discount rate and a series of future payments (negative values) and income (positive values).
-
#pmt(rate, nper, pv, fv = 0, type = 0) ⇒ Float
Calculates the payment for a loan based on constant payments and a constant interest rate.
Instance Method Details
#lookup(value, lookup_array, output_array = nil) ⇒ Object
The LOOKUP function searches for the value in the lookup_range and returns the value from the same position in the result_range. If this parameter is omitted, the LOOKUP function will return the first column of data. In Syntax #2, the LOOKUP function searches for the value in the first row or column of the array and returns the corresponding value in the last row or column of the array.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/excel_functions/lookup.rb', line 14 def lookup(value, lookup_array, output_array = nil) #combining both syntaxes here by taking 1st col of lookup_array to lookup # and the output_array or the last column of the lookup_array to output output_array = output_array || lookup_array.map{|arr| Array(arr)[-1]} lookup_array = lookup_array.map{|arr| Array(arr)[0]} index = lookup_array.find_index(value) return output_array[index] if index #raise only after exact check fails, we only need sorted for approx lookup raise "lookup_array must be sorted" unless lookup_array.sort == lookup_array #better way to check sorted? first_greater_index = lookup_array.find_index{|look| look > value} || lookup_array.count less_than_index = first_greater_index - 1 less_than_index >=0 ? output_array[less_than_index] : nil end |
#npv(rate, *values) ⇒ Object
Calculates the net present value of an investment by using a discount rate and a series of future payments (negative values) and income (positive values).
8 9 10 11 12 |
# File 'lib/excel_functions/financial.rb', line 8 def npv(rate,*values) #If n is the number of cash flows in the list of values, the formula for NPV is: # sum upto n (values[i]/(1+rate)^i) values.each_with_index.inject(0){|sum,(val, index)| sum + val/((1.0+rate)**(index+1))} end |
#pmt(rate, nper, pv, fv = 0, type = 0) ⇒ Float
Calculates the payment for a loan based on constant payments and a constant interest rate.
28 29 30 31 32 33 34 35 |
# File 'lib/excel_functions/financial.rb', line 28 def pmt(rate,nper,pv,fv =0,type = 0) #- pv/((1 - (1 / (1 + rate)**nper )) / rate) pv = -pv k = [1,1+rate][type] -(pv + (pv - fv)/((1+rate) ** nper - 1))* -rate/k #added some extra neg signs to get it to work end |