xlsx_writer 0.3.2 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. data/CHANGELOG +12 -0
  2. data/README.markdown +1 -1
  3. data/foo.rb +2 -1
  4. data/lib/xlsx_writer.rb +100 -19
  5. data/lib/xlsx_writer/autofilter.rb +1 -1
  6. data/lib/xlsx_writer/cell.rb +140 -136
  7. data/lib/xlsx_writer/header_footer.rb +1 -1
  8. data/lib/xlsx_writer/page_setup.rb +1 -1
  9. data/lib/xlsx_writer/row.rb +8 -23
  10. data/lib/xlsx_writer/shared_strings.rb +70 -0
  11. data/lib/xlsx_writer/sheet.rb +145 -0
  12. data/lib/xlsx_writer/version.rb +2 -2
  13. data/lib/xlsx_writer/xml.rb +11 -3
  14. data/lib/xlsx_writer/{generators → xml}/app.erb +0 -0
  15. data/lib/xlsx_writer/{generators → xml}/app.rb +1 -1
  16. data/lib/xlsx_writer/{generators → xml}/content_types.erb +1 -0
  17. data/lib/xlsx_writer/{generators → xml}/content_types.rb +1 -1
  18. data/lib/xlsx_writer/{generators → xml}/doc_props.erb +0 -0
  19. data/lib/xlsx_writer/{generators → xml}/doc_props.rb +1 -1
  20. data/lib/xlsx_writer/{generators → xml}/image.rb +1 -1
  21. data/lib/xlsx_writer/{generators → xml}/rels.erb +0 -0
  22. data/lib/xlsx_writer/{generators → xml}/rels.rb +1 -1
  23. data/lib/xlsx_writer/{generators → xml}/sheet_rels.erb +0 -0
  24. data/lib/xlsx_writer/{generators → xml}/sheet_rels.rb +1 -1
  25. data/lib/xlsx_writer/{generators → xml}/styles.erb +0 -0
  26. data/lib/xlsx_writer/{generators → xml}/styles.rb +1 -1
  27. data/lib/xlsx_writer/{generators → xml}/vml_drawing.erb +0 -0
  28. data/lib/xlsx_writer/{generators → xml}/vml_drawing.rb +1 -1
  29. data/lib/xlsx_writer/{generators → xml}/vml_drawing_rels.erb +0 -0
  30. data/lib/xlsx_writer/{generators → xml}/vml_drawing_rels.rb +1 -1
  31. data/lib/xlsx_writer/{generators → xml}/workbook.erb +0 -0
  32. data/lib/xlsx_writer/{generators → xml}/workbook.rb +1 -1
  33. data/lib/xlsx_writer/{generators → xml}/workbook_rels.erb +1 -0
  34. data/lib/xlsx_writer/{generators → xml}/workbook_rels.rb +1 -1
  35. data/test/helper.rb +2 -0
  36. data/test/test_xlsx_writer.rb +43 -16
  37. data/xlsx_writer.gemspec +1 -0
  38. metadata +41 -25
  39. data/lib/xlsx_writer/document.rb +0 -88
  40. data/lib/xlsx_writer/generators/sheet.rb +0 -138
data/xlsx_writer.gemspec CHANGED
@@ -18,6 +18,7 @@ Gem::Specification.new do |s|
18
18
  s.add_development_dependency 'minitest-reporters'
19
19
  s.add_development_dependency 'yard'
20
20
  s.add_development_dependency 'remote_table'
21
+ s.add_development_dependency 'ruby-decimal'
21
22
 
22
23
  s.files = `git ls-files`.split("\n")
23
24
  s.test_files = `git ls-files -- {test,features}/*`.split("\n")
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.3.2
4
+ version: 0.4.0
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-07-12 00:00:00.000000000 Z
14
+ date: 2012-07-19 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: activesupport
@@ -125,6 +125,22 @@ dependencies:
125
125
  - - ! '>='
126
126
  - !ruby/object:Gem::Version
127
127
  version: '0'
128
+ - !ruby/object:Gem::Dependency
129
+ name: ruby-decimal
130
+ requirement: !ruby/object:Gem::Requirement
131
+ none: false
132
+ requirements:
133
+ - - ! '>='
134
+ - !ruby/object:Gem::Version
135
+ version: '0'
136
+ type: :development
137
+ prerelease: false
138
+ version_requirements: !ruby/object:Gem::Requirement
139
+ none: false
140
+ requirements:
141
+ - - ! '>='
142
+ - !ruby/object:Gem::Version
143
+ version: '0'
128
144
  description: Writes XLSX files. Minimal XML and style. Supports autofilters and headers/footers
129
145
  with images and page numbers.
130
146
  email:
@@ -144,34 +160,34 @@ files:
144
160
  - lib/xlsx_writer.rb
145
161
  - lib/xlsx_writer/autofilter.rb
146
162
  - lib/xlsx_writer/cell.rb
