diff options
| author | Artem Kozaev <[email protected]> | 2020-11-27 18:51:01 +0300 |
|---|---|---|
| committer | Artem Kozaev <[email protected]> | 2020-11-27 18:51:01 +0300 |
| commit | 155840c2772c964f9793683db7a2de95b7bd9027 (patch) | |
| tree | a95814de902ffa78b008a024c7d46ac2ab5ec4d0 | |
| parent | 93ecd05c7ad587a60361b042ab9d692391e40e3b (diff) | |
| download | caxlsx-155840c2772c964f9793683db7a2de95b7bd9027.tar.gz caxlsx-155840c2772c964f9793683db7a2de95b7bd9027.zip | |
Add casting to date
Add tests
| -rw-r--r-- | lib/axlsx/workbook/worksheet/cell.rb | 6 | ||||
| -rw-r--r-- | test/util/tc_validators.rb | 2 | ||||
| -rw-r--r-- | test/workbook/worksheet/tc_cell.rb | 7 |
3 files changed, 13 insertions, 2 deletions
diff --git a/lib/axlsx/workbook/worksheet/cell.rb b/lib/axlsx/workbook/worksheet/cell.rb index a066ba91..9b87b894 100644 --- a/lib/axlsx/workbook/worksheet/cell.rb +++ b/lib/axlsx/workbook/worksheet/cell.rb @@ -475,7 +475,11 @@ module Axlsx case type when :date self.style = STYLE_DATE if self.style == 0 - v + if !v.is_a?(Date) && v.respond_to?(:to_date) + v.to_date + else + v + end when :time self.style = STYLE_DATE if self.style == 0 if !v.is_a?(Time) && v.respond_to?(:to_time) diff --git a/test/util/tc_validators.rb b/test/util/tc_validators.rb index 6effac51..d6936496 100644 --- a/test/util/tc_validators.rb +++ b/test/util/tc_validators.rb @@ -127,7 +127,7 @@ class TestValidators < Test::Unit::TestCase assert_raise(ArgumentError) { Axlsx.validate_data_validation_error_style 0 } #data_validation_type - [:custom, :data, :decimal, :list, :none, :textLength, :time, :whole].each do |sym| + [:custom, :data, :decimal, :list, :none, :textLength, :date, :time, :whole].each do |sym| assert_nothing_raised { Axlsx.validate_data_validation_type sym } end assert_raise(ArgumentError) { Axlsx.validate_data_validation_error_style :other_symbol } diff --git a/test/workbook/worksheet/tc_cell.rb b/test/workbook/worksheet/tc_cell.rb index d6f5aeae..7e21b8cb 100644 --- a/test/workbook/worksheet/tc_cell.rb +++ b/test/workbook/worksheet/tc_cell.rb @@ -71,6 +71,13 @@ class TestCell < Test::Unit::TestCase assert_equal(@c.value, now.to_time) end + def test_date + @c.type = :date + now = Time.now + @c.value = now + assert_equal(@c.value, now.to_date) + end + def test_style assert_raise(ArgumentError, "must reject invalid style indexes") { @[email protected] } assert_nothing_raised("must allow valid style index changes") {@c.style=1} |
