summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorRandy Morgan <[email protected]>2013-01-15 21:53:03 +0900
committerRandy Morgan <[email protected]>2013-01-15 21:53:03 +0900
commite14682fb484cd72ba5f479cc1f7d46a7a9fd6007 (patch)
treefd5a6efd273caebcb5ad4e6be66d0639e37cfcc7
parentbd099af6f8fe954eabc1db33c9ad3990629ddff1 (diff)
downloadcaxlsx-e14682fb484cd72ba5f479cc1f7d46a7a9fd6007.tar.gz
caxlsx-e14682fb484cd72ba5f479cc1f7d46a7a9fd6007.zip
Added support for iso 8601 data types.
Not really sure what excel is going to do with these data types, but hooking it up is the first step ;)
-rw-r--r--lib/axlsx/workbook/worksheet/cell.rb10
-rw-r--r--test/workbook/worksheet/tc_cell.rb6
2 files changed, 14 insertions, 2 deletions
diff --git a/lib/axlsx/workbook/worksheet/cell.rb b/lib/axlsx/workbook/worksheet/cell.rb
index 7617bb0f..abd60661 100644
--- a/lib/axlsx/workbook/worksheet/cell.rb
+++ b/lib/axlsx/workbook/worksheet/cell.rb
@@ -81,7 +81,7 @@ module Axlsx
attr_reader :type
# @see type
def type=(v)
- RestrictionValidator.validate "Cell.type", [:date, :time, :float, :integer, :string, :boolean], v
+ RestrictionValidator.validate "Cell.type", [:date, :time, :float, :integer, :string, :boolean, :iso_8601], v
@type=v
self.value = @value unless @value.nil?
end
@@ -373,6 +373,11 @@ module Axlsx
:integer
elsif v.to_s =~ /\A[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?\Z/ #float
:float
+ # \A(-?(?:[1-9][0-9]*)?[0-9]{4})-(1[0-2]|0[1-9])-(3[0-1]|0[1-9]|[1-2][0-9])
+ # T(2[0-3]|[0-1][0-9]):([0-5][0-9]):([0-5][0-9])(\.[0-9]+)?
+ # (Z|[+-](?:2[0-3]|[0-1][0-9]):[0-5][0-9])?\Z
+ elsif v.to_s =~/\A(-?(?:[1-9][0-9]*)?[0-9]{4})-(1[0-2]|0[1-9])-(3[0-1]|0[1-9]|[1-2][0-9])T(2[0-3]|[0-1][0-9]):([0-5][0-9]):([0-5][0-9])(\.[0-9]+)?(Z|[+-](?:2[0-3]|[0-1][0-9]):[0-5][0-9])?\Z/
+ :iso_8601
else
:string
end
@@ -396,6 +401,9 @@ module Axlsx
v.to_i
elsif @type == :boolean
v ? 1 : 0
+ elsif @type == :iso_8601
+ #consumer is responsible for ensuring the iso_8601 format when specifying this type
+ v
else
@type = :string
# TODO find a better way to do this as it accounts for 30% of
diff --git a/test/workbook/worksheet/tc_cell.rb b/test/workbook/worksheet/tc_cell.rb
index b237077c..208b15e0 100644
--- a/test/workbook/worksheet/tc_cell.rb
+++ b/test/workbook/worksheet/tc_cell.rb
@@ -89,6 +89,7 @@ class TestCell < Test::Unit::TestCase
assert_equal(@c.send(:cell_type_from_value, true), :boolean)
assert_equal(@c.send(:cell_type_from_value, false), :boolean)
assert_equal(@c.send(:cell_type_from_value, 1.0/10**6), :float)
+ assert_equal(:iso_8601, @c.send(:cell_type_from_value, '2008-08-30T01:45:36.123+09:00'))
end
def test_cast_value
@@ -105,6 +106,8 @@ class TestCell < Test::Unit::TestCase
@c.type = :boolean
assert_equal(@c.send(:cast_value, true), 1)
assert_equal(@c.send(:cast_value, false), 0)
+ @c.type = :iso_8601
+ assert_equal("2012-10-10T12:24", @c.send(:cast_value, "2012-10-10T12:24"))
end
def test_color
@@ -299,8 +302,9 @@ class TestCell < Test::Unit::TestCase
end
def test_to_xml
# TODO This could use some much more stringent testing related to the xml content generated!
- @ws.add_row [Time.now, Date.today, true, 1, 1.0, "text", "=sum(A1:A2)"]
+ @ws.add_row [Time.now, Date.today, true, 1, 1.0, "text", "=sum(A1:A2)", "2013-01-13T13:31:25.123"]
@ws.rows.last.cells[5].u = true
+
schema = Nokogiri::XML::Schema(File.open(Axlsx::SML_XSD))
doc = Nokogiri::XML(@ws.to_xml_string)
errors = []