-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathmap_formulae_to_ruby.rb
More file actions
103 lines (89 loc) · 2.38 KB
/
map_formulae_to_ruby.rb
File metadata and controls
103 lines (89 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
require_relative 'map_values_to_ruby'
class MapFormulaeToRuby < MapValuesToRuby
attr_accessor :sheet_names
attr_accessor :worksheet
FUNCTIONS = {
'*' => 'multiply',
'+' => 'add',
'-' => 'subtract',
'/' => 'divide',
'<' => 'less_than?',
'<=' => 'less_than_or_equal?',
'<>' => 'not_equal?',
'=' => 'excel_equal?',
'>' => 'more_than?',
'>=' => 'more_than_or_equal?',
'ABS' => 'abs',
'AND' => 'excel_and',
'AVERAGE' => 'average',
'CHOOSE' => 'choose',
'COSH' => 'cosh',
'COUNT' => 'count',
'COUNTA' => 'counta',
'FIND' => 'find',
'IF' => 'excel_if',
'IFERROR' => 'iferror',
'INDEX' => 'index',
'LEFT' => 'left',
'MATCH' => 'excel_match',
'MAX' => 'max',
'MIN' => 'min',
'MOD' => 'mod',
<<<<<<< HEAD
<<<<<<< HEAD
'MONTH' => 'month',
'OR' => 'excel_or',
=======
>>>>>>> parent of d46088e... Implemented EXP, INT, and MONTH functions
=======
>>>>>>> parent of d46088e... Implemented EXP, INT, and MONTH functions
'PI' => 'pi',
'PMT' => 'pmt',
'ROUND' => 'round',
'ROUNDDOWN' => 'rounddown',
'ROUNDUP' => 'roundup',
'SUBTOTAL' => 'subtotal',
'SUM' => 'sum',
'SUMIF' => 'sumif',
'SUMIFS' => 'sumifs',
'SUMPRODUCT' => 'sumproduct',
'TODAY' => 'today',
'VLOOKUP' => 'vlookup',
'^' => 'power'
}
def prefix(symbol,ast)
return map(ast) if symbol == "+"
return "negative(#{map(ast)})"
end
def brackets(*contents)
"(#{contents.map { |a| map(a) }.join(',')})"
end
def arithmetic(left,operator,right)
"#{FUNCTIONS[operator.last]}(#{map(left)},#{map(right)})"
end
def string_join(*strings)
"string_join(#{strings.map {|a| map(a)}.join(',')})"
end
def comparison(left,operator,right)
"#{FUNCTIONS[operator.last]}(#{map(left)},#{map(right)})"
end
def function(function_name,*arguments)
if FUNCTIONS.has_key?(function_name)
"#{FUNCTIONS[function_name]}(#{arguments.map { |a| map(a) }.join(",")})"
else
raise NotSupportedException.new("Function #{function_name} not supported")
end
end
def cell(reference)
reference.downcase.gsub('$','')
end
def sheet_reference(sheet,reference)
"#{sheet_names[sheet]}_#{map(reference)}"
end
def array(*rows)
"[#{rows.map {|r| map(r)}.join(",")}]"
end
def row(*cells)
"[#{cells.map {|r| map(r)}.join(",")}]"
end
end