diff options
| author | Noel Peden <[email protected]> | 2021-01-05 20:24:48 -0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2021-01-05 20:24:48 -0800 |
| commit | c9e3211d85eb99bf9c82e24325c4facc6ba90635 (patch) | |
| tree | 248d70ed70a7ab5b56f945c611314114ec774e79 | |
| parent | f4d711d810620879ed2b73e3218741a351a357b8 (diff) | |
| parent | 155840c2772c964f9793683db7a2de95b7bd9027 (diff) | |
| download | caxlsx-c9e3211d85eb99bf9c82e24325c4facc6ba90635.tar.gz caxlsx-c9e3211d85eb99bf9c82e24325c4facc6ba90635.zip | |
Merge pull request #71 from artplan1/add-date-type
Allow to set cell type to `date`
| -rw-r--r-- | lib/axlsx/util/validators.rb | 2 | ||||
| -rw-r--r-- | lib/axlsx/workbook/worksheet/cell.rb | 6 | ||||
| -rw-r--r-- | lib/axlsx/workbook/worksheet/data_validation.rb | 8 | ||||
| -rw-r--r-- | test/util/tc_validators.rb | 2 | ||||
| -rw-r--r-- | test/workbook/worksheet/tc_cell.rb | 7 |
5 files changed, 18 insertions, 7 deletions
diff --git a/lib/axlsx/util/validators.rb b/lib/axlsx/util/validators.rb index 76ee2250..f1bf0ffc 100644 --- a/lib/axlsx/util/validators.rb +++ b/lib/axlsx/util/validators.rb @@ -269,7 +269,7 @@ module Axlsx # valid types must be one of custom, data, decimal, list, none, textLength, time, whole # @param [Any] v The value validated def self.validate_data_validation_type(v) - RestrictionValidator.validate :data_validation_type, [:custom, :data, :decimal, :list, :none, :textLength, :time, :whole], v + RestrictionValidator.validate :data_validation_type, [:custom, :data, :decimal, :list, :none, :textLength, :date, :time, :whole], v end # Requires that the value is a valid sheet view type. diff --git a/lib/axlsx/workbook/worksheet/cell.rb b/lib/axlsx/workbook/worksheet/cell.rb index a11bddfd..51a14494 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/lib/axlsx/workbook/worksheet/data_validation.rb b/lib/axlsx/workbook/worksheet/data_validation.rb index ab018695..1c5f5051 100644 --- a/lib/axlsx/workbook/worksheet/data_validation.rb +++ b/lib/axlsx/workbook/worksheet/data_validation.rb @@ -171,7 +171,7 @@ module Axlsx def formula1=(v); Axlsx::validate_string(v); @formula1 = v end # @see formula2 - def formula2=(v); Axlsx::validate_string(v); @formula2 = v end + def formula2=(v); Axlsx::validate_string(v); @formula2 = v end # @see allowBlank def allowBlank=(v); Axlsx::validate_boolean(v); @allowBlank = v end @@ -216,8 +216,8 @@ module Axlsx valid_attributes = get_valid_attributes str << '<dataValidation ' - str << instance_values.map do |key, value| - '' << key << '="' << Axlsx.booleanize(value).to_s << '"' if (valid_attributes.include?(key.to_sym) && !CHILD_ELEMENTS.include?(key.to_sym)) + str << instance_values.map do |key, value| + '' << key << '="' << Axlsx.booleanize(value).to_s << '"' if (valid_attributes.include?(key.to_sym) && !CHILD_ELEMENTS.include?(key.to_sym)) end.join(' ') str << '>' str << ('<formula1>' << self.formula1 << '</formula1>') if @formula1 and valid_attributes.include?(:formula1) @@ -229,7 +229,7 @@ module Axlsx def get_valid_attributes attributes = [:allowBlank, :error, :errorStyle, :errorTitle, :prompt, :promptTitle, :showErrorMessage, :showInputMessage, :sqref, :type ] - if [:whole, :decimal, :data, :time, :textLength].include?(@type) + if [:whole, :decimal, :data, :time, :date, :textLength].include?(@type) attributes << [:operator, :formula1] attributes << [:formula2] if [:between, :notBetween].include?(@operator) elsif @type == :list 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 3a7c1566..04f6bed3 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} |
