xlsx_writer 0.2.1 → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG +10 -0
- data/README.markdown +9 -0
- data/foo.rb +14 -6
- data/lib/xlsx_writer/cell.rb +26 -11
- data/lib/xlsx_writer/generators/styles.erb +18 -17
- data/lib/xlsx_writer/version.rb +1 -1
- metadata +2 -2
data/CHANGELOG
CHANGED
data/README.markdown
CHANGED
@@ -2,6 +2,12 @@
|
|
2
2
|
|
3
3
|
Writes (doesn't read or modify) XLSX files.
|
4
4
|
|
5
|
+
## Real-world usage
|
6
|
+
|
7
|
+
<p><a href="http://brighterplanet.com"><img src="https://s3.amazonaws.com/static.brighterplanet.com/assets/logos/flush-left/inline/green/rasterized/brighter_planet-160-transparent.png" alt="Brighter Planet logo"/></a></p>
|
8
|
+
|
9
|
+
We use `xlsx_writer` for [sustainability analytics at Brighter Planet](http://brighterplanet.com/case_studies).
|
10
|
+
|
5
11
|
## Credit
|
6
12
|
|
7
13
|
Based on the [original simple\_xlsx\_writer gem](https://github.com/harvesthq/simple_xlsx_writer) and [patches by mumboe](https://github.com/mumboe/simple_xlsx_writer)
|
@@ -74,3 +80,6 @@ Features not present in simple_xlsx_writer:
|
|
74
80
|
# don't forget
|
75
81
|
doc.cleanup
|
76
82
|
|
83
|
+
## Copyright
|
84
|
+
|
85
|
+
Copyright (c) 2012 Dee Zsombor, Justin Beck, Seamus Abshere. See LICENSE for details.
|
data/foo.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
1
2
|
require 'bundler/setup'
|
2
3
|
|
3
4
|
if ::Bundler.definition.specs['debugger'].first
|
@@ -10,15 +11,22 @@ require 'xlsx_writer'
|
|
10
11
|
|
11
12
|
@doc = XlsxWriter::Document.new
|
12
13
|
|
13
|
-
@sheet1 = @doc.add_sheet("Sheet1")
|
14
|
-
@sheet1.add_row(['a', 'a'])
|
15
|
-
@sheet1.add_row(['a', 'a'])
|
16
|
-
@sheet1.add_row(['a', 'a'])
|
17
|
-
# @sheet1.add_row(['foo', 'bar'])
|
18
|
-
@sheet1.add_autofilter 'A1:B1'
|
14
|
+
# @sheet1 = @doc.add_sheet("Sheet1")
|
15
|
+
# @sheet1.add_row(['a', 'a'])
|
16
|
+
# @sheet1.add_row(['a', { :value => 'a', :faded => true, :type => :String }])
|
17
|
+
# @sheet1.add_row(['a', 'a'])
|
18
|
+
# # @sheet1.add_row(['foo', 'bar'])
|
19
|
+
# @sheet1.add_autofilter 'A1:B1'
|
19
20
|
|
20
21
|
@sheet2 = @doc.add_sheet("Sheet2")
|
21
22
|
@sheet2.add_row(['a', 'a'])
|
23
|
+
@sheet2.add_row(['false1', false])
|
24
|
+
@sheet2.add_row(['false2', {:value => false, :type => :Boolean}])
|
25
|
+
@sheet2.add_row(['false3', 'faLse'])
|
26
|
+
@sheet2.add_row(['true1', true])
|
27
|
+
@sheet2.add_row(['true2', {:value => true, :type => :Boolean}])
|
28
|
+
@sheet2.add_row(['true3', 'trUe'])
|
29
|
+
|
22
30
|
# @sheet2.add_row(['hello', 'world'])
|
23
31
|
# @sheet2.add_row(['yo', 'there'])
|
24
32
|
# @sheet2.add_row(['foo', 'bar'])
|
data/lib/xlsx_writer/cell.rb
CHANGED
@@ -18,8 +18,8 @@ module XlsxWriter
|
|
18
18
|
end
|
19
19
|
|
20
20
|
# TODO make a class for this
|
21
|
-
def excel_style_number(calculated_type)
|
22
|
-
case calculated_type
|
21
|
+
def excel_style_number(calculated_type, faded = false)
|
22
|
+
i = case calculated_type
|
23
23
|
when :String
|
24
24
|
0
|
25
25
|
when :Boolean
|
@@ -35,6 +35,11 @@ module XlsxWriter
|
|
35
35
|
else
|
36
36
|
raise ::ArgumentError, "Unknown cell type #{k}"
|
37
37
|
end
|
38
|
+
if faded
|
39
|
+
i * 2 + 1
|
40
|
+
else
|
41
|
+
i * 2
|
42
|
+
end
|
38
43
|
end
|
39
44
|
|
40
45
|
def excel_column_letter(i)
|
@@ -86,16 +91,23 @@ module XlsxWriter
|
|
86
91
|
end
|
87
92
|
|
88
93
|
def calculate_type(value)
|
89
|
-
|
94
|
+
case value
|
95
|
+
when Date
|
90
96
|
:Date
|
91
|
-
|
97
|
+
when Integer
|
92
98
|
:Integer
|
93
|
-
|
99
|
+
when Float
|
94
100
|
:Decimal
|
95
|
-
|
101
|
+
when Numeric
|
96
102
|
:Number
|
103
|
+
when TrueClass, FalseClass, TRUE_FALSE_PATTERN
|
104
|
+
:Boolean
|
97
105
|
else
|
98
|
-
|
106
|
+
if (defined?(Decimal) and value.is_a?(Decimal)) or (defined?(BigDecimal) and value.is_a?(BigDecimal))
|
107
|
+
:Decimal
|
108
|
+
else
|
109
|
+
:String
|
110
|
+
end
|
99
111
|
end
|
100
112
|
end
|
101
113
|
|
@@ -144,8 +156,9 @@ module XlsxWriter
|
|
144
156
|
MAX_DIGIT_WIDTH = 5
|
145
157
|
MAX_REASONABLE_WIDTH = 75
|
146
158
|
DATE_LENGTH = 'YYYY-MM-DD'.length
|
147
|
-
BOOLEAN_LENGTH = 'FALSE'.length
|
159
|
+
BOOLEAN_LENGTH = 'FALSE'.length + 1
|
148
160
|
JAN_1_1900 = ::Time.parse '1900-01-01'
|
161
|
+
TRUE_FALSE_PATTERN = %r{^true|false$}i
|
149
162
|
|
150
163
|
attr_reader :row
|
151
164
|
attr_reader :value
|
@@ -158,21 +171,23 @@ module XlsxWriter
|
|
158
171
|
@row = row
|
159
172
|
if data.is_a?(::Hash)
|
160
173
|
data = data.symbolize_keys
|
161
|
-
calculated_type = data[:type]
|
162
174
|
@value = data[:value]
|
175
|
+
faded = data[:faded]
|
176
|
+
calculated_type = data[:type] || Cell.calculate_type(@value)
|
163
177
|
else
|
164
178
|
@value = data
|
179
|
+
faded = false
|
165
180
|
calculated_type = Cell.calculate_type @value
|
166
181
|
end
|
167
182
|
character_width = Cell.character_width @value, calculated_type
|
168
183
|
@pixel_width = Cell.pixel_width character_width
|
169
184
|
@excel_type = Cell.excel_type calculated_type
|
170
|
-
@excel_style_number = Cell.excel_style_number calculated_type
|
185
|
+
@excel_style_number = Cell.excel_style_number calculated_type, faded
|
171
186
|
@excel_value = Cell.send "excel_#{calculated_type.to_s.underscore}", @value
|
172
187
|
end
|
173
188
|
|
174
189
|
def to_xml
|
175
|
-
if value.
|
190
|
+
if value.nil? or (value.is_a?(String) and value.empty?)
|
176
191
|
%{<c r="#{excel_column_letter}#{row.ndx}" s="0" t="inlineStr" />}
|
177
192
|
elsif excel_type == :inlineStr
|
178
193
|
%{<c r="#{excel_column_letter}#{row.ndx}" s="#{excel_style_number}" t="#{excel_type}"><is><t>#{excel_value}</t></is></c>}
|
@@ -4,11 +4,17 @@
|
|
4
4
|
<numFmt numFmtId="164" formatCode="0.00" />
|
5
5
|
</numFmts>
|
6
6
|
|
7
|
-
<fonts count="
|
7
|
+
<fonts count="2">
|
8
8
|
<font>
|
9
9
|
<sz val="10"/>
|
10
10
|
<name val="Arial"/>
|
11
11
|
</font>
|
12
|
+
<!-- faded -->
|
13
|
+
<font>
|
14
|
+
<color rgb="FFCCCCCC"/>
|
15
|
+
<sz val="10"/>
|
16
|
+
<name val="Arial"/>
|
17
|
+
</font>
|
12
18
|
</fonts>
|
13
19
|
|
14
20
|
<fills count="1">
|
@@ -26,26 +32,21 @@
|
|
26
32
|
<xf builtinId="4" />
|
27
33
|
</cellStyleXfs>
|
28
34
|
|
29
|
-
<cellXfs count="
|
35
|
+
<cellXfs count="10">
|
30
36
|
<!-- general -->
|
31
|
-
<xf numFmtId="0" fontId="0">
|
32
|
-
|
33
|
-
</xf>
|
37
|
+
<xf numFmtId="0" fontId="0"><alignment vertical="top" horizontal="left" /></xf>
|
38
|
+
<xf numFmtId="0" fontId="1"><alignment vertical="top" horizontal="left" /></xf>
|
34
39
|
<!-- currency (really accounting) -->
|
35
|
-
<xf numFmtId="39" fontId="0" xfId="1">
|
36
|
-
|
37
|
-
</xf>
|
40
|
+
<xf numFmtId="39" fontId="0" xfId="1"><alignment vertical="top" horizontal="right" /></xf>
|
41
|
+
<xf numFmtId="39" fontId="1" xfId="1"><alignment vertical="top" horizontal="right" /></xf>
|
38
42
|
<!-- date -->
|
39
|
-
<xf numFmtId="14" fontId="0">
|
40
|
-
|
41
|
-
</xf>
|
43
|
+
<xf numFmtId="14" fontId="0"><alignment vertical="top" horizontal="left" /></xf>
|
44
|
+
<xf numFmtId="14" fontId="1"><alignment vertical="top" horizontal="left" /></xf>
|
42
45
|
<!-- integer -->
|
43
|
-
<xf numFmtId="0" fontId="0">
|
44
|
-
|
45
|
-
</xf>
|
46
|
+
<xf numFmtId="0" fontId="0"><alignment vertical="top" horizontal="right" /></xf>
|
47
|
+
<xf numFmtId="0" fontId="1"><alignment vertical="top" horizontal="right" /></xf>
|
46
48
|
<!-- float/decimal/etc. -->
|
47
|
-
<xf numFmtId="164" fontId="0">
|
48
|
-
|
49
|
-
</xf>
|
49
|
+
<xf numFmtId="164" fontId="0"><alignment vertical="top" horizontal="right" /></xf>
|
50
|
+
<xf numFmtId="164" fontId="1"><alignment vertical="top" horizontal="right" /></xf>
|
50
51
|
</cellXfs>
|
51
52
|
</styleSheet>
|
data/lib/xlsx_writer/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: xlsx_writer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -11,7 +11,7 @@ authors:
|
|
11
11
|
autorequire:
|
12
12
|
bindir: bin
|
13
13
|
cert_chain: []
|
14
|
-
date: 2012-
|
14
|
+
date: 2012-07-04 00:00:00.000000000 Z
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
17
|
name: activesupport
|