From 6439ce4d2ec5c97ad82a8dc10d974ca9754f2113 Mon Sep 17 00:00:00 2001 From: Randy Morgan Date: Thu, 1 Dec 2011 14:38:13 +0900 Subject: added default formatting for date types when the cell style is 0 and the data type is :time --- examples/example.rb | 2 +- lib/axlsx/stylesheet/styles.rb | 2 ++ lib/axlsx/util/constants.rb | 3 +++ lib/axlsx/workbook/worksheet/cell.rb | 1 + test/rels/tc_relationships.rb | 1 - test/stylesheet/tc_styles.rb | 12 ------------ test/workbook/worksheet/tc_cell.rb | 7 ++++++- 7 files changed, 13 insertions(+), 15 deletions(-) diff --git a/examples/example.rb b/examples/example.rb index 1ef2ab24..609096b5 100644 --- a/examples/example.rb +++ b/examples/example.rb @@ -9,7 +9,7 @@ if ARGV.size == 0 || ARGV.include?("1") p = Axlsx::Package.new p.workbook.add_worksheet do |sheet| sheet.add_row ["First", "Second", "Third"] - sheet.add_row [1, 2, 3] + sheet.add_row [1, 2, Time.now] end p.serialize("example1.xlsx") end diff --git a/lib/axlsx/stylesheet/styles.rb b/lib/axlsx/stylesheet/styles.rb index 6b64fb30..de5835dd 100644 --- a/lib/axlsx/stylesheet/styles.rb +++ b/lib/axlsx/stylesheet/styles.rb @@ -286,6 +286,8 @@ module Axlsx @cellXfs = SimpleTypedList.new Xf, "cellXfs" @cellXfs << Xf.new(:borderId=>0, :xfId=>0, :numFmtId=>0, :fontId=>0, :fillId=>0) @cellXfs << Xf.new(:borderId=>1, :xfId=>0, :numFmtId=>0, :fontId=>0, :fillId=>0) + # default date formatting + @cellXfs << Xf.new(:borderId=>0, :xfId=>0, :numFmtId=>14, :fontId=>0, :fillId=>0) @cellXfs.lock @dxfs = SimpleTypedList.new(Xf, "dxfs"); @dxfs.lock diff --git a/lib/axlsx/util/constants.rb b/lib/axlsx/util/constants.rb index 50dd6f27..4f1cb561 100644 --- a/lib/axlsx/util/constants.rb +++ b/lib/axlsx/util/constants.rb @@ -199,6 +199,9 @@ module Axlsx # cellXfs id for thin borders around the cell STYLE_THIN_BORDER = 1 + # cellXfs id for default date styling + STYLE_DATE = 2 + # error messages RestrictionValidor ERR_RESTRICTION = "Invalid Data: %s. %s must be one of %s." diff --git a/lib/axlsx/workbook/worksheet/cell.rb b/lib/axlsx/workbook/worksheet/cell.rb index 1b1b60cc..a9b4a704 100644 --- a/lib/axlsx/workbook/worksheet/cell.rb +++ b/lib/axlsx/workbook/worksheet/cell.rb @@ -169,6 +169,7 @@ module Axlsx def cast_value(v) if (@type == :time && v.is_a?(Time)) || (@type == :time && v.respond_to?(:to_time)) v = v.respond_to?(:to_time) ? v.to_time : v + self.style = STYLE_DATE if self.style == 0 # Using hardcoded offsets here as some operating systems will not except a 'negative' offset from the ruby epoc. # (1970) epoc1900 = -2209021200 #Time.local(1900, 1, 1) diff --git a/test/rels/tc_relationships.rb b/test/rels/tc_relationships.rb index fb3795c3..c6b4860d 100644 --- a/test/rels/tc_relationships.rb +++ b/test/rels/tc_relationships.rb @@ -3,7 +3,6 @@ require 'axlsx.rb' class TestRelationships < Test::Unit::TestCase - def test_valid_document @rels = Axlsx::Relationships.new schema = Nokogiri::XML::Schema(File.open(Axlsx::RELS_XSD)) diff --git a/test/stylesheet/tc_styles.rb b/test/stylesheet/tc_styles.rb index 0c9b3fba..15020608 100644 --- a/test/stylesheet/tc_styles.rb +++ b/test/stylesheet/tc_styles.rb @@ -49,16 +49,4 @@ class TestStyles < Test::Unit::TestCase end - - #:numFmts, :fonts, :fills, :borders, :cellStyleXfs, :cellXfs, :dxfs, :tableStyles - def test_ensure_locking - assert_equal(@styles.numFmts.locked_at, 2, "numFmts should be locked at 2") - assert_equal(@styles.fonts.locked_at, 1, "fonts should be locked at 1" ) - assert_equal(@styles.fills.locked_at, 2, "fills should be locked at 2" ) - assert_equal(@styles.borders.locked_at, 2, "borders should be locked at two" ) - assert_equal(@styles.cellStyleXfs.locked_at, 1, "cellStyleXfs should be locked at two" ) - assert_equal(@styles.cellXfs.locked_at, 2, "cellXfs should be locked at 2" ) - assert_equal(@styles.dxfs.locked_at, 0, "dxfs should be locked at 0" ) - assert_equal(@styles.tableStyles.locked_at, 0, "tableStyles should be locked at 0" ) - end end diff --git a/test/workbook/worksheet/tc_cell.rb b/test/workbook/worksheet/tc_cell.rb index 6cc9a2c8..58c76bbc 100644 --- a/test/workbook/worksheet/tc_cell.rb +++ b/test/workbook/worksheet/tc_cell.rb @@ -18,6 +18,11 @@ class TestCell < Test::Unit::TestCase assert_equal(@c.value, 1.0, "type option is applied and value is casted") end + def test_style_date_data + c = Axlsx::Cell.new(@c.row, Time.now) + assert_equal(Axlsx::STYLE_DATE, c.style) + end + def test_index assert_equal(@c.index, @row.cells.index(@c)) end @@ -31,7 +36,7 @@ class TestCell < Test::Unit::TestCase end def test_style - assert_raise(ArgumentError, "must reject invalid style indexes") { @c.style=3 } + assert_raise(ArgumentError, "must reject invalid style indexes") { @c.style=@c.row.worksheet.workbook.styles.cellXfs.size } assert_nothing_raised("must allow valid style index changes") {@c.style=1} assert_equal(@c.style, 1) end -- cgit v1.2.3