axlsx 1.0.13 → 1.0.14

Sign up to get free protection for your applications and to get access to all the features.
Files changed (96) hide show
  1. data/README.md +161 -134
  2. data/examples/example.rb +133 -153
  3. data/lib/axlsx.rb +3 -1
  4. data/lib/axlsx/#cfb.xlsx# +0 -0
  5. data/lib/axlsx/drawing/axis.rb +13 -10
  6. data/lib/axlsx/drawing/bar_3D_chart.rb +19 -11
  7. data/lib/axlsx/drawing/bar_series.rb +1 -1
  8. data/lib/axlsx/drawing/cat_axis.rb +5 -5
  9. data/lib/axlsx/drawing/cat_axis_data.rb +7 -7
  10. data/lib/axlsx/drawing/chart.rb +29 -15
  11. data/lib/axlsx/drawing/drawing.rb +1 -1
  12. data/lib/axlsx/drawing/graphic_frame.rb +10 -10
  13. data/lib/axlsx/drawing/line_3D_chart.rb +15 -7
  14. data/lib/axlsx/drawing/marker.rb +1 -1
  15. data/lib/axlsx/drawing/one_cell_anchor.rb +4 -4
  16. data/lib/axlsx/drawing/pic.rb +14 -14
  17. data/lib/axlsx/drawing/pie_3D_chart.rb +2 -2
  18. data/lib/axlsx/drawing/pie_series.rb +1 -1
  19. data/lib/axlsx/drawing/scaling.rb +5 -5
  20. data/lib/axlsx/drawing/ser_axis.rb +3 -3
  21. data/lib/axlsx/drawing/series.rb +3 -3
  22. data/lib/axlsx/drawing/series_title.rb +7 -7
  23. data/lib/axlsx/drawing/title.rb +13 -9
  24. data/lib/axlsx/drawing/two_cell_anchor.rb +4 -4
  25. data/lib/axlsx/drawing/val_axis.rb +2 -2
  26. data/lib/axlsx/drawing/val_axis_data.rb +7 -9
  27. data/lib/axlsx/drawing/view_3D.rb +7 -7
  28. data/lib/axlsx/package.rb +8 -0
  29. data/lib/axlsx/util/constants.rb +0 -4
  30. data/lib/axlsx/util/ms_off_crypto.rb +88 -0
  31. data/lib/axlsx/util/ms_off_crypto.rb~ +3 -0
  32. data/lib/axlsx/util/ms_offcrypto.rb~ +0 -0
  33. data/lib/axlsx/version.rb +1 -1
  34. data/lib/axlsx/workbook/workbook.rb +1 -0
  35. data/lib/axlsx/workbook/worksheet/cell.rb +21 -8
  36. data/lib/axlsx/workbook/worksheet/worksheet.rb +74 -18
  37. data/test/content_type/tc_content_type.rb +81 -0
  38. data/test/content_type/tc_default.rb +40 -0
  39. data/test/content_type/tc_override.rb +40 -0
  40. data/test/doc_props/tc_app.rb +19 -0
  41. data/test/doc_props/tc_core.rb +34 -0
  42. data/test/drawing/tc_axis.rb +40 -0
  43. data/test/drawing/tc_bar_3D_chart.rb +66 -0
  44. data/test/drawing/tc_bar_series.rb +34 -0
  45. data/test/drawing/tc_cat_axis.rb +32 -0
  46. data/test/drawing/tc_cat_axis_data.rb +18 -0
  47. data/test/drawing/tc_chart.rb +73 -0
  48. data/test/drawing/tc_drawing.rb +80 -0
  49. data/test/drawing/tc_graphic_frame.rb +26 -0
  50. data/test/drawing/tc_line_3d_chart.rb +48 -0
  51. data/test/drawing/tc_line_series.rb +27 -0
  52. data/test/drawing/tc_line_series.tc~ +34 -0
  53. data/test/drawing/tc_marker.rb +45 -0
  54. data/test/drawing/tc_one_cell_anchor.rb +67 -0
  55. data/test/drawing/tc_pic.rb +71 -0
  56. data/test/drawing/tc_picture_locking.rb +73 -0
  57. data/test/drawing/tc_picture_locking.rb~ +77 -0
  58. data/test/drawing/tc_pie_3D_chart.rb +33 -0
  59. data/test/drawing/tc_pie_series.rb +35 -0
  60. data/test/drawing/tc_scaling.rb +37 -0
  61. data/test/drawing/tc_ser_axis.rb +31 -0
  62. data/test/drawing/tc_series.rb +24 -0
  63. data/test/drawing/tc_series_title.rb +34 -0
  64. data/test/drawing/tc_title.rb +34 -0
  65. data/test/drawing/tc_two_cell_anchor.rb +38 -0
  66. data/test/drawing/tc_val_axis.rb +25 -0
  67. data/test/drawing/tc_val_axis_data.rb +18 -0
  68. data/test/drawing/tc_view_3D.rb +55 -0
  69. data/test/rels/tc_relationship.rb +16 -0
  70. data/test/rels/tc_relationships.rb +27 -0
  71. data/test/stylesheet/tc_border.rb +38 -0
  72. data/test/stylesheet/tc_border_pr.rb +33 -0
  73. data/test/stylesheet/tc_cell_alignment.rb +77 -0
  74. data/test/stylesheet/tc_cell_protection.rb +30 -0
  75. data/test/stylesheet/tc_cell_style.rb +58 -0
  76. data/test/stylesheet/tc_color.rb +38 -0
  77. data/test/stylesheet/tc_fill.rb +19 -0
  78. data/test/stylesheet/tc_font.rb +114 -0
  79. data/test/stylesheet/tc_gradient_fill.rb +65 -0
  80. data/test/stylesheet/tc_gradient_stop.rb +32 -0
  81. data/test/stylesheet/tc_num_fmt.rb +31 -0
  82. data/test/stylesheet/tc_pattern_fill.rb +38 -0
  83. data/test/stylesheet/tc_styles.rb +52 -0
  84. data/test/stylesheet/tc_table_style.rb +37 -0
  85. data/test/stylesheet/tc_table_style_element.rb +37 -0
  86. data/test/stylesheet/tc_table_styles.rb +30 -0
  87. data/test/stylesheet/tc_xf.rb +121 -0
  88. data/test/tc_package.rb +78 -0
  89. data/test/util/tc_simple_typed_list.rb +66 -0
  90. data/test/util/tc_validators.rb +76 -0
  91. data/test/workbook/tc_workbook.rb +60 -0
  92. data/test/workbook/worksheet/tc_cell.rb +194 -0
  93. data/test/workbook/worksheet/tc_row.rb +36 -0
  94. data/test/workbook/worksheet/tc_worksheet.rb +159 -0
  95. metadata +191 -31
  96. data/lib/axlsx/workbook/#workbook.rb# +0 -165
