Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Use 1904 datesystem instead of strings for Date and Time objects;

Use builtin formatCode values instead of custom ones.
  • Loading branch information...
commit 3f09489873ff8da7cd8fb4f9cab11f8983e22f84 1 parent 7ed3267
@zsombor zsombor authored
View
16 lib/simple_xlsx/serializer.rb
@@ -23,6 +23,7 @@ def add_workbook_part
f.puts <<-ends
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
+<workbookPr date1904="1" />
<sheets>
ends
@doc.sheets.each_with_index do |sheet, ndx|
@@ -127,11 +128,6 @@ def add_styles
<styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
<numFmts count="7">
<numFmt formatCode="GENERAL" numFmtId="164"/>
- <numFmt formatCode="MM/DD/YYYY\ HH:MM:SS" numFmtId="165"/>
- <numFmt formatCode="MMM\ D&quot;, &quot;YYYY" numFmtId="166"/>
- <numFmt formatCode="0" numFmtId="167"/>
- <numFmt formatCode="0.00" numFmtId="168"/>
- <numFmt formatCode="@" numFmtId="169"/>
<numFmt formatCode="&quot;TRUE&quot;;&quot;TRUE&quot;;&quot;FALSE&quot;" numFmtId="170"/>
</numFmts>
<fonts count="5">
@@ -175,11 +171,11 @@ def add_styles
</cellStyleXfs>
<cellXfs count="7">
<xf applyAlignment="false" applyBorder="false" applyFont="false" applyProtection="false" borderId="0" fillId="0" fontId="4" numFmtId="164" xfId="0"></xf>
- <xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="4" numFmtId="165" xfId="0"></xf>
- <xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="4" numFmtId="166" xfId="0"></xf>
- <xf applyAlignment="false" applyBorder="false" applyFont="false" applyProtection="false" borderId="0" fillId="0" fontId="4" numFmtId="167" xfId="0"></xf>
- <xf applyAlignment="false" applyBorder="false" applyFont="false" applyProtection="false" borderId="0" fillId="0" fontId="4" numFmtId="168" xfId="0"></xf>
- <xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="4" numFmtId="169" xfId="0"></xf>
+ <xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="4" numFmtId="22" xfId="0"></xf>
+ <xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="4" numFmtId="15" xfId="0"></xf>
+ <xf applyAlignment="false" applyBorder="false" applyFont="false" applyProtection="false" borderId="0" fillId="0" fontId="4" numFmtId="1" xfId="0"></xf>
+ <xf applyAlignment="false" applyBorder="false" applyFont="false" applyProtection="false" borderId="0" fillId="0" fontId="4" numFmtId="2" xfId="0"></xf>
+ <xf applyAlignment="false" applyBorder="false" applyFont="true" applyProtection="false" borderId="0" fillId="0" fontId="4" numFmtId="49" xfId="0"></xf>
<xf applyAlignment="false" applyBorder="false" applyFont="false" applyProtection="false" borderId="0" fillId="0" fontId="4" numFmtId="170" xfId="0"></xf>
</cellXfs>
<cellStyles count="6"><cellStyle builtinId="0" customBuiltin="false" name="Normal" xfId="0"/>
View
21 lib/simple_xlsx/sheet.rb
@@ -1,4 +1,5 @@
require 'bigdecimal'
+require 'time'
module SimpleXlsx
@@ -37,13 +38,15 @@ def self.format_field_and_type_and_style value
if value.is_a?(String)
[:inlineStr, "<is><t>#{value.to_xs}</t></is>", 5]
elsif value.is_a?(BigDecimal)
- [:n, "<v>#{value.to_s('f')}</v>", 3]
+ [:n, "<v>#{value.to_s('f')}</v>", 4]
+ elsif value.is_a?(Float)
+ [:n, "<v>#{value.to_s}</v>", 4]
elsif value.is_a?(Numeric)
[:n, "<v>#{value.to_s}</v>", 3]
elsif value.is_a?(Date)
- [:inlineStr, "<is><t>#{value.strftime('%Y-%b-%d')}</t></is>", 1]
- elsif value.is_a?(DateTime)
- [:inlineStr, "<is><t>#{value.strftime('%Y-%b-%d %H:%M:%S')}</t></is>", 2]
+ [:n, "<v>#{days_since_jan_1_1904(value)}</v>", 2]
+ elsif value.is_a?(Time)
+ [:n, "<v>#{fractional_days_since_jan_1_1904(value)}</v>", 1]
elsif value.is_a?(TrueClass) || value.is_a?(FalseClass)
[:b, "<v>#{value ? '1' : '0'}</v>", 6]
else
@@ -51,6 +54,16 @@ def self.format_field_and_type_and_style value
end
end
+ def self.days_since_jan_1_1904 date
+ @@jan_1_1904 ||= Date.parse("1904 Jan 1")
+ (date - @@jan_1_1904).to_i
+ end
+
+ def self.fractional_days_since_jan_1_1904 value
+ @@jan_1_1904_midnight ||= ::Time.utc(1904, 1, 1)
+ (value - @@jan_1_1904_midnight) / 86400.0 #24*60*60
+ end
+
def self.abc
@@abc ||= ('A'..'Z').to_a
end
View
13 test/simple_xlsx/sheet_test.rb
@@ -1,5 +1,6 @@
require File.dirname(__FILE__) + '/../test_helper.rb'
require "rexml/document"
+require 'time'
module SimpleXlsx
@@ -34,16 +35,22 @@ def test_format_field_for_numbers
v = Sheet.format_field_and_type_and_style 3
assert_equal [:n, "<v>3</v>", 3], v
v = Sheet.format_field_and_type_and_style(BigDecimal.new("45"))
- assert_equal [:n, "<v>45.0</v>", 3], v
+ assert_equal [:n, "<v>45.0</v>", 4], v
v = Sheet.format_field_and_type_and_style(9.32)
- assert_equal [:n, "<v>9.32</v>", 3], v
+ assert_equal [:n, "<v>9.32</v>", 4], v
end
def test_format_field_for_date
v = Sheet.format_field_and_type_and_style(Date.parse('2010-Jul-24'))
- assert_equal [:inlineStr, "<is><t>2010-Jul-24</t></is>", 1], v
+ assert_equal [:n, "<v>38921</v>", 2], v
end
+ def test_format_field_for_datetime
+ v = Sheet.format_field_and_type_and_style(Time.parse('2010-Jul-24 12:00 UTC'))
+ assert_equal [:n, "<v>38921.5</v>", 1], v
+ end
+
+
def test_format_field_for_boolean
v = Sheet.format_field_and_type_and_style(false)
assert_equal [:b, "<v>0</v>", 6], v
View
4 test/simple_xlsx_test.rb
@@ -7,8 +7,8 @@ def test_top_level
FileUtils.rm_f "test.xlsx"
o = SimpleXlsx::Serializer.new("test.xlsx") do |doc|
doc.add_sheet "First" do |sheet|
- sheet.add_row ["Hello", "World", 3.14]
- sheet.add_row ["Another", "Row", Date.today]
+ sheet.add_row ["Hello", "World", 3.14, 7]
+ sheet.add_row ["Another", "Row", Date.today, Time.parse('2010-Jul-24 12:00 UTC')]
end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.