excel_functions 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ OTg4NmNlZDk3MmNhODRhYzZkM2IyOTc1NjAxZTU3MjQ2MzNhYTIzYQ==
5
+ data.tar.gz: !binary |-
6
+ MTNjYjhkNmJiMThlYWIwYTU3N2U3ZTBiYTBlZGNhZDI2MWI2NmVlOA==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ YzUzNTE4ZDNjZGZmN2U5ZjAxZjFlN2I2ZThkZWIyYzFlZjhhOGU5YTdjOTIw
10
+ NTg4YTkxYjZhYzQ3OGEwZDAxMGQzN2MxZDk4NTE2NTFmYjFjNTRhNDUxNTU5
11
+ NTlkZDhhZGEzNmY1ZTYwMzM3ZDMzNzA2ODU3NjllNDYzNTQzYTk=
12
+ data.tar.gz: !binary |-
13
+ MTQxZGI5YzZlOGE2ZjMxYzRjYWFmODUxMDlmZWI0NTBmMzNlOGYyN2ZjMmZi
14
+ MGIzZGY1NTM0YmM4NTQxMmUzNTZhMWQyZTFiYjdlMzhhYjg2Y2Y2MGE0NWIw
15
+ NjJjNTFkOWVlZTZjMzk0M2M0MzI5YjAxYTYzOWU2YzkwNzUyYjI=
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
5
+ - 2.1.0
6
+ script: bundle exec rspec
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in excel_functions.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Karthik T
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,36 @@
1
+ # ExcelFunctions
2
+
3
+ **Reverse engineered versions of some Excel functions**
4
+ This gem can be used if you need to reverse engineer any excel spreadsheets. Use the functions here to convert excel formulae into ruby code easily
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'excel_functions'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install excel_functions
19
+
20
+ ## Usage
21
+
22
+ This gem can be used in 2 ways.
23
+
24
+ 1. Include `ExcelFunctions` module in the class that needs it and use its functions
25
+ 2. Use the `Excel.<function>` style anywhere.
26
+
27
+ #TODO
28
+ - more docs about the functions themselves, for now, you can take a look at the code
29
+
30
+ ## Contributing
31
+
32
+ 1. Fork it
33
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
34
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
35
+ 4. Push to the branch (`git push origin my-new-feature`)
36
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'excel_functions/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "excel_functions"
8
+ spec.version = ExcelFunctions::VERSION
9
+ spec.authors = ["Karthik T"]
10
+ spec.email = ["karthikt.holmes+github@gmail.com"]
11
+ spec.description = %q{Reverse engineered versions of some Excel functions}
12
+ spec.summary = %q{This gem can be used if you need to reverse engineer any excel spreadsheets.}
13
+ spec.homepage = "https://github.com/ktaragorn/excel_functions"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ end
@@ -0,0 +1,10 @@
1
+ require "excel_functions/version"
2
+ require "excel_functions/financial"
3
+ require "excel_functions/lookup"
4
+ module ExcelFunctions
5
+
6
+ end
7
+
8
+ class Excel
9
+ extend ExcelFunctions
10
+ end
@@ -0,0 +1,30 @@
1
+ #file containing the financial functions
2
+ module ExcelFunctions
3
+ # attempt to implement http://office.microsoft.com/en-sg/excel-help/npv-HP005209199.aspx
4
+ def npv(rate,*values)
5
+ #If n is the number of cash flows in the list of values, the formula for NPV is:
6
+ # sum upto n (values[i]/(1+rate)^i)
7
+ values.each_with_index.inject(0){|sum,(val, index)| sum + val/((1.0+rate)**(index+1))}
8
+ end
9
+
10
+ #http://office.microsoft.com/en-sg/excel-help/pmt-HP005209215.aspx
11
+ #Calculates the payment for a loan based on constant payments and a constant interest rate.
12
+ # Rate is the interest rate for the loan.
13
+ # Nper is the total number of payments for the loan.
14
+ # Pv is the present value, or the total amount that a series of future payments is worth now; also known as the principal.
15
+ # Fv is the future value, or a cash balance you want to attain after the last payment is made. If fv is omitted, it is assumed to be 0 (zero), that is, the future value of a loan is 0.
16
+ # Type is the number 0 (zero) or 1 and indicates when payments are due.
17
+ # Set type equal to If payments are due
18
+ # 0 or omitted At the end of the period
19
+ # 1 At the beginning of the period
20
+ # full formula at http://www.getobjects.com/Components/Finance/TVM/formulas.html
21
+ #first 3 params at http://answers.yahoo.com/question/index?qid=20080822070859AAY94ZT
22
+ def pmt(rate,nper,pv,fv =0,type = 0)
23
+ #- pv/((1 - (1 / (1 + rate)**nper )) / rate)
24
+ pv = -pv
25
+
26
+ k = [1,1+rate][type]
27
+
28
+ -(pv + (pv - fv)/((1+rate) ** nper - 1))* -rate/k #added some extra neg signs to get it to work
29
+ end
30
+ end
@@ -0,0 +1,25 @@
1
+ #file containing the lookup based functions
2
+ module ExcelFunctions
3
+ #attempt to implement http://www.techonthenet.com/excel/formulas/lookup.php
4
+ def lookup(value, lookup_array, output_array = nil)
5
+ #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.
6
+ #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.
7
+ #combining both syntaxes here by taking 1st col of lookup_array to lookup
8
+ # and the output_array or the last column of the lookup_array to output
9
+ output_array = output_array || lookup_array.map{|arr| Array(arr)[-1]}
10
+
11
+ lookup_array = lookup_array.map{|arr| Array(arr)[0]}
12
+
13
+ #If the values in the LOOKUP_range are not sorted in ascending order, the LOOKUP function will return the incorrect value.
14
+ raise "lookup_array must be sorted" unless lookup_array.sort == lookup_array #better way to check sorted?
15
+
16
+ index = lookup_array.find_index(value)
17
+ return output_array[index] if index
18
+
19
+ # If the LOOKUP function can not find an exact match, it chooses the largest value in the lookup_range that is less than or equal to the value.
20
+ # If the value is smaller than all of the values in the lookup_range, then the LOOKUP function will return #N/A.
21
+ first_greater_index = lookup_array.find_index{|look| look > value} || lookup_array.count
22
+ less_than_index = first_greater_index - 1
23
+ less_than_index >=0 ? output_array[less_than_index] : nil
24
+ end
25
+ end
@@ -0,0 +1,3 @@
1
+ module ExcelFunctions
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,40 @@
1
+ require "excel_functions"
2
+ describe Excel do
3
+ describe "Financial" do
4
+ it "calculates npv correctly" do
5
+ expect(Excel.npv(0.5, 50, 45,34)).to be_within(0.01).of(63.41)
6
+ expect(Excel.npv(0.2,15,53,23,64,23)).to be_within(0.01).of(102.72)
7
+ end
8
+
9
+ it "calculates pmt correctly" do
10
+ # examples from http://office.microsoft.com/en-sg/excel-help/pmt-HP005209215.aspx?CTT=1
11
+ expect(Excel.pmt(0.08/12, 10,10000)).to be_within(0.01).of(-1037.03)
12
+ expect(Excel.pmt(0.08/12, 10,10000,0,1)).to be_within(0.01).of(-1030.16)
13
+ expect(Excel.pmt(0.06/12, 18*12,0,50000)).to be_within(0.01).of(-129.08)
14
+ end
15
+ end
16
+ describe "Lookup" do
17
+ #should this be 3 it blocks?
18
+ it "performs lookup correctly" do
19
+ input_arr = [0,0.05,0.1,0.15]
20
+ output_arr = ["a","b","c","d"]
21
+ tests = [
22
+ [0, "a"],
23
+ [0.1, "c"],
24
+ [0.12, "c"],
25
+ [1, "d"],
26
+ [-1, nil]
27
+ ]
28
+ #2 arrays
29
+ tests.each do |(value, result)|
30
+ expect(Excel.lookup(value,input_arr, output_arr)).to be_eql(result)
31
+ end
32
+ #single array
33
+ tests.each do |(value, result)|
34
+ expect(Excel.lookup(value,input_arr.zip(output_arr))).to be_eql(result)
35
+ end
36
+
37
+ expect{Excel.lookup(0.1,input_arr.reverse,output_arr)}.to raise_error
38
+ end
39
+ end
40
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: excel_functions
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Karthik T
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-01-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Reverse engineered versions of some Excel functions
56
+ email:
57
+ - karthikt.holmes+github@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - .travis.yml
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - excel_functions.gemspec
69
+ - lib/excel_functions.rb
70
+ - lib/excel_functions/financial.rb
71
+ - lib/excel_functions/lookup.rb
72
+ - lib/excel_functions/version.rb
73
+ - spec/excelfunctions_spec.rb
74
+ homepage: https://github.com/ktaragorn/excel_functions
75
+ licenses:
76
+ - MIT
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ! '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ! '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.0.5
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: This gem can be used if you need to reverse engineer any excel spreadsheets.
98
+ test_files:
99
+ - spec/excelfunctions_spec.rb