147
- - lib/xlsx_writer/document.rb
148
- - lib/xlsx_writer/generators/app.erb
149
- - lib/xlsx_writer/generators/app.rb
150
- - lib/xlsx_writer/generators/content_types.erb
151
- - lib/xlsx_writer/generators/content_types.rb
152
- - lib/xlsx_writer/generators/doc_props.erb
153
- - lib/xlsx_writer/generators/doc_props.rb
154
- - lib/xlsx_writer/generators/image.rb
155
- - lib/xlsx_writer/generators/rels.erb
156
- - lib/xlsx_writer/generators/rels.rb
157
- - lib/xlsx_writer/generators/sheet.rb
158
- - lib/xlsx_writer/generators/sheet_rels.erb
159
- - lib/xlsx_writer/generators/sheet_rels.rb
160
- - lib/xlsx_writer/generators/styles.erb
161
- - lib/xlsx_writer/generators/styles.rb
162
- - lib/xlsx_writer/generators/vml_drawing.erb
163
- - lib/xlsx_writer/generators/vml_drawing.rb
164
- - lib/xlsx_writer/generators/vml_drawing_rels.erb
165
- - lib/xlsx_writer/generators/vml_drawing_rels.rb
166
- - lib/xlsx_writer/generators/workbook.erb
167
- - lib/xlsx_writer/generators/workbook.rb
168
- - lib/xlsx_writer/generators/workbook_rels.erb
169
- - lib/xlsx_writer/generators/workbook_rels.rb
170
163
  - lib/xlsx_writer/header_footer.rb
171
164
  - lib/xlsx_writer/page_setup.rb
172
165
  - lib/xlsx_writer/row.rb
166
+ - lib/xlsx_writer/shared_strings.rb
167
+ - lib/xlsx_writer/sheet.rb
173
168
  - lib/xlsx_writer/version.rb
174
169
  - lib/xlsx_writer/xml.rb
170
+ - lib/xlsx_writer/xml/app.erb
171
+ - lib/xlsx_writer/xml/app.rb
172
+ - lib/xlsx_writer/xml/content_types.erb
173
+ - lib/xlsx_writer/xml/content_types.rb
174
+ - lib/xlsx_writer/xml/doc_props.erb
175
+ - lib/xlsx_writer/xml/doc_props.rb
176
+ - lib/xlsx_writer/xml/image.rb
177
+ - lib/xlsx_writer/xml/rels.erb
178
+ - lib/xlsx_writer/xml/rels.rb
179
+ - lib/xlsx_writer/xml/sheet_rels.erb
180
+ - lib/xlsx_writer/xml/sheet_rels.rb
181
+ - lib/xlsx_writer/xml/styles.erb
182
+ - lib/xlsx_writer/xml/styles.rb
183
+ - lib/xlsx_writer/xml/vml_drawing.erb
184
+ - lib/xlsx_writer/xml/vml_drawing.rb
185
+ - lib/xlsx_writer/xml/vml_drawing_rels.erb
186
+ - lib/xlsx_writer/xml/vml_drawing_rels.rb
187
+ - lib/xlsx_writer/xml/workbook.erb
188
+ - lib/xlsx_writer/xml/workbook.rb
189
+ - lib/xlsx_writer/xml/workbook_rels.erb
190
+ - lib/xlsx_writer/xml/workbook_rels.rb
175
191
  - repack.rb
176
192
  - test/helper.rb
177
193
  - test/support/image1.emf
