xls_function 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (136) hide show
  1. checksums.yaml +7 -0
  2. data/.github/workflows/publish_gem.yml +25 -0
  3. data/.github/workflows/rspec.yml +30 -0
  4. data/.gitignore +13 -0
  5. data/.rspec +2 -0
  6. data/.rubocop.yml +41 -0
  7. data/CODE_OF_CONDUCT.md +74 -0
  8. data/Gemfile +8 -0
  9. data/LICENSE +201 -0
  10. data/README.md +48 -0
  11. data/Rakefile +6 -0
  12. data/bin/console +14 -0
  13. data/bin/setup +8 -0
  14. data/lib/xls_function/class_dictionary.rb +7 -0
  15. data/lib/xls_function/converter.rb +24 -0
  16. data/lib/xls_function/converters/date_converter.rb +30 -0
  17. data/lib/xls_function/converters/date_serial_converter.rb +17 -0
  18. data/lib/xls_function/converters/number_converter.rb +28 -0
  19. data/lib/xls_function/converters/time_converter.rb +158 -0
  20. data/lib/xls_function/converters/time_serial_converter.rb +97 -0
  21. data/lib/xls_function/default_logger.rb +7 -0
  22. data/lib/xls_function/error.rb +49 -0
  23. data/lib/xls_function/evaluators/arguments_definable.rb +39 -0
  24. data/lib/xls_function/evaluators/binary_operation_evaluator.rb +20 -0
  25. data/lib/xls_function/evaluators/binary_operations/add.rb +13 -0
  26. data/lib/xls_function/evaluators/binary_operations/concat.rb +13 -0
  27. data/lib/xls_function/evaluators/binary_operations/divide.rb +13 -0
  28. data/lib/xls_function/evaluators/binary_operations/equal.rb +13 -0
  29. data/lib/xls_function/evaluators/binary_operations/greater_than.rb +13 -0
  30. data/lib/xls_function/evaluators/binary_operations/greater_than_or_equal_to.rb +13 -0
  31. data/lib/xls_function/evaluators/binary_operations/less_than.rb +13 -0
  32. data/lib/xls_function/evaluators/binary_operations/less_than_or_equal_to.rb +13 -0
  33. data/lib/xls_function/evaluators/binary_operations/multiple.rb +13 -0
  34. data/lib/xls_function/evaluators/binary_operations/not_equal.rb +13 -0
  35. data/lib/xls_function/evaluators/binary_operations/power.rb +13 -0
  36. data/lib/xls_function/evaluators/binary_operations/subtract.rb +13 -0
  37. data/lib/xls_function/evaluators/class_dictionary.rb +30 -0
  38. data/lib/xls_function/evaluators/error_detector.rb +29 -0
  39. data/lib/xls_function/evaluators/evaluable.rb +37 -0
  40. data/lib/xls_function/evaluators/false_evaluator.rb +19 -0
  41. data/lib/xls_function/evaluators/function_evaluator.rb +133 -0
  42. data/lib/xls_function/evaluators/functions/and.rb +21 -0
  43. data/lib/xls_function/evaluators/functions/asc.rb +17 -0
  44. data/lib/xls_function/evaluators/functions/char.rb +19 -0
  45. data/lib/xls_function/evaluators/functions/clean.rb +22 -0
  46. data/lib/xls_function/evaluators/functions/code.rb +18 -0
  47. data/lib/xls_function/evaluators/functions/concat.rb +27 -0
  48. data/lib/xls_function/evaluators/functions/date.rb +57 -0
  49. data/lib/xls_function/evaluators/functions/date_value.rb +17 -0
  50. data/lib/xls_function/evaluators/functions/day.rb +15 -0
  51. data/lib/xls_function/evaluators/functions/dbcs.rb +18 -0
  52. data/lib/xls_function/evaluators/functions/e_date.rb +19 -0
  53. data/lib/xls_function/evaluators/functions/e_o_month.rb +20 -0
  54. data/lib/xls_function/evaluators/functions/exact.rb +16 -0
  55. data/lib/xls_function/evaluators/functions/find.rb +28 -0
  56. data/lib/xls_function/evaluators/functions/fixed.rb +46 -0
  57. data/lib/xls_function/evaluators/functions/hour.rb +15 -0
  58. data/lib/xls_function/evaluators/functions/if.rb +24 -0
  59. data/lib/xls_function/evaluators/functions/ifs.rb +20 -0
  60. data/lib/xls_function/evaluators/functions/int.rb +15 -0
  61. data/lib/xls_function/evaluators/functions/iserror.rb +20 -0
  62. data/lib/xls_function/evaluators/functions/isnumber.rb +15 -0
  63. data/lib/xls_function/evaluators/functions/lambda.rb +57 -0
  64. data/lib/xls_function/evaluators/functions/left.rb +16 -0
  65. data/lib/xls_function/evaluators/functions/len.rb +15 -0
  66. data/lib/xls_function/evaluators/functions/let.rb +24 -0
  67. data/lib/xls_function/evaluators/functions/lower.rb +15 -0
  68. data/lib/xls_function/evaluators/functions/mid.rb +23 -0
  69. data/lib/xls_function/evaluators/functions/minute.rb +15 -0
  70. data/lib/xls_function/evaluators/functions/month.rb +15 -0
  71. data/lib/xls_function/evaluators/functions/not.rb +15 -0
  72. data/lib/xls_function/evaluators/functions/now.rb +19 -0
  73. data/lib/xls_function/evaluators/functions/or.rb +21 -0
  74. data/lib/xls_function/evaluators/functions/power.rb +16 -0
  75. data/lib/xls_function/evaluators/functions/proper.rb +17 -0
  76. data/lib/xls_function/evaluators/functions/replace.rb +19 -0
  77. data/lib/xls_function/evaluators/functions/rept.rb +25 -0
  78. data/lib/xls_function/evaluators/functions/right.rb +16 -0
  79. data/lib/xls_function/evaluators/functions/round.rb +16 -0
  80. data/lib/xls_function/evaluators/functions/round_down.rb +16 -0
  81. data/lib/xls_function/evaluators/functions/round_up.rb +16 -0
  82. data/lib/xls_function/evaluators/functions/second.rb +15 -0
  83. data/lib/xls_function/evaluators/functions/sqrt.rb +17 -0
  84. data/lib/xls_function/evaluators/functions/substitute.rb +44 -0
  85. data/lib/xls_function/evaluators/functions/text.rb +17 -0
  86. data/lib/xls_function/evaluators/functions/time.rb +54 -0
  87. data/lib/xls_function/evaluators/functions/timevalue.rb +17 -0
  88. data/lib/xls_function/evaluators/functions/today.rb +19 -0
  89. data/lib/xls_function/evaluators/functions/trim.rb +16 -0
  90. data/lib/xls_function/evaluators/functions/trunc.rb +16 -0
  91. data/lib/xls_function/evaluators/functions/unichar.rb +19 -0
  92. data/lib/xls_function/evaluators/functions/unicode.rb +18 -0
  93. data/lib/xls_function/evaluators/functions/upper.rb +15 -0
  94. data/lib/xls_function/evaluators/functions/value.rb +26 -0
  95. data/lib/xls_function/evaluators/functions/year.rb +15 -0
  96. data/lib/xls_function/evaluators/number_evaluator.rb +18 -0
  97. data/lib/xls_function/evaluators/string_evaluator.rb +18 -0
  98. data/lib/xls_function/evaluators/true_evaluator.rb +19 -0
  99. data/lib/xls_function/evaluators/variant_evaluator.rb +24 -0
  100. data/lib/xls_function/extensions/array_extension.rb +22 -0
  101. data/lib/xls_function/extensions/big_decimal_extension.rb +15 -0
  102. data/lib/xls_function/extensions/date_extension.rb +11 -0
  103. data/lib/xls_function/extensions/hash_extension.rb +24 -0
  104. data/lib/xls_function/extensions/time_extension.rb +11 -0
  105. data/lib/xls_function/format_string/evaluators/elapsed_time_evaluator.rb +42 -0
  106. data/lib/xls_function/format_string/evaluators/number_evaluator.rb +243 -0
  107. data/lib/xls_function/format_string/evaluators/time_evaluator.rb +103 -0
  108. data/lib/xls_function/format_string/parse_rules/dates.rb +53 -0
  109. data/lib/xls_function/format_string/parse_rules/numbers.rb +31 -0
  110. data/lib/xls_function/format_string/parse_rules/texts.rb +25 -0
  111. data/lib/xls_function/format_string/parse_rules/times.rb +55 -0
  112. data/lib/xls_function/format_string/parser.rb +27 -0
  113. data/lib/xls_function/format_string/transform.rb +87 -0
  114. data/lib/xls_function/format_string/transform_rules/dates.rb +98 -0
  115. data/lib/xls_function/format_string/transform_rules/numbers.rb +59 -0
  116. data/lib/xls_function/format_string/transform_rules/texts.rb +25 -0
  117. data/lib/xls_function/format_string/transform_rules/times.rb +97 -0
  118. data/lib/xls_function/format_string.rb +24 -0
  119. data/lib/xls_function/i18n.rb +4 -0
  120. data/lib/xls_function/locales/en.yml +65 -0
  121. data/lib/xls_function/locales/ja.yml +65 -0
  122. data/lib/xls_function/parse_rules/binary_operation.rb +28 -0
  123. data/lib/xls_function/parse_rules/common.rb +34 -0
  124. data/lib/xls_function/parser.rb +12 -0
  125. data/lib/xls_function/transform.rb +58 -0
  126. data/lib/xls_function/transform_rules/binary_operation_transform.rb +16 -0
  127. data/lib/xls_function/transform_rules/boolean_transform.rb +12 -0
  128. data/lib/xls_function/transform_rules/function_call_transform.rb +16 -0
  129. data/lib/xls_function/transform_rules/number_transform.rb +11 -0
  130. data/lib/xls_function/transform_rules/string_transform.rb +17 -0
  131. data/lib/xls_function/transform_rules/variant_transform.rb +13 -0
  132. data/lib/xls_function/user_defined_function_factory.rb +59 -0
  133. data/lib/xls_function/version.rb +3 -0
  134. data/lib/xls_function.rb +82 -0
  135. data/xls_function.gemspec +30 -0
  136. metadata +221 -0