data/README.md CHANGED
@@ -7,20 +7,23 @@ Axlsx: Office Open XML Spreadsheet Generation
7
7
  **Author**: Randy Morgan
8
8
  **Copyright**: 2011
9
9
  **License**: MIT License
10
- **Latest Version**: 1.0.13
10
+ **Latest Version**: 1.0.14
11
11
  **Ruby Version**: 1.8.7, 1.9.2, 1.9.3
12
12
 
13
- **Release Date**: December 8th 2011
13
+ **Release Date**: December 14th 2011
14
14
 
15
15
  Synopsis
16
16
  --------
17
17
 
18
18
  Axlsx is an Office Open XML Spreadsheet generator for the Ruby programming language.
19
- With Axlsx you can create worksheets with charts, images, automated column width, customizable styles and full schema validation. Axlsx excels at helping you generate beautiful Office Open XML Spreadsheet documents without having to understand the entire ECMA specification. Check out the README for some examples of how easy it is. Best of all, you can validate your xlsx file before serialization so you know for sure that anything generated is going to load on your client's machine.
19
+ With Axlsx you can create excel worksheets with charts, images, automated column width, customizable styles, functins, merged cells and auto filters as well as full schema validation. Axlsx excels at helping you generate beautiful Office Open XML Spreadsheet documents without having to understand the entire ECMA specification.
20
20
 
21
21
  If you are working in rails, or with active record see:
22
22
  http://github.com/randym/acts_as_xlsx
23
23
 