@@ -1,88 +0,0 @@
1
- require 'fileutils'
2
-
3
- module XlsxWriter
4
- class Document
5
- class << self
6
- def auto
7
- ::Dir[::File.expand_path('../generators/*.rb', __FILE__)].map do |path|
8
- XlsxWriter.const_get ::File.basename(path, '.rb').camelcase
9
- end.reject do |klass|
10
- klass.const_defined?(:AUTO) and klass.const_get(:AUTO) == false
11
- end
12
- end
13
- end
14
-
15
- attr_reader :staging_dir
16
- attr_reader :sheets
17
- attr_reader :images
18
- attr_reader :page_setup
19
- attr_reader :header_footer
20
-
21
- def initialize
22
- staging_dir = ::UnixUtils.tmp_path 'xlsx_writer'
23
- ::FileUtils.mkdir_p staging_dir
24
- @staging_dir = staging_dir
25
- @sheets = []
26
- @images = []
27
- @page_setup = PageSetup.new
28
- @header_footer = HeaderFooter.new
29
- @mutex = ::Mutex.new
30
- end
31
-
32
- # Instead of TRUE or FALSE, show TRUE and blank if false
33
- def quiet_booleans!
34
- @quiet_booleans = true
35
- end
36
-
37
- def quiet_booleans?
38
- @quiet_booleans == true
39
- end
40
-
41
- def add_sheet(name)
42
- raise ::RuntimeError, "Can't add sheet, already generated!" if generated?
43
- sheet = Sheet.new self, name
44
- sheets << sheet
45
- sheet
46
- end
47
-
48
- delegate :header, :footer, :to => :header_footer
49
-
50
- def add_image(path, width, height)
51
- raise ::RuntimeError, "Can't add image, already generated!" if generated?
52
- image = Image.new self, path, width, height
53
- images << image
54
- image
55
- end
56
-
57
- def path
58
- @path || @mutex.synchronize do
59
- @path ||= begin
60
- sheets.each(&:generate)
61
- images.each(&:generate)
62
- Document.auto.each do |part|
63
- part.new(self).generate
64
- end
65
- with_zip_extname = ::UnixUtils.zip staging_dir
66
- with_xlsx_extname = with_zip_extname.sub(/.zip$/, '.xlsx')
67
- ::FileUtils.mv with_zip_extname, with_xlsx_extname
68
- @generated = true
69
- with_xlsx_extname
70
- end
71
- end
72
- end
73
-
74
- def cleanup
75
- ::FileUtils.rm_rf @staging_dir.to_s
76
- ::FileUtils.rm_f @path.to_s
77
- end
78
-
79
- def generate
80
- path
81
- true
82
- end
83
-
84
- def generated?
85
- @generated == true
86
- end
87
- end
88
- end
@@ -1,138 +0,0 @@
1
- require 'fast_xs'
2
-
3
- module XlsxWriter
4
- class Sheet < Xml
5
- class << self
6
- def excel_name(value)
7
- str = value.to_s.dup
8
- str.gsub! '/', '' # remove forward slashes
9
- str.gsub! /\s+/, '' # compress "inner" whitespace
10
- str.strip! # trim whitespace from ends
11
- str.fast_xs
12
- end
13
- end
14
-
15
- AUTO = false
16
-
17
- attr_reader :name
18
- attr_reader :rows
19
- attr_reader :autofilters
20
-
21
- # Freeze the pane under this top left cell
22
- attr_accessor :freeze_top_left
23
-
24
- def initialize(document, name)
25
- @name = Sheet.excel_name name
26
- @rows = []
27
- @autofilters = []
28
- super document
29
- end
30
-
31
- def ndx
32
- document.sheets.index(self) + 1
33
- end
34
-
35
- def local_id
36
- ndx - 1
37
- end
38
-
39
- # +1 because styles.xml occupies the first spot
40
- def rid
41
- "rId#{ndx + 1}"
42
- end
43
-
44
- def relative_path
45
- "xl/worksheets/sheet#{ndx}.xml"
46
- end
47
-
48
- def absolute_path
49
- "/#{relative_path}"
50
- end
51
-
52
- # specify range like "A1:C1"
53
- def add_autofilter(range)
54
- raise ::RuntimeError, "Can't add autofilter, already generated!" if generated?
55
- autofilters << Autofilter.new(self, range)
56
- end
57
-
58
- def add_row(data)
59
- raise ::RuntimeError, "Can't add row, already generated!" if generated?
60
- row = Row.new self, data
61
- rows << row
62
- row
63
- end
64
-
65
- # override Xml method to save memory
66
- def path
67
- @path || @mutex.synchronize do
68
- @path ||= begin
69
- memo = ::File.join document.staging_dir, relative_path
70
- ::FileUtils.mkdir_p ::File.dirname(memo)
71
- ::File.open(memo, 'wb') do |f|
72
- to_file f
73
- end
74
- converted = UnixUtils.unix2dos memo
75
- ::FileUtils.mv converted, memo
76
- SheetRels.new(document, self).path
77
- @generated = true
78
- memo
79
- end
80
- end
81
- end
82
-
83
- private
84
-
85
- def y_split
86
- if freeze_top_left =~ /(\d+)$/
87
- $1.to_i - 1
88
- else
89
- raise "freeze_top_left must be like 'A3', was #{freeze_top_left}"
90
- end
91
- end
92
-
93
- # not using ERB to save memory
94
- def to_file(f)
95
- f.write <<-EOS
96
- <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
97
- <worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
98
- EOS
99
- if freeze_top_left
100
- f.write <<-EOS
101
- <sheetViews>
102
- <sheetView workbookViewId="0">
103
- <pane ySplit="#{y_split}" topLeftCell="#{freeze_top_left}" activePane="bottomLeft" state="frozen"/>
104
- </sheetView>
105
- </sheetViews>
106
- EOS
107
- end
108
- f.write %{<cols>}
109
- (0..max_length-1).each do |x|
110
- f.write %{<col min="#{x+1}" max="#{x+1}" width="#{max_cell_width(x)}" bestFit="1" customWidth="1" />}
111
- end
112
- f.write %{</cols>}
113
- f.write %{<sheetData>}
114
- rows.each { |row| f.write row.to_xml }
115
- f.write %{</sheetData>}
116
- autofilters.each { |autofilter| f.write autofilter.to_xml }
117
- f.write document.page_setup.to_xml
118
- f.write document.header_footer.to_xml
119
- f.write %{</worksheet>}
120
- end
121
-
122
- def max_length
123
- if max = rows.max_by { |row| row.length }
124
- max.length
125
- else
126
- 1
127
- end
128
- end
129
-
130
- def max_cell_width(x)
131
- if max = rows.max_by { |row| row.cell_width(x) }
132
- max.cell_width x
133
- else
134
- Cell.pixel_width 5
135
- end
136
- end
137
- end
138
- end