@@ -0,0 +1,16 @@
1
+ module XlsFunction
2
+ module Evaluators
3
+ module Functions
4
+ class Left < ::XlsFunction::Evaluators::FunctionEvaluator
5
+ function_as :left
6
+
7
+ define_arg :source
8
+ define_arg :length
9
+
10
+ def eval
11
+ source[0, length]
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,15 @@
1
+ module XlsFunction
2
+ module Evaluators
3
+ module Functions
4
+ class Len < ::XlsFunction::Evaluators::FunctionEvaluator
5
+ function_as :len
6
+
7
+ define_arg :source
8
+
9
+ def eval
10
+ source.length
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,24 @@
1
+ module XlsFunction
2
+ module Evaluators
3
+ module Functions
4
+ class Let < ::XlsFunction::Evaluators::FunctionEvaluator
5
+ function_as :let
6
+
7
+ def eval_arglist
8
+ # Skip common argument evaluation and assignment
9
+ end
10
+
11
+ def eval
12
+ # last arg is calculation
13
+ calculation = arg_list.pop
14
+ # set evaluated result to variant context
15
+ arg_list.each_slice(2) do |variant, expr|
16
+ variant_context[variant.name] = expr&.evaluate(context)
17
+ end
18
+
19
+ calculation.evaluate(context)
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,15 @@
1
+ module XlsFunction
2
+ module Evaluators
3
+ module Functions
4
+ class Lower < ::XlsFunction::Evaluators::FunctionEvaluator
5
+ function_as :lower
6
+
7
+ define_arg :source
8
+
9
+ def eval
10
+ source.downcase
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,23 @@
1
+ module XlsFunction
2
+ module Evaluators
3
+ module Functions
4
+ class Mid < ::XlsFunction::Evaluators::FunctionEvaluator
5
+ function_as :mid
6
+
7
+ define_arg :source
8
+ define_arg :start
9
+ define_arg :length
10
+
11
+ def eval
12
+ source[safe_start, length]
13
+ end
14
+
15
+ def safe_start
16
+ return 0 unless start.positive?
17
+
18
+ start - 1
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,15 @@
1
+ module XlsFunction
2
+ module Evaluators
3
+ module Functions
4
+ class Minute < ::XlsFunction::Evaluators::FunctionEvaluator
5
+ function_as :minute
6
+
7
+ define_arg :time_value, type: :time
8
+
9
+ def eval
10
+ time_value.min
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ module XlsFunction
2
+ module Evaluators
3
+ module Functions
4
+ class Month < ::XlsFunction::Evaluators::FunctionEvaluator
5
+ function_as :month
6
+
7
+ define_arg :date_value, type: :date
8
+
9
+ def eval
10
+ date_value.month
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ module XlsFunction
2
+ module Evaluators
3
+ module Functions
4
+ class Not < ::XlsFunction::Evaluators::FunctionEvaluator
5
+ function_as :not
6
+
7
+ define_arg :logical
8
+
9
+ def eval
10
+ !logical
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,19 @@
1
+ module XlsFunction
2
+ module Evaluators
3
+ module Functions
4
+ class Now < ::XlsFunction::Evaluators::FunctionEvaluator
5
+ function_as :now
6
+
7
+ using ::XlsFunction::Extensions::TimeExtension
8
+
9
+ def eval
10
+ now.to_serial
11
+ end
12
+
13
+ def now
14
+ Time.now
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,21 @@
1
+ module XlsFunction
2
+ module Evaluators
3
+ module Functions
4
+ class Or < ::XlsFunction::Evaluators::FunctionEvaluator
5
+ function_as :or
6
+
7
+ def eval_arglist
8
+ # Skip common argument evaluation and assignment for short-circuit
9
+ end
10
+
11
+ def eval
12
+ arg_list.each do |arg|
13
+ return true if arg.evaluate(context)
14
+ end
15
+
16
+ false
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,16 @@
1
+ module XlsFunction
2
+ module Evaluators
3
+ module Functions
4
+ class POWER < ::XlsFunction::Evaluators::FunctionEvaluator
5
+ function_as :power
6
+
7
+ define_arg :number, type: :number
8
+ define_arg :power, type: :number
9
+
10
+ def eval
11
+ number**power
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,17 @@
1
+ module XlsFunction
2
+ module Evaluators
3
+ module Functions
4
+ class Proper < ::XlsFunction::Evaluators::FunctionEvaluator
5
+ function_as :proper
6
+
7
+ define_arg :source
8
+
9
+ def eval
10
+ source.split(/([[:digit:][:blank:][:punct:]]+)/)
11
+ .map(&:capitalize)
12
+ .join
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,19 @@
1
+ module XlsFunction
2
+ module Evaluators
3
+ module Functions
4
+ class Replace < ::XlsFunction::Evaluators::FunctionEvaluator
5
+ function_as :replace
6
+
7
+ define_arg :source
8
+ define_arg :start_index
9
+ define_arg :length
10
+ define_arg :replace_string
11
+
12
+ def eval
13
+ source[start_index - 1, length] = replace_string
14
+ source
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,25 @@
1
+ module XlsFunction
2
+ module Evaluators
3
+ module Functions
4
+ class Rept < ::XlsFunction::Evaluators::FunctionEvaluator
5
+ function_as :rept
6
+
7
+ define_arg :source
8
+ define_arg :number_times, type: :number
9
+
10
+ def eval
11
+ number_times_i = number_times.to_i
12
+ return ::XlsFunction::ErrorValue.value!(number_times_is_negative) if number_times_i.negative?
13
+ return '' if number_times_i.zero?
14
+
15
+ source * number_times_i
16
+ end
17
+
18
+ def number_times_is_negative
19
+ message = error_message(:number_is_negative, label: 'number_times', number: number_times)
20
+ class_info(message)
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,16 @@
1
+ module XlsFunction
2
+ module Evaluators
3
+ module Functions
4
+ class Right < ::XlsFunction::Evaluators::FunctionEvaluator
5
+ function_as :right
6
+
7
+ define_arg :source
8
+ define_arg :length
9
+
10
+ def eval
11
+ source[-length..-1]
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,16 @@
1
+ module XlsFunction
2
+ module Evaluators
3
+ module Functions
4
+ class Round < ::XlsFunction::Evaluators::FunctionEvaluator
5
+ function_as :round
6
+
7
+ define_arg :number, type: :number
8
+ define_arg :num_digits, type: :number
9
+
10
+ def eval
11
+ number.round(num_digits.to_i).to_d
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,16 @@
1
+ module XlsFunction
2
+ module Evaluators
3
+ module Functions
4
+ class RoundDown < ::XlsFunction::Evaluators::FunctionEvaluator
5
+ function_as :rounddown
6
+
7
+ define_arg :number, type: :number
8
+ define_arg :num_digits, type: :number
9
+
10
+ def eval
11
+ number.truncate(num_digits.to_i).to_d
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,16 @@
1
+ module XlsFunction
2
+ module Evaluators
3
+ module Functions
4
+ class RoundUp < ::XlsFunction::Evaluators::FunctionEvaluator
5
+ function_as :roundup
6
+
7
+ define_arg :number, type: :number
8
+ define_arg :num_digits, type: :number
9
+
10
+ def eval
11
+ number.round(num_digits.to_i, BigDecimal::ROUND_UP).to_d
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,15 @@
1
+ module XlsFunction
2
+ module Evaluators
3
+ module Functions
4
+ class Second < ::XlsFunction::Evaluators::FunctionEvaluator
5
+ function_as :second
6
+
7
+ define_arg :time_value, type: :time
8
+
9
+ def eval
10
+ time_value.sec
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,17 @@
1
+ module XlsFunction
2
+ module Evaluators
3
+ module Functions
4
+ class Sqrt < ::XlsFunction::Evaluators::FunctionEvaluator
5
+ function_as :sqrt
6
+
7
+ define_arg :number, type: :number
8
+
9
+ def eval
10
+ return ErrorValue.num!(error_message(:number_is_negative, label: 'number', number: number)) if number.negative?
11
+
12
+ Math.sqrt(number).to_d
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,44 @@
1
+ module XlsFunction
2
+ module Evaluators
3
+ module Functions
4
+ class Substitute < ::XlsFunction::Evaluators::FunctionEvaluator
5
+ function_as :substitute
6
+
7
+ define_arg :source
8
+ define_arg :search_for
9
+ define_arg :replace_with
10
+ define_arg :instance_num, default: 0
11
+
12
+ def eval
13
+ instance_num_i = instance_num.to_i
14
+ return ::XlsFunction::ErrorValue.value!(instance_num_is_negative(instance_num_i)) if instance_num_i.negative?
15
+
16
+ if replace_all?(instance_num_i)
17
+ replace_all(source, search_for, replace_with)
18
+ else
19
+ replace(source, search_for, replace_with, instance_num_i)
20
+ end
21
+ end
22
+
23
+ private
24
+
25
+ def replace_all?(instance_num_i)
26
+ instance_num_i.zero?
27
+ end
28
+
29
+ def replace_all(source, search_for, replace_with)
30
+ source.gsub(search_for, replace_with)
31
+ end
32
+
33
+ def replace(source, search_for, replace_with, instance_num_i)
34
+ source.gsub(search_for).with_index(1) { |s, i| i == instance_num_i ? replace_with : s }
35
+ end
36
+
37
+ def instance_num_is_negative(instance_num_i)
38
+ message = error_message(:number_is_negative, label: 'instance_num', number: instance_num_i)
39
+ class_info(message)
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,17 @@
1
+ module XlsFunction
2
+ module Evaluators
3
+ module Functions
4
+ class Text < ::XlsFunction::Evaluators::FunctionEvaluator
5
+ function_as :text
6
+
7
+ define_arg :value
8
+ define_arg :format_text
9
+
10
+ def eval
11
+ converter = ::XlsFunction::FormatString.evaluate_converter(format_text, context)
12
+ converter.call(value)
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,54 @@
1
+ module XlsFunction
2
+ module Evaluators
3
+ module Functions
4
+ class TimeFn < ::XlsFunction::Evaluators::FunctionEvaluator
5
+ function_as :time
6
+
7
+ define_arg :hour, default: 0, type: :number
8
+ define_arg :minute, default: 0, type: :number
9
+ define_arg :second, default: 0, type: :number
10
+
11
+ ARG_LIMIT = 32_767
12
+
13
+ using ::XlsFunction::Extensions::TimeExtension
14
+
15
+ def eval
16
+ return ::XlsFunction::ErrorValue.num!(out_of_range) unless validate_args
17
+
18
+ adjusted = ::XlsFunction::Converters::TimeConverter.adjust_elapsed_time(hour_i, minute_i, second_i)
19
+ time = Time.new(Time.now.year, nil, nil, adjusted[:hour], adjusted[:minute], adjusted[:second])
20
+
21
+ time.to_serial(except_date: true)
22
+ end
23
+
24
+ def validate_args
25
+ return false if hour_i > ARG_LIMIT || minute_i > ARG_LIMIT || second_i > ARG_LIMIT
26
+
27
+ # raise error when negative converted to seconds
28
+ hour_sec = hour_i * 60 * 60
29
+ minute_sec = minute_i * 60
30
+ return false if (hour_sec + minute_sec + second_i).negative?
31
+
32
+ true
33
+ end
34
+
35
+ def hour_i
36
+ @hour_i ||= hour.to_i
37
+ end
38
+
39
+ def minute_i
40
+ @minute_i ||= minute.to_i
41
+ end
42
+
43
+ def second_i
44
+ @second_i ||= second.to_i
45
+ end
46
+
47
+ def out_of_range
48
+ message = error_message(:out_of_range, value: "#{hour}, #{minute}, #{second}")
49
+ class_info(message)
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,17 @@
1
+ module XlsFunction
2
+ module Evaluators
3
+ module Functions
4
+ class Timevalue < ::XlsFunction::Evaluators::FunctionEvaluator
5
+ function_as :timevalue
6
+
7
+ define_arg :time_value, type: :time
8
+
9
+ using ::XlsFunction::Extensions::TimeExtension
10
+
11
+ def eval
12
+ time_value.to_serial(except_date: true)
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,19 @@
1
+ module XlsFunction
2
+ module Evaluators
3
+ module Functions
4
+ class Today < ::XlsFunction::Evaluators::FunctionEvaluator
5
+ function_as :today
6
+
7
+ using ::XlsFunction::Extensions::DateExtension
8
+
9
+ def eval
10
+ today.to_serial
11
+ end
12
+
13
+ def today
14
+ Date.today
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,16 @@
1
+ module XlsFunction
2
+ module Evaluators
3
+ module Functions
4
+ class Trim < ::XlsFunction::Evaluators::FunctionEvaluator
5
+ function_as :trim
6
+
7
+ define_arg :source
8
+
9
+ def eval
10
+ trimed = source.to_s.gsub(/(^[\s ]+)|([\s ]+$)/, '')
11
+ trimed.gsub(/[\s ]+/) { |spaces| spaces[0] }
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,16 @@
1
+ module XlsFunction
2
+ module Evaluators
3
+ module Functions
4
+ class Trunc < ::XlsFunction::Evaluators::FunctionEvaluator
5
+ function_as :trunc
6
+
7
+ define_arg :number, type: :number
8
+ define_arg :num_digits, type: :number, default: 0
9
+
10
+ def eval
11
+ number.truncate(num_digits.to_i).to_d
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,19 @@
1
+ module XlsFunction
2
+ module Evaluators
3
+ module Functions
4
+ class Unichar < ::XlsFunction::Evaluators::FunctionEvaluator
5
+ function_as :unichar
6
+
7
+ define_arg :number
8
+
9
+ def eval
10
+ return '' unless number.respond_to?(:to_i)
11
+
12
+ number.to_i.chr(Encoding::UTF_8)
13
+ rescue RangeError => e
14
+ e.message
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,18 @@
1
+ module XlsFunction
2
+ module Evaluators
3
+ module Functions
4
+ class Unicode < ::XlsFunction::Evaluators::FunctionEvaluator
5
+ function_as :unicode
6
+
7
+ define_arg :source
8
+
9
+ def eval
10
+ s = source.to_s
11
+ return if s.empty?
12
+
13
+ s.encode(Encoding::UTF_8).ord
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,15 @@
1
+ module XlsFunction
2
+ module Evaluators
3
+ module Functions
4
+ class Upper < ::XlsFunction::Evaluators::FunctionEvaluator
5
+ function_as :upper
6
+
7
+ define_arg :source
8
+
9
+ def eval
10
+ source.upcase
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,26 @@
1
+ module XlsFunction
2
+ module Evaluators
3
+ module Functions
4
+ class Value < ::XlsFunction::Evaluators::FunctionEvaluator
5
+ function_as :value
6
+
7
+ define_arg :source
8
+
9
+ def eval
10
+ # Delete comma because ruby converts '1,000' to 1.
11
+ return BigDecimal(source.delete(',')) if source.is_a?(String)
12
+ return source.to_d if source.respond_to?(:to_d)
13
+
14
+ ::XlsFunction::ErrorValue.value!(cannot_convert)
15
+ rescue TypeError, ArgumentError
16
+ ::XlsFunction::ErrorValue.value!(cannot_convert)
17
+ end
18
+
19
+ def cannot_convert
20
+ message = error_message(:cannot_convert_to_number, source: source)
21
+ class_info(message)
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,15 @@
1
+ module XlsFunction
2
+ module Evaluators
3
+ module Functions
4
+ class Year < ::XlsFunction::Evaluators::FunctionEvaluator
5
+ function_as :year
6
+
7
+ define_arg :date_value, type: :date
8
+
9
+ def eval
10
+ date_value.year
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end