24
+ There are guides for using axlsx and acts_as_xlsx here:
25
+ [http://axlsx.blogspot.com](http://axlsx.blogspot.com)
26
+
24
27
  Help Wanted
25
28
  -----------
26
29
 
@@ -49,6 +52,10 @@ Feature List
49
52
 
50
53
  **10. Support for formulas
51
54
 
55
+ **11. Support for cell merging via worksheet.merged_cells
56
+
57
+ **12. Auto filtering tables with worksheet.auto_filter
58
+
52
59
  Installing
53
60
  ----------
54
61
 
@@ -56,196 +63,216 @@ To install Axlsx, use the following command:
56
63
 
57
64
  $ gem install axlsx
58
65
 
59
- Usage
60
- -----
66
+ #Usage
67
+ ------
61
68
 
62
- ###Examples
63
-
64
- require 'rubygems'
65
69
  require 'axlsx'
66
70
 
67
-
68
- Validation
69
-
70
71
  p = Axlsx::Package.new
71
- p.workbook.add_worksheet do |sheet|
72
- sheet.add_row ["First", "Second", "Third"]
73
- sheet.add_row [1, 2, 3]
74
- end
75
-
76
- p.validate.each do |error|
77
- puts error.inspect
78
- end
72
+ wb = p.workbook
79
73
 
80
- A Simple Workbook
74
+ ##A Simple Workbook
81
75
 
82
- p = Axlsx::Package.new
83
- p.workbook.add_worksheet do |sheet|
84
- sheet.add_row ["First Column", "Second Column", "Third Column"]
85
- sheet.add_row [1, 2, Time.now]
76
+ wb.add_worksheet(:name => "Basic Worksheet") do |sheet|
77
+ sheet.add_row ["First Column", "Second", "Third"]
78
+ sheet.add_row [1, 2, 3]
86
79
  end
87
- p.serialize("example1.xlsx")
88
80
 
89
- Generating A Bar Chart
81
+ ##Using Custom Styles
90
82
 
91
- p = Axlsx::Package.new
92
- p.workbook.add_worksheet do |sheet|
93
- sheet.add_row ["A Simple Bar Chart"]
94
- sheet.add_row ["First", "Second", "Third"]
95
- sheet.add_row [1, 2, 3]
96
- sheet.add_chart(Axlsx::Bar3DChart, :start_at => "A4", :end_at => "F17", :title=>sheet["A1"]) do |chart|
97
- chart.add_series :data => sheet["A3:C3"], :labels => sheet["A2:C2"]
83
+ wb.styles do |s|
84
+ black_cell = s.add_style :bg_color => "00", :fg_color => "FF", :sz => 14, :alignment => { :horizontal=> :center }
85
+ blue_cell = s.add_style :bg_color => "0000FF", :fg_color => "FF", :sz => 20, :alignment => { :horizontal=> :center }
86
+ wb.add_worksheet(:name => "Custom Styles") do |sheet|
87
+ sheet.add_row ["Text Autowidth", "Second", "Third"], :style => [black_cell, blue_cell, black_cell]
88
+ sheet.add_row [1, 2, 3], :style => Axlsx::STYLE_THIN_BORDER
98
89
  end
99
- end
100
- p.serialize("example2.xlsx")
90
+ end
101
91
 
102
- Generating A Pie Chart
92
+ ##Using Custom Formatting
103
93
 
104
- p = Axlsx::Package.new
105
- p.workbook.add_worksheet do |sheet|
106
- sheet.add_row ["First", "Second", "Third", "Fourth"]
107
- sheet.add_row [1, 2, 3, "=PRODUCT(A2:C2)"]
108
- sheet.add_chart(Axlsx::Pie3DChart, :start_at => [0, 2], :end_at => [5, 25], :title=>"example 3: Pie Chart") do |chart|
109
- chart.add_series :data => sheet["A2:D2"], :labels => sheet["A1:D1"]
94
+ wb.styles do |s|
95
+ date = s.add_style(:format_code => "yyyy-mm-dd", :border => Axlsx::STYLE_THIN_BORDER)
96
+ padded = s.add_style(:format_code => "00#", :border => Axlsx::STYLE_THIN_BORDER)
97
+ percent = s.add_style(:format_code => "0000%", :border => Axlsx::STYLE_THIN_BORDER)
98
+ wb.add_worksheet(:name => "Formatting Data") do |sheet|
99
+ sheet.add_row ["Custom Formatted Date", "Percent Formatted Float", "Padded Numbers"], :style => Axlsx::STYLE_THIN_BORDER
100
+ sheet.add_row [Time.now, 0.2, 32], :style => [date, percent, padded]
110
101
  end
111
- end
112
- p.serialize("example3.xlsx")
102
+ end
113
103
 
114
- Generating A Line Chart
104
+ ##Styling Columns
115
105
 
116
- p = Axlsx::Package.new
117
- p.workbook.add_worksheet do |sheet|
118
- sheet.add_row ["First", 1, 5, 7, 9]
119
- sheet.add_row ["Second", 5, 2, 14, 9]
120
- sheet.add_chart(Axlsx::Line3DChart, :title=>"example 6: Line Chart") do |chart|
121
- chart.start_at 0, 2
122
- chart.end_at 10, 15
123
- chart.add_series :data=>sheet["B1:E1"], :title=> sheet["A1"]
124
- chart.add_series :data=>sheet["B2:E2"], :title=> sheet["A2"]
106
+ wb.styles do |s|
107
+ percent = s.add_style :num_fmt => 9
108
+ wb.add_worksheet(:name => "Styling Columns") do |sheet|
109
+ sheet.add_row ['col 1', 'col 2', 'col 3', 'col 4']
110
+ sheet.add_row [1, 2, 0.3, 4]
111
+ sheet.add_row [1, 2, 0.2, 4]
112
+ sheet.add_row [1, 2, 0.1, 4]
113
+ sheet.col_style 2, percent, :row_offset => 1
125
114
  end
126
-
127
- end
128
- p.serialize("example4.xlsx")
129
-
130
- Using Custom Styles
131
-
132
- p = Axlsx::Package.new
133
- wb = p.workbook
134
- black_cell = wb.styles.add_style :bg_color => "00", :fg_color => "FF", :sz=>14, :alignment => { :horizontal=> :center }
135
- blue_cell = wb.styles.add_style :bg_color => "0000FF", :fg_color => "FF", :sz=>14, :alignment => { :horizontal=> :center }
136
- wb.add_worksheet do |sheet|
137
- sheet.add_row ["Text Autowidth", "Second", "Third"], :style => [black_cell, blue_cell, black_cell]
138
- sheet.add_row [1, 2, 3], :style => Axlsx::STYLE_THIN_BORDER
139
115
  end
140
- p.serialize("example5.xlsx")
141
-
142
- Using Custom Formatting and date1904
143
116
 
144
- p = Axlsx::Package.new
145
- wb = p.workbook
146
- date = wb.styles.add_style :format_code=>"yyyy-mm-dd", :border => Axlsx::STYLE_THIN_BORDER
147
- padded = wb.styles.add_style :format_code=>"00#", :border => Axlsx::STYLE_THIN_BORDER
148
- percent = wb.styles.add_style :format_code=>"0%", :border => Axlsx::STYLE_THIN_BORDER
149
- wb.date1904 = true # required for generation on mac
150
- wb.add_worksheet do |sheet|
151
- sheet.add_row ["Custom Formatted Date", "Percent Formatted Float", "Padded Numbers"], :style => Axlsx::STYLE_THIN_BORDER
152
- sheet.add_row [Time.now, 0.2, 32], :style => [date, percent, padded]
117
+ ##Styling Rows
118
+
119
+ wb.styles do |s|
120
+ head = s.add_style :bg_color => "00", :fg_color => "FF"
121
+ percent = s.add_style :num_fmt => 9
122
+ wb.add_worksheet(:name => "Styling Rows") do |sheet|
123
+ sheet.add_row ['col 1', 'col 2', 'col 3', 'col 4']
124
+ sheet.add_row [1, 2, 0.3, 4]
125
+ sheet.add_row [1, 2, 0.2, 4]
126
+ sheet.add_row [1, 2, 0.1, 4]
127
+ sheet.col_style 2, percent, :row_offset => 1
128
+ sheet.row_style 0, head
129
+ end
153
130
  end
154
- p.serialize("example6.xlsx")
155
131
 
132
+ ##Styling Cell Overrides
156
133
 
134
+ wb.add_worksheet(:name => "Cell Level Style Overrides") do |sheet|
135
+ # cell level style overides when adding cells
136
+ sheet.add_row ['col 1', 'col 2', 'col 3', 'col 4'], :sz => 16
137
+ sheet.add_row [1, 2, 3, "=SUM(A2:C2)"]
138
+ # cell level style overrides via sheet range
139
+ sheet["A1:D1"].each { |c| c.color = "FF0000"}
140
+ sheet['A1:D2'].each { |c| c.style = Axlsx::STYLE_THIN_BORDER }
141
+ end
157
142
 
158
- Add an Image
143
+ ##Add an Image
159
144
 
160
- p = Axlsx::Package.new
161
- p.workbook.add_worksheet do |sheet|
145
+ wb.add_worksheet(:name => "Images") do |sheet|
162
146
  img = File.expand_path('examples/image1.jpeg')
163
- sheet.add_image(:image_src => img, :noSelect=>true, :noMove=>true) do |image|
147
+ sheet.add_image(:image_src => img, :noSelect => true, :noMove => true) do |image|
164
148
  image.width=720
165
149
  image.height=666
166
150
  image.start_at 2, 2
167
151
  end
168
152
  end
169
- p.serialize("example8.xlsx")
170
153
 
171
- Asian Language Support
154
+ ##Asian Language Support
172
155
 
173
- p = Axlsx::Package.new
174
- p.workbook.add_worksheet do |sheet|
156
+ wb.add_worksheet(:name => "Unicode Support") do |sheet|
175
157
  sheet.add_row ["日本語"]
176
158
  sheet.add_row ["华语/華語"]
177
159
  sheet.add_row ["한국어/조선말"]
178
160
  end
179
- p.serialize("example9.xlsx")
180
161
 
181
- Styling Columns
182
162
 
183
- p = Axlsx::Package.new
184
- percent = p.workbook.styles.add_style :num_fmt => 9
185
- p.workbook.add_worksheet do |sheet|
186
- sheet.add_row ['col 1', 'col 2', 'col 3', 'col 4']
187
- sheet.add_row [1, 2, 0.3, 4]
188
- sheet.add_row [1, 2, 0.2, 4]
189
- sheet.add_row [1, 2, 0.1, 4]
190
- end
191
- p.workbook.worksheets.first.col_style 2, percent, :row_offset=>1
192
- p.serialize("example10.xlsx")
193
-
194
- Styling Rows
195
-
196
- p = Axlsx::Package.new
197
- p.workbook.add_worksheet do |sheet|
198
- sheet.add_row ['col 1', 'col 2', 'col 3', 'col 4']
199
- sheet.add_row [1, 2, 0.3, 4]
200
- sheet.add_row [1, 2, 0.2, 4]
201
- sheet.add_row [1, 2, 0.1, 4]
202
- end
203
- head = p.workbook.styles.add_style :bg_color => "00", :fg_color=>"FF"
204
- percent = p.workbook.styles.add_style :num_fmt => 9
205
- p.workbook.worksheets.first.col_style 2, percent, :row_offset=>1
206
- p.workbook.worksheets.first.row_style 0, head
207
- p.serialize("example11.xlsx")
208
-
209
- Using formula
163
+ ##Using formula
210
164
 
211
- p = Axlsx::Package.new
212
- p.workbook.add_worksheet do |sheet|
165
+ wb.add_worksheet(:name => "Using Formulas") do |sheet|
213
166
  sheet.add_row ['col 1', 'col 2', 'col 3', 'col 4']
214
167
  sheet.add_row [1, 2, 3, "=SUM(A2:C2)"]
215
168
  end
216
- p.serialize("example12.xlsx")
217
169
 
218
- Using cell specific styling and range / name based access
219
170
 
220
- p = Axlsx::Package.new
221
- p.workbook.add_worksheet(:name=>'My Worksheet') do |sheet|
171
+ ##Merging Cells
172
+
173
+ wb.add_worksheet(:name => 'Merging Cells') do |sheet|
222
174
  # cell level style overides when adding cells
223
- sheet.add_row ['col 1', 'col 2', 'col 3', 'col 4'], :sz => 16
175
+ sheet.add_row ["col 1", "col 2", "col 3", "col 4"], :sz => 16
224
176
  sheet.add_row [1, 2, 3, "=SUM(A2:C2)"]
225
- # cell level style overrides via sheet range
177
+ sheet.add_row [2, 3, 4, "=SUM(A3:C3)"]
178
+ sheet.add_row ["total", "", "", "=SUM(D2:D3)"]
179
+ sheet.merge_cells("A4:C4")
226
180
  sheet["A1:D1"].each { |c| c.color = "FF0000"}
181
+ sheet["A1:D4"].each { |c| c.style = Axlsx::STYLE_THIN_BORDER }
227
182
  end
228
- p.workbook['My Worksheet!A1:D2'].each { |c| c.style = Axlsx::STYLE_THIN_BORDER }
229
- p.serialize("example13.xlsx")
230
183
 
184
+ ##Generating A Bar Chart
231
185
 
186
+ wb.add_worksheet(:name => "Bar Chart") do |sheet|
187
+ sheet.add_row ["A Simple Bar Chart"]
188
+ sheet.add_row ["First", "Second", "Third"]
189
+ sheet.add_row [1, 2, 3]
190
+ sheet.add_chart(Axlsx::Bar3DChart, :start_at => "A4", :end_at => "F17") do |chart|
191
+ chart.add_series :data => sheet["A3:C3"], :labels => sheet["A2:C2"], :title => sheet["A1"]
192
+ end
193
+ end
232
194
 
233
- ###Documentation
195
+ ##Generating A Pie Chart
234
196
 
197
+ wb.add_worksheet(:name => "Pie Chart") do |sheet|
198
+ sheet.add_row ["First", "Second", "Third", "Fourth"]
199
+ sheet.add_row [1, 2, 3, "=PRODUCT(A2:C2)"]
200
+ sheet.add_chart(Axlsx::Pie3DChart, :start_at => [0,2], :end_at => [5, 15], :title => "example 3: Pie Chart") do |chart|
201
+ chart.add_series :data => sheet["A2:D2"], :labels => sheet["A1:D1"]
202
+ end
203
+ end
204
+
205
+ ##Data over time
206
+
207
+ wb.add_worksheet(:name=>'Charting Dates') do |sheet|
208
+ # cell level style overides when adding cells
209
+ sheet.add_row ['Date', 'Value'], :sz => 16
210
+ sheet.add_row [Time.now - (7*60*60*24), 3]
211
+ sheet.add_row [Time.now - (6*60*60*24), 7]
212
+ sheet.add_row [Time.now - (5*60*60*24), 18]
213
+ sheet.add_row [Time.now - (4*60*60*24), 1]
214
+ sheet.add_chart(Axlsx::Bar3DChart) do |chart|
215
+ chart.start_at "B7"
216
+ chart.end_at "H27"
217
+ chart.add_series(:data => sheet["B2:B5"], :labels => sheet["A2:A5"], :title => sheet["B1"])
218
+ end
219
+ end
220
+
221
+ ##Generating A Line Chart
222
+
223
+ wb.add_worksheet(:name => "Line Chart") do |sheet|
224
+ sheet.add_row ["First", 1, 5, 7, 9]
225
+ sheet.add_row ["Second", 5, 2, 14, 9]
226
+ sheet.add_chart(Axlsx::Line3DChart, :title => "example 6: Line Chart", :rotX => 30, :rotY => 20) do |chart|
227
+ chart.start_at 0, 2
228
+ chart.end_at 10, 15
229
+ chart.add_series :data => sheet["B1:E1"], :title => sheet["A1"]
230
+ chart.add_series :data => sheet["B2:E2"], :title => sheet["A2"]
231
+ end
232
+ end
233
+
234
+ ##Auto Filter
235
+
236
+ wb.add_worksheet(:name => "Auto Filter") do |sheet|
237
+ sheet.add_row ["Build Matrix"]
238
+ sheet.add_row ["Build", "Duration", "Finished", "Rvm"]
239
+ sheet.add_row ["19.1", "1 min 32 sec", "about 10 hours ago", "1.8.7"]
240
+ sheet.add_row ["19.2", "1 min 28 sec", "about 10 hours ago", "1.9.2"]
241
+ sheet.add_row ["19.3", "1 min 35 sec", "about 10 hours ago", "1.9.3"]
242
+ sheet.auto_filter = "A2:D5"
243
+ end
244
+
245
+ ##Validate and Serialize
246
+
247
+ p.validate.each { |e| puts e.message }
248
+ p.serialize("example.xlsx")
249
+
250
+
251
+ #Documentation
252
+ --------------
235
253
  This gem is 100% documented with YARD, an exceptional documentation library. To see documentation for this, and all the gems installed on your system use:
236
254
 
237
255
  gem install yard
238
256
  yard server -g
239
257
 
240
258
 
241
- ###Specs
242
-
259
+ #Specs
260
+ ------
243
261
  This gem has 100% test coverage using test/unit. To execute tests for this gem, simply run rake in the gem directory.
244
262
 
245
- Changelog
263
+ #Changelog
246
264
  ---------
265
+ - **December.14.11**: 1.0.14 release
266
+ - Added support for mergin cells
267
+ - Added support for auto filters
268
+ - Improved auto width calculations
269
+ - Improved charts
270
+ - Updated examples to output to a single workbook with multiple sheets
271
+ - Added access to app and core package objects so you can set the creator and other properties of the package
272
+ - The beginning of password protected xlsx files - roadmapped for January release.
273
+
247
274
  - **December.8.11**: 1.0.13 release
248
- Fixing .gemspec errors that caused gem to miss the lib directory. Sorry about that.
275
+ - Fixing .gemspec errors that caused gem to miss the lib directory. Sorry about that.
249
276
 
250
277
  - **December.7.11**: 1.0.12 release
251
278
  DO NOT USE THIS VERSION = THE GEM IS BROKEN
@@ -262,8 +289,8 @@ Changelog
262
289
  Please see the {file:CHANGELOG.md} document for past release information.
263
290
 
264
291
 
265
- Copyright
266
- ---------
292
+ #Copyright and License
293
+ ----------
267
294
 
268
295
  Axlsx © 2011 by [Randy Morgan](mailto:digial.ipseity@gmail.com). Axlsx is
269
296
  licensed under the MIT license. Please see the {file:LICENSE} document for more information.
@@ -1,204 +1,184 @@
1
1
  #!/usr/bin/env ruby
2
2
  # -*- coding: utf-8 -*-
3
- # require 'rubygems' if that is how you do it!
4
- require 'axlsx'
3
+ require 'axlsx'
5
4
 
5
+ p = Axlsx::Package.new
6
+ wb = p.workbook
6
7
 
7
8
  #A Simple Workbook
8
- if ARGV.size == 0 || ARGV.include?("1")
9
- p = Axlsx::Package.new
10
- p.workbook.add_worksheet do |sheet|
9
+
10
+ wb.add_worksheet(:name => "Basic Worksheet") do |sheet|
11
11
  sheet.add_row ["First Column", "Second", "Third"]
12
12
  sheet.add_row [1, 2, 3]
13
13
  end
14
- #p.workbook.date1904 = true
15
- p.validate.each { |e| puts e.message }
16
- p.serialize("example1.xlsx")
17
- end
18
- #Generating A Bar Chart
19
- if ARGV.size==0 || ARGV.include?("2")
20
-
21
- p = Axlsx::Package.new
22
- p.workbook.add_worksheet do |sheet|
23
- sheet.add_row ["A Simple Bar Chart"]
24
- sheet.add_row ["First", "Second", "Third"]
25
- sheet.add_row [1, 2, 3]
26
- sheet.add_chart(Axlsx::Bar3DChart, :start_at => "A4", :end_at => "F17", :title=>sheet["A1"]) do |chart|
27
- chart.add_series :data => sheet["A3:C3"], :labels => sheet["A2:C2"]
28
- end
29
- end
30
- p.serialize("example2.xlsx")
31
-
32
- end
33
- #Generating A Pie Chart
34
- if ARGV.size==0 || ARGV.include?("3")
35
-
36
- p = Axlsx::Package.new
37
- p.workbook.add_worksheet do |sheet|
38
- sheet.add_row ["First", "Second", "Third", "Fourth"]
39
- sheet.add_row [1, 2, 3, "=PRODUCT(A2:C2)"]
40
- sheet.add_chart(Axlsx::Pie3DChart, :start_at => [0,2], :end_at => [5, 15], :title=>"example 3: Pie Chart") do |chart|
41
- chart.add_series :data => sheet["A2:D2"], :labels => sheet["A1:D1"]
42
- end
43
- end
44
- p.serialize("example3.xlsx")
45
-
46
- end
47
14
 
48
15
  #Using Custom Styles
49
- if ARGV.size==0 || ARGV.include?("4")
50
-
51
- p = Axlsx::Package.new
52
- wb = p.workbook
53
- black_cell = wb.styles.add_style :bg_color => "00", :fg_color => "FF", :sz=>14, :alignment => { :horizontal=> :center }
54
- blue_cell = wb.styles.add_style :bg_color => "0000FF", :fg_color => "FF", :sz=>14, :alignment => { :horizontal=> :center }
55
- wb.add_worksheet do |sheet|
56
- sheet.add_row ["Text Autowidth", "Second", "Third"], :style => [black_cell, blue_cell, black_cell]
57
- sheet.add_row [1, 2, 3], :style => Axlsx::STYLE_THIN_BORDER
58
- end
59
- p.serialize("example4.xlsx")
60
-
61
- end
62
- #Using Custom Formatting and date1904
63
- if ARGV.size==0 || ARGV.include?("5")
64
16
 
65
- p = Axlsx::Package.new
66
- wb = p.workbook
67
- date = wb.styles.add_style :format_code=>"yyyy-mm-dd", :border => Axlsx::STYLE_THIN_BORDER
68
- padded = wb.styles.add_style :format_code=>"00#", :border => Axlsx::STYLE_THIN_BORDER
69
- percent = wb.styles.add_style :format_code=>"0%", :border => Axlsx::STYLE_THIN_BORDER
70
- wb.date1904 = true # required for generation on mac
71
- wb.add_worksheet do |sheet|
72
- sheet.add_row ["Custom Formatted Date", "Percent Formatted Float", "Padded Numbers"], :style => Axlsx::STYLE_THIN_BORDER
73
- sheet.add_row [Time.now, 0.2, 32], :style => [date, percent, padded]
74
- end
75
- p.serialize("example5.xlsx")
76
- end
77
- #Validation
78
- if ARGV.size==0 || ARGV.include?("6")
79
-
80
- p = Axlsx::Package.new
81
- p.workbook.add_worksheet do |sheet|
82
- sheet.add_row ["First", "Second", "Third"]
83
- sheet.add_row [1, 2, 3]
84
- end
85
-
86
- p.validate.each do |error|
87
- puts error.inspect
17
+ wb.styles do |s|
18
+ black_cell = s.add_style :bg_color => "00", :fg_color => "FF", :sz => 14, :alignment => { :horizontal=> :center }
19
+ blue_cell = s.add_style :bg_color => "0000FF", :fg_color => "FF", :sz => 20, :alignment => { :horizontal=> :center }
20
+ wb.add_worksheet(:name => "Custom Styles") do |sheet|
21
+ sheet.add_row ["Text Autowidth", "Second", "Third"], :style => [black_cell, blue_cell, black_cell]
22
+ sheet.add_row [1, 2, 3], :style => Axlsx::STYLE_THIN_BORDER
23
+ end
88
24
  end
89
25
 
90
- end
91
- #Generating A Line Chart
92
- if ARGV.size==0 || ARGV.include?("7")
26
+ ##Using Custom Formatting and date1904
93
27
 
94
- p = Axlsx::Package.new
95
- p.workbook.add_worksheet do |sheet|
96
- sheet.add_row ["First", 1, 5, 7, 9]
97
- sheet.add_row ["Second", 5, 2, 14, 9]
98
- sheet.add_chart(Axlsx::Line3DChart, :title=>"example 6: Line Chart") do |chart|
99
- chart.start_at 0, 2
100
- chart.end_at 10, 15
101
- chart.add_series :data=>["B1:E1"], :title=> sheet["A1"]
102
- chart.add_series :data=>sheet.rows.last.cells[(1..-1)], :title=> sheet.rows.last.cells.first
28
+ wb.styles do |s|
29
+ date = s.add_style(:format_code => "yyyy-mm-dd", :border => Axlsx::STYLE_THIN_BORDER)
30
+ padded = s.add_style(:format_code => "00#", :border => Axlsx::STYLE_THIN_BORDER)
31
+ percent = s.add_style(:format_code => "0000%", :border => Axlsx::STYLE_THIN_BORDER)
32
+ wb.date1904 = true # required for generation on mac
33
+ wb.add_worksheet(:name => "Formatting Data") do |sheet|
34
+ sheet.add_row ["Custom Formatted Date", "Percent Formatted Float", "Padded Numbers"], :style => Axlsx::STYLE_THIN_BORDER
35
+ sheet.add_row [Time.now, 0.2, 32], :style => [date, percent, padded]
103
36
  end
104
-
105
- end
106
- p.serialize("example7.xlsx")
107
-
108
- end
37
+ end
109
38
 
110
- #Add an Image
111
- if ARGV.size==0 || ARGV.include?("8")
39
+ ##Add an Image
112
40
 
113
- p = Axlsx::Package.new
114
- p.workbook.add_worksheet do |sheet|
41
+ wb.add_worksheet(:name => "Images") do |sheet|
115
42
  img = File.expand_path('examples/image1.jpeg')
116
- sheet.add_image(:image_src => img, :noSelect=>true, :noMove=>true) do |image|
43
+ sheet.add_image(:image_src => img, :noSelect => true, :noMove => true) do |image|
117
44
  image.width=720
118
45
  image.height=666
119
46
  image.start_at 2, 2
120
47
  end
121
48
  end
122
- p.serialize("example8.xlsx")
123
-
124
- end
125
49
 
126
- #Asian Language Support
127
- if ARGV.size==0 || ARGV.include?("9")
50
+ ##Asian Language Support
128
51
 
129
- p = Axlsx::Package.new
130
- p.workbook.add_worksheet do |sheet|
52
+ wb.add_worksheet(:name => "Unicode Support") do |sheet|
131
53
  sheet.add_row ["日本語"]
132
54
  sheet.add_row ["华语/華語"]
133
55
  sheet.add_row ["한국어/조선말"]
134
56
  end
135
- p.serialize("example9.xlsx")
136
57
 
137
- end
58
+ ##Styling Columns
138
59
 
60
+ wb.styles do |s|
61
+ percent = s.add_style :num_fmt => 9
62
+ wb.add_worksheet(:name => "Styling Columns") do |sheet|
63
+ sheet.add_row ['col 1', 'col 2', 'col 3', 'col 4']
64
+ sheet.add_row [1, 2, 0.3, 4]
65
+ sheet.add_row [1, 2, 0.2, 4]
66
+ sheet.add_row [1, 2, 0.1, 4]
67
+ sheet.col_style 2, percent, :row_offset => 1
68
+ end
69
+ end
139
70
 
140
- #Styling Columns
141
- if ARGV.size==0 || ARGV.include?("10")
142
-
143
- p = Axlsx::Package.new
144
- percent = p.workbook.styles.add_style :num_fmt => 9
145
- p.workbook.add_worksheet do |sheet|
146
- sheet.add_row ['col 1', 'col 2', 'col 3', 'col 4']
147
- sheet.add_row [1, 2, 0.3, 4]
148
- sheet.add_row [1, 2, 0.2, 4]
149
- sheet.add_row [1, 2, 0.1, 4]
71
+ ##Styling Rows
72
+
73
+ wb.styles do |s|
74
+ head = s.add_style :bg_color => "00", :fg_color => "FF"
75
+ percent = s.add_style :num_fmt => 9
76
+ wb.add_worksheet(:name => "Styling Rows") do |sheet|
77
+ sheet.add_row ['col 1', 'col 2', 'col 3', 'col 4']
78
+ sheet.add_row [1, 2, 0.3, 4]
79
+ sheet.add_row [1, 2, 0.2, 4]
80
+ sheet.add_row [1, 2, 0.1, 4]
81
+ sheet.col_style 2, percent, :row_offset => 1
82
+ sheet.row_style 0, head
83
+ end
150
84
  end
151
- p.workbook.worksheets.first.col_style 2, percent, :row_offset=>1
152
- p.serialize("example10.xlsx")
153
85
 
154
- end
86
+ ##Styling Cell Overrides
87
+
88
+ wb.add_worksheet(:name => "Cell Level Style Overrides") do |sheet|
89
+ # cell level style overides when adding cells
90
+ sheet.add_row ['col 1', 'col 2', 'col 3', 'col 4'], :sz => 16
91
+ sheet.add_row [1, 2, 3, "=SUM(A2:C2)"]
92
+ # cell level style overrides via sheet range
93
+ sheet["A1:D1"].each { |c| c.color = "FF0000"}
94
+ sheet['A1:D2'].each { |c| c.style = Axlsx::STYLE_THIN_BORDER }
95
+ end
155
96
 
156
- #Styling Rows
157
- if ARGV.size==0 || ARGV.include?("11")
97
+ ##Using formula
158
98
 
159
- p = Axlsx::Package.new
160
- p.workbook.add_worksheet do |sheet|
99
+ wb.add_worksheet(:name => "Using Formulas") do |sheet|
161
100
  sheet.add_row ['col 1', 'col 2', 'col 3', 'col 4']
162
- sheet.add_row [1, 2, 0.3, 4]
163
- sheet.add_row [1, 2, 0.2, 4]
164
- sheet.add_row [1, 2, 0.1, 4]
101
+ sheet.add_row [1, 2, 3, "=SUM(A2:C2)"]
165
102
  end
166
- head = p.workbook.styles.add_style :bg_color => "00", :fg_color=>"FF"
167
- percent = p.workbook.styles.add_style :num_fmt => 9
168
- p.workbook.worksheets.first.col_style 2, percent, :row_offset=>1
169
- p.workbook.worksheets.first.row_style 0, head
170
- p.serialize("example11.xlsx")
171
103
 
172
- end
104
+ ##Merging Cells.
173
105
 
174
- #Using formula
175
- if ARGV.size==0 || ARGV.include?("12")
106
+ wb.add_worksheet(:name => 'Merging Cells') do |sheet|
107
+ # cell level style overides when adding cells
108
+ sheet.add_row ["col 1", "col 2", "col 3", "col 4"], :sz => 16
109
+ sheet.add_row [1, 2, 3, "=SUM(A2:C2)"]
110
+ sheet.add_row [2, 3, 4, "=SUM(A3:C3)"]
111
+ sheet.add_row ["total", "", "", "=SUM(D2:D3)"]
112
+ sheet.merge_cells("A4:C4")
113
+ sheet["A1:D1"].each { |c| c.color = "FF0000"}
114
+ sheet["A1:D4"].each { |c| c.style = Axlsx::STYLE_THIN_BORDER }
115
+ end
176
116
 
177
- p = Axlsx::Package.new
178
- p.workbook.add_worksheet do |sheet|
179
- sheet.add_row ['col 1', 'col 2', 'col 3', 'col 4']
180
- sheet.add_row [1, 2, 3, "=SUM(A2:C2)"]
181
- end
182
- p.serialize("example12.xlsx")
117
+ ##Generating A Bar Chart
183
118
 
184
- end
119
+ wb.add_worksheet(:name => "Bar Chart") do |sheet|
120
+ sheet.add_row ["A Simple Bar Chart"]
121
+ sheet.add_row ["First", "Second", "Third"]
122
+ sheet.add_row [1, 2, 3]
123
+ sheet.add_chart(Axlsx::Bar3DChart, :start_at => "A4", :end_at => "F17") do |chart|
124
+ chart.add_series :data => sheet["A3:C3"], :labels => sheet["A2:C2"], :title => sheet["A1"]
125
+ end
126
+ end
127
+
128
+ ##Generating A Pie Chart
185
129
 
130
+ wb.add_worksheet(:name => "Pie Chart") do |sheet|
131
+ sheet.add_row ["First", "Second", "Third", "Fourth"]
132
+ sheet.add_row [1, 2, 3, "=PRODUCT(A2:C2)"]
133
+ sheet.add_chart(Axlsx::Pie3DChart, :start_at => [0,2], :end_at => [5, 15], :title => "example 3: Pie Chart") do |chart|
134
+ chart.add_series :data => sheet["A2:D2"], :labels => sheet["A1:D1"]
135
+ end
136
+ end
186
137
 
187
- #Using cell specific styling and range / name based access
188
- if ARGV.size==0 || ARGV.include?("13")
138
+ ##Data over time
189
139
 
190
- p = Axlsx::Package.new
191
- p.workbook.add_worksheet(:name=>'My Worksheet') do |sheet|
140
+ wb.add_worksheet(:name=>'Charting Dates') do |sheet|
192
141
  # cell level style overides when adding cells
193
- sheet.add_row ['col 1', 'col 2', 'col 3', 'col 4'], :sz => 16
194
- sheet.add_row [1, 2, 3, "=SUM(A2:C2)"]
195
- # cell level style overrides via sheet range
196
- sheet["A1:D1"].each { |c| c.color = "FF0000"}
142
+ sheet.add_row ['Date', 'Value'], :sz => 16
143
+ sheet.add_row [Time.now - (7*60*60*24), 3]
144
+ sheet.add_row [Time.now - (6*60*60*24), 7]
145
+ sheet.add_row [Time.now - (5*60*60*24), 18]
146
+ sheet.add_row [Time.now - (4*60*60*24), 1]
147
+ sheet.add_chart(Axlsx::Bar3DChart) do |chart|
148
+ chart.start_at "B7"
149
+ chart.end_at "H27"
150
+ chart.add_series(:data => sheet["B2:B5"], :labels => sheet["A2:A5"], :title => sheet["B1"])
151
+ end
197
152
  end
198
- p.workbook['My Worksheet!A1:D2'].each { |c| c.style = Axlsx::STYLE_THIN_BORDER }
199
- p.serialize("example13.xlsx")
200
153
 
201
- end
154
+ ##Generating A Line Chart
155
+
156
+ wb.add_worksheet(:name => "Line Chart") do |sheet|
157
+ sheet.add_row ["First", 1, 5, 7, 9]
158
+ sheet.add_row ["Second", 5, 2, 14, 9]
159
+ sheet.add_chart(Axlsx::Line3DChart, :title => "example 6: Line Chart", :rotX => 30, :rotY => 20) do |chart|
160
+ chart.start_at 0, 2
161
+ chart.end_at 10, 15
162
+ chart.add_series :data => sheet["B1:E1"], :title => sheet["A1"]
163
+ chart.add_series :data => sheet["B2:E2"], :title => sheet["A2"]
164
+ end
165
+ end
166
+
167
+ ##Auto Filter
168
+
169
+ wb.add_worksheet(:name => "Auto Filter") do |sheet|
170
+ sheet.add_row ["Build Matrix"]
171
+ sheet.add_row ["Build", "Duration", "Finished", "Rvm"]
172
+ sheet.add_row ["19.1", "1 min 32 sec", "about 10 hours ago", "1.8.7"]
173
+ sheet.add_row ["19.2", "1 min 28 sec", "about 10 hours ago", "1.9.2"]
174
+ sheet.add_row ["19.3", "1 min 35 sec", "about 10 hours ago", "1.9.3"]
175
+ sheet.auto_filter = "A2:D5"
176
+ end
177
+
178
+ ##Validate and Serialize
179
+
180
+ p.validate.each { |e| puts e.message }
181
+ p.serialize("example.xlsx")
202
182
 
203
183
 
204
184