Skip to content

Commit

Permalink
added default formatting for date types when the cell style is 0 and …
Browse files Browse the repository at this point in the history
…the data type is :time
  • Loading branch information
Randy Morgan committed Dec 1, 2011
1 parent 8cf0296 commit 6439ce4
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 15 deletions.
2 changes: 1 addition & 1 deletion examples/example.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
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
Expand Down
2 changes: 2 additions & 0 deletions lib/axlsx/stylesheet/styles.rb
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,8 @@ def load_default_styles
@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
Expand Down
3 changes: 3 additions & 0 deletions lib/axlsx/util/constants.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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."

Expand Down
1 change: 1 addition & 0 deletions lib/axlsx/workbook/worksheet/cell.rb
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ def cell_type_from_value(v)
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)
Expand Down
1 change: 0 additions & 1 deletion test/rels/tc_relationships.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

class TestRelationships < Test::Unit::TestCase


def test_valid_document
@rels = Axlsx::Relationships.new
schema = Nokogiri::XML::Schema(File.open(Axlsx::RELS_XSD))
Expand Down
12 changes: 0 additions & 12 deletions test/stylesheet/tc_styles.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,4 @@ def test_add_style

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
7 changes: 6 additions & 1 deletion test/workbook/worksheet/tc_cell.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ def test_initialize
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
Expand All @@ -31,7 +36,7 @@ def test_r_abs
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
Expand Down

0 comments on commit 6439ce4

Please sign in to comment.