summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJonathan Tron <[email protected]>2012-02-22 14:41:50 +0100
committerJonathan Tron <[email protected]>2012-02-22 14:41:50 +0100
commitac16190cdd4b0d20ec25f3d92b9e842b1ea51348 (patch)
tree43fbdb8234e751093c16098f304a5b3ddd9ec924
parentc22fbb916b01495f90aa5430c00dc063f1690f98 (diff)
downloadcaxlsx-ac16190cdd4b0d20ec25f3d92b9e842b1ea51348.tar.gz
caxlsx-ac16190cdd4b0d20ec25f3d92b9e842b1ea51348.zip
Add :boolean as a valid type for cells
-rw-r--r--lib/axlsx/workbook/worksheet/cell.rb17
-rw-r--r--test/workbook/worksheet/tc_cell.rb8
2 files changed, 17 insertions, 8 deletions
diff --git a/lib/axlsx/workbook/worksheet/cell.rb b/lib/axlsx/workbook/worksheet/cell.rb
index 5e7f594f..8b6bf574 100644
--- a/lib/axlsx/workbook/worksheet/cell.rb
+++ b/lib/axlsx/workbook/worksheet/cell.rb
@@ -40,21 +40,21 @@ module Axlsx
# @return [Row]
attr_reader :row
- # The cell's data type. Currently only four types are supported, :time, :float, :integer and :string.
+ # The cell's data type. Currently only five types are supported, :time, :float, :integer, :string and :boolean.
# Changing the type for a cell will recast the value into that type. If no type option is specified in the constructor, the type is
# automatically determed.
# @see Cell#cell_type_from_value
# @return [Symbol] The type of data this cell's value is cast to.
- # @raise [ArgumentExeption] Cell.type must be one of [:time, :float, :integer, :string]
+ # @raise [ArgumentExeption] Cell.type must be one of [:time, :float, :integer, :string, :boolean]
# @note
# If the value provided cannot be cast into the type specified, type is changed to :string and the following logic is applied.
- # :string to :integer or :float, type coversions always return 0 or 0.0
+ # :string to :integer or :float, type conversions always return 0 or 0.0
# :string, :integer, or :float to :time conversions always return the original value as a string and set the cells type to :string.
# No support is currently implemented for parsing time strings.
attr_reader :type
# @see type
def type=(v)
- RestrictionValidator.validate "Cell.type", [:time, :float, :integer, :string], v
+ RestrictionValidator.validate "Cell.type", [:time, :float, :integer, :string, :boolean], v
@type=v
self.value = @value unless @value.nil?
end
@@ -354,12 +354,15 @@ module Axlsx
# Determines the cell type based on the cell value.
# @note This is only used when a cell is created but no :type option is specified, the following rules apply:
# 1. If the value is an instance of Time, the type is set to :time
- # 2. :float and :integer types are determined by regular expression matching.
- # 3. Anything that does not meet either of the above is determined to be :string.
+ # 2. If the value is an instance of TrueClass or FalseClass, the type is set to :boolean
+ # 3. :float and :integer types are determined by regular expression matching.
+ # 4. Anything that does not meet either of the above is determined to be :string.
# @return [Symbol] The determined type
def cell_type_from_value(v)
if v.is_a? Time
:time
+ elsif v.is_a?(TrueClass) || v.is_a?(FalseClass)
+ :boolean
elsif v.to_s.match(/\A[+-]?\d+?\Z/) #numeric
:integer
elsif v.to_s.match(/\A[+-]?\d+\.\d+?\Z/) #float
@@ -381,6 +384,8 @@ module Axlsx
v.to_f
elsif @type == :integer
v.to_i
+ elsif @type == :boolean
+ v ? 1 : 0
else
@type = :string
v.to_s
diff --git a/test/workbook/worksheet/tc_cell.rb b/test/workbook/worksheet/tc_cell.rb
index 91e6559a..d9a1b351 100644
--- a/test/workbook/worksheet/tc_cell.rb
+++ b/test/workbook/worksheet/tc_cell.rb
@@ -51,7 +51,7 @@ class TestCell < Test::Unit::TestCase
end
def test_type
- assert_raise(ArgumentError, "type must be :string, :integer, :float, :time") { @c.type = :array }
+ assert_raise(ArgumentError, "type must be :string, :integer, :float, :time, :boolean") { @c.type = :array }
assert_nothing_raised("type can be changed") { @c.type = :string }
assert_equal(@c.value, "1.0", "changing type casts the value")
@@ -76,6 +76,8 @@ class TestCell < Test::Unit::TestCase
assert_equal(@c.send(:cell_type_from_value, "d"), :string)
assert_equal(@c.send(:cell_type_from_value, nil), :string)
assert_equal(@c.send(:cell_type_from_value, -1), :integer)
+ assert_equal(@c.send(:cell_type_from_value, true), :boolean)
+ assert_equal(@c.send(:cell_type_from_value, false), :boolean)
end
def test_cast_value
@@ -87,7 +89,9 @@ class TestCell < Test::Unit::TestCase
assert_equal(@c.send(:cast_value, "1.0"), 1.0)
@c.type = :string
assert_equal(@c.send(:cast_value, nil), "")
-
+ @c.type = :boolean
+ assert_equal(@c.send(:cast_value, true), 1)
+ assert_equal(@c.send(:cast_value, false), 0)
end
def test_color