xls_function 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.github/workflows/publish_gem.yml +25 -0
- data/.github/workflows/rspec.yml +30 -0
- data/.gitignore +13 -0
- data/.rspec +2 -0
- data/.rubocop.yml +41 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +8 -0
- data/LICENSE +201 -0
- data/README.md +48 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/xls_function/class_dictionary.rb +7 -0
- data/lib/xls_function/converter.rb +24 -0
- data/lib/xls_function/converters/date_converter.rb +30 -0
- data/lib/xls_function/converters/date_serial_converter.rb +17 -0
- data/lib/xls_function/converters/number_converter.rb +28 -0
- data/lib/xls_function/converters/time_converter.rb +158 -0
- data/lib/xls_function/converters/time_serial_converter.rb +97 -0
- data/lib/xls_function/default_logger.rb +7 -0
- data/lib/xls_function/error.rb +49 -0
- data/lib/xls_function/evaluators/arguments_definable.rb +39 -0
- data/lib/xls_function/evaluators/binary_operation_evaluator.rb +20 -0
- data/lib/xls_function/evaluators/binary_operations/add.rb +13 -0
- data/lib/xls_function/evaluators/binary_operations/concat.rb +13 -0
- data/lib/xls_function/evaluators/binary_operations/divide.rb +13 -0
- data/lib/xls_function/evaluators/binary_operations/equal.rb +13 -0
- data/lib/xls_function/evaluators/binary_operations/greater_than.rb +13 -0
- data/lib/xls_function/evaluators/binary_operations/greater_than_or_equal_to.rb +13 -0
- data/lib/xls_function/evaluators/binary_operations/less_than.rb +13 -0
- data/lib/xls_function/evaluators/binary_operations/less_than_or_equal_to.rb +13 -0
- data/lib/xls_function/evaluators/binary_operations/multiple.rb +13 -0
- data/lib/xls_function/evaluators/binary_operations/not_equal.rb +13 -0
- data/lib/xls_function/evaluators/binary_operations/power.rb +13 -0
- data/lib/xls_function/evaluators/binary_operations/subtract.rb +13 -0
- data/lib/xls_function/evaluators/class_dictionary.rb +30 -0
- data/lib/xls_function/evaluators/error_detector.rb +29 -0
- data/lib/xls_function/evaluators/evaluable.rb +37 -0
- data/lib/xls_function/evaluators/false_evaluator.rb +19 -0
- data/lib/xls_function/evaluators/function_evaluator.rb +133 -0
- data/lib/xls_function/evaluators/functions/and.rb +21 -0
- data/lib/xls_function/evaluators/functions/asc.rb +17 -0
- data/lib/xls_function/evaluators/functions/char.rb +19 -0
- data/lib/xls_function/evaluators/functions/clean.rb +22 -0
- data/lib/xls_function/evaluators/functions/code.rb +18 -0
- data/lib/xls_function/evaluators/functions/concat.rb +27 -0
- data/lib/xls_function/evaluators/functions/date.rb +57 -0
- data/lib/xls_function/evaluators/functions/date_value.rb +17 -0
- data/lib/xls_function/evaluators/functions/day.rb +15 -0
- data/lib/xls_function/evaluators/functions/dbcs.rb +18 -0
- data/lib/xls_function/evaluators/functions/e_date.rb +19 -0
- data/lib/xls_function/evaluators/functions/e_o_month.rb +20 -0
- data/lib/xls_function/evaluators/functions/exact.rb +16 -0
- data/lib/xls_function/evaluators/functions/find.rb +28 -0
- data/lib/xls_function/evaluators/functions/fixed.rb +46 -0
- data/lib/xls_function/evaluators/functions/hour.rb +15 -0
- data/lib/xls_function/evaluators/functions/if.rb +24 -0
- data/lib/xls_function/evaluators/functions/ifs.rb +20 -0
- data/lib/xls_function/evaluators/functions/int.rb +15 -0
- data/lib/xls_function/evaluators/functions/iserror.rb +20 -0
- data/lib/xls_function/evaluators/functions/isnumber.rb +15 -0
- data/lib/xls_function/evaluators/functions/lambda.rb +57 -0
- data/lib/xls_function/evaluators/functions/left.rb +16 -0
- data/lib/xls_function/evaluators/functions/len.rb +15 -0
- data/lib/xls_function/evaluators/functions/let.rb +24 -0
- data/lib/xls_function/evaluators/functions/lower.rb +15 -0
- data/lib/xls_function/evaluators/functions/mid.rb +23 -0
- data/lib/xls_function/evaluators/functions/minute.rb +15 -0
- data/lib/xls_function/evaluators/functions/month.rb +15 -0
- data/lib/xls_function/evaluators/functions/not.rb +15 -0
- data/lib/xls_function/evaluators/functions/now.rb +19 -0
- data/lib/xls_function/evaluators/functions/or.rb +21 -0
- data/lib/xls_function/evaluators/functions/power.rb +16 -0
- data/lib/xls_function/evaluators/functions/proper.rb +17 -0
- data/lib/xls_function/evaluators/functions/replace.rb +19 -0
- data/lib/xls_function/evaluators/functions/rept.rb +25 -0
- data/lib/xls_function/evaluators/functions/right.rb +16 -0
- data/lib/xls_function/evaluators/functions/round.rb +16 -0
- data/lib/xls_function/evaluators/functions/round_down.rb +16 -0
- data/lib/xls_function/evaluators/functions/round_up.rb +16 -0
- data/lib/xls_function/evaluators/functions/second.rb +15 -0
- data/lib/xls_function/evaluators/functions/sqrt.rb +17 -0
- data/lib/xls_function/evaluators/functions/substitute.rb +44 -0
- data/lib/xls_function/evaluators/functions/text.rb +17 -0
- data/lib/xls_function/evaluators/functions/time.rb +54 -0
- data/lib/xls_function/evaluators/functions/timevalue.rb +17 -0
- data/lib/xls_function/evaluators/functions/today.rb +19 -0
- data/lib/xls_function/evaluators/functions/trim.rb +16 -0
- data/lib/xls_function/evaluators/functions/trunc.rb +16 -0
- data/lib/xls_function/evaluators/functions/unichar.rb +19 -0
- data/lib/xls_function/evaluators/functions/unicode.rb +18 -0
- data/lib/xls_function/evaluators/functions/upper.rb +15 -0
- data/lib/xls_function/evaluators/functions/value.rb +26 -0
- data/lib/xls_function/evaluators/functions/year.rb +15 -0
- data/lib/xls_function/evaluators/number_evaluator.rb +18 -0
- data/lib/xls_function/evaluators/string_evaluator.rb +18 -0
- data/lib/xls_function/evaluators/true_evaluator.rb +19 -0
- data/lib/xls_function/evaluators/variant_evaluator.rb +24 -0
- data/lib/xls_function/extensions/array_extension.rb +22 -0
- data/lib/xls_function/extensions/big_decimal_extension.rb +15 -0
- data/lib/xls_function/extensions/date_extension.rb +11 -0
- data/lib/xls_function/extensions/hash_extension.rb +24 -0
- data/lib/xls_function/extensions/time_extension.rb +11 -0
- data/lib/xls_function/format_string/evaluators/elapsed_time_evaluator.rb +42 -0
- data/lib/xls_function/format_string/evaluators/number_evaluator.rb +243 -0
- data/lib/xls_function/format_string/evaluators/time_evaluator.rb +103 -0
- data/lib/xls_function/format_string/parse_rules/dates.rb +53 -0
- data/lib/xls_function/format_string/parse_rules/numbers.rb +31 -0
- data/lib/xls_function/format_string/parse_rules/texts.rb +25 -0
- data/lib/xls_function/format_string/parse_rules/times.rb +55 -0
- data/lib/xls_function/format_string/parser.rb +27 -0
- data/lib/xls_function/format_string/transform.rb +87 -0
- data/lib/xls_function/format_string/transform_rules/dates.rb +98 -0
- data/lib/xls_function/format_string/transform_rules/numbers.rb +59 -0
- data/lib/xls_function/format_string/transform_rules/texts.rb +25 -0
- data/lib/xls_function/format_string/transform_rules/times.rb +97 -0
- data/lib/xls_function/format_string.rb +24 -0
- data/lib/xls_function/i18n.rb +4 -0
- data/lib/xls_function/locales/en.yml +65 -0
- data/lib/xls_function/locales/ja.yml +65 -0
- data/lib/xls_function/parse_rules/binary_operation.rb +28 -0
- data/lib/xls_function/parse_rules/common.rb +34 -0
- data/lib/xls_function/parser.rb +12 -0
- data/lib/xls_function/transform.rb +58 -0
- data/lib/xls_function/transform_rules/binary_operation_transform.rb +16 -0
- data/lib/xls_function/transform_rules/boolean_transform.rb +12 -0
- data/lib/xls_function/transform_rules/function_call_transform.rb +16 -0
- data/lib/xls_function/transform_rules/number_transform.rb +11 -0
- data/lib/xls_function/transform_rules/string_transform.rb +17 -0
- data/lib/xls_function/transform_rules/variant_transform.rb +13 -0
- data/lib/xls_function/user_defined_function_factory.rb +59 -0
- data/lib/xls_function/version.rb +3 -0
- data/lib/xls_function.rb +82 -0
- data/xls_function.gemspec +30 -0
- 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,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,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,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,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,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
|