Skip to content

Commit

Permalink
Implemented EXP, INT, and MONTH functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Christopher Rankin committed Apr 10, 2013
1 parent 3fa911a commit d46088e
Show file tree
Hide file tree
Showing 9 changed files with 136 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ source 'http://rubygems.org'
gem 'nokogiri'
gem 'ffi'
gem 'rubypeg'
gem 'active_support'

group :test do
gem 'ZenTest'
gem 'rspec'
end
end
29 changes: 29 additions & 0 deletions spec/excel/excel_functions/exp_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require_relative '../../spec_helper.rb'

describe "ExcelFunctions: EXP" do

it "should return the constant e when given a power of 1" do
FunctionTest.exp(1).should == Math::E
end

it "should return the the square of constant e when given a power of 2 (7.3890560989306495)" do
FunctionTest.exp(2).should == Math::E ** 2
end

it "should return an error when given inappropriate arguments" do
FunctionTest.exp("Asdasddf").should == :error
end

it "should treat nil as zero" do
FunctionTest.exp(nil).should == 0
end

it "should return an error if an argument is an error" do
FunctionTest.exp(:error).should == :error
end

it "should be in the list of functions that can be mapped to ruby" do
MapFormulaeToRuby::FUNCTIONS['EXP'].should == 'exp'
end

end
33 changes: 33 additions & 0 deletions spec/excel/excel_functions/int_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require_relative '../../spec_helper.rb'

describe "ExcelFunctions: INT" do

it "should return 1 rounded to lowest integer (1.49)" do
FunctionTest.int(1.49).should == 1
end

it "should return 3 rounded to lowest integer (3.0723)" do
FunctionTest.int(3).should == 3
end

it "should return 7 rounded to lowest integer (7.9999999999)" do
FunctionTest.int(7).should == 7
end

it "should return an error when given inappropriate arguments" do
FunctionTest.int("Asdasddf").should == :error
end

it "should treat nil as zero" do
FunctionTest.int(nil).should == 0
end

it "should return an error if an argument is an error" do
FunctionTest.int(:error).should == :error
end

it "should be in the list of functions that can be mapped to ruby" do
MapFormulaeToRuby::FUNCTIONS['INT'].should == 'int'
end

end
25 changes: 25 additions & 0 deletions spec/excel/excel_functions/month_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require_relative '../../spec_helper.rb'

describe "ExcelFunctions: MONTH" do

it "should return 3 when date is 03/26/2013 (serial: 41359)" do
FunctionTest.month(41359).should == 3
end

it "should return an error when given inappropriate arguments" do
FunctionTest.month("Asdasddf").should == :error
end

it "should treat nil as zero" do
FunctionTest.month(nil).should == 0
end

it "should return an error if an argument is an error" do
FunctionTest.month(:error).should == :error
end

it "should be in the list of functions that can be mapped to ruby" do
MapFormulaeToRuby::FUNCTIONS['MONTH'].should == 'month'
end

end
3 changes: 3 additions & 0 deletions src/compile/ruby/map_formulae_to_ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,18 @@ class MapFormulaeToRuby < MapValuesToRuby
'COSH' => 'cosh',
'COUNT' => 'count',
'COUNTA' => 'counta',
'EXP' => 'exp',
'FIND' => 'find',
'IF' => 'excel_if',
'IFERROR' => 'iferror',
'INDEX' => 'index',
'INT' => 'int',
'LEFT' => 'left',
'MATCH' => 'excel_match',
'MAX' => 'max',
'MIN' => 'min',
'MOD' => 'mod',
'MONTH' => 'month',
'PI' => 'pi',
'PMT' => 'pmt',
'ROUND' => 'round',
Expand Down
6 changes: 6 additions & 0 deletions src/excel/excel_functions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,9 @@ module ExcelFunctions
require_relative 'excel_functions/rounddown'

require_relative 'excel_functions/negative'

require_relative 'excel_functions/exp'

require_relative 'excel_functions/int'

require_relative 'excel_functions/month'
11 changes: 11 additions & 0 deletions src/excel/excel_functions/exp.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module ExcelFunctions

def exp(a)
return 0 if a.nil?
return :error unless a.is_a? Numeric
a ||= 0
result = Math::E ** a
return result
end

end
11 changes: 11 additions & 0 deletions src/excel/excel_functions/int.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module ExcelFunctions

def int(a)
return 0 if a.nil?
return :error unless a.is_a? Numeric
a ||= 0
result = a.to_s.split('.')[0].to_i
return result
end

end
16 changes: 16 additions & 0 deletions src/excel/excel_functions/month.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require 'date'
require 'active_support'
require 'active_support/core_ext'

module ExcelFunctions

def month(a)
return 0 if a.nil?
return :error unless a.is_a? Numeric
a ||= 0

result = (Date.civil(1899, 12, 31) + (a - 1).days).month
return result
end

end

0 comments on commit d46088e

Please sign in to comment.