axlsx 1.2.0 → 1.3.3

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.
Files changed (161) hide show
  1. data/.yardopts +3 -2
  2. data/CHANGELOG.md +34 -1
  3. data/README.md +47 -33
  4. data/Rakefile +1 -1
  5. data/examples/auto_filter.rb +16 -0
  6. data/examples/basic_charts.rb +4 -0
  7. data/examples/colored_links.rb +59 -0
  8. data/examples/doc/_index.html +88 -0
  9. data/examples/doc/class_list.html +53 -0
  10. data/examples/doc/css/common.css +1 -0
  11. data/examples/doc/css/full_list.css +57 -0
  12. data/examples/doc/css/style.css +328 -0
  13. data/examples/doc/file_list.html +52 -0
  14. data/examples/doc/frames.html +28 -0
  15. data/examples/doc/index.html +88 -0
  16. data/examples/doc/js/app.js +214 -0
  17. data/examples/doc/js/full_list.js +173 -0
  18. data/examples/doc/js/jquery.js +4 -0
  19. data/examples/doc/method_list.html +52 -0
  20. data/examples/doc/top-level-namespace.html +102 -0
  21. data/examples/example.rb +447 -257
  22. data/examples/finance.rb +82 -0
  23. data/examples/hyperlinks.rb +23 -0
  24. data/examples/page_setup.rb +11 -0
  25. data/examples/skydrive/axlsx.csv +1 -0
  26. data/examples/skydrive/axlsx.xlsx +0 -0
  27. data/examples/skydrive/real_example.rb +8 -8
  28. data/examples/sprk2012/Screen Shot 2012-09-11 at 10.42.06 PM.png +0 -0
  29. data/examples/sprk2012/Screen Shot 2012-09-11 at 11.07.48 PM.png +0 -0
  30. data/examples/sprk2012/Screen Shot 2012-09-11 at 8.31.50 PM.png +0 -0
  31. data/examples/sprk2012/Screen Shot 2012-09-11 at 9.23.27 PM.png +0 -0
  32. data/examples/sprk2012/Screen Shot 2012-09-11 at 9.32.06 PM.png +0 -0
  33. data/examples/sprk2012/Screen Shot 2012-09-11 at 9.33.35 PM.png +0 -0
  34. data/examples/sprk2012/Screen Shot 2012-09-11 at 9.46.44 PM.png +0 -0
  35. data/examples/sprk2012/Screen Shot 2012-09-12 at 5.07.23 PM.png +0 -0
  36. data/examples/sprk2012/basics.rb +11 -0
  37. data/examples/sprk2012/basics.xlsx +0 -0
  38. data/examples/sprk2012/gravatar.jpeg +0 -0
  39. data/examples/sprk2012/hair_club.jpg +0 -0
  40. data/examples/sprk2012/images.rb +9 -0
  41. data/examples/{example.xlsx → sprk2012/images.xlsx} +0 -0
  42. data/examples/sprk2012/line_chart.rb +56 -0
  43. data/examples/sprk2012/line_chart.xlsx +0 -0
  44. data/examples/sprk2012/sprk2012.key +0 -0
  45. data/examples/sprk2012/styles.rb +20 -0
  46. data/examples/sprk2012/styles.xlsx +0 -0
  47. data/examples/styles.rb +62 -0
  48. data/lib/axlsx/content_type/abstract_content_type.rb +32 -0
  49. data/lib/axlsx/content_type/content_type.rb +1 -1
  50. data/lib/axlsx/content_type/default.rb +6 -37
  51. data/lib/axlsx/content_type/override.rb +6 -38
  52. data/lib/axlsx/doc_props/app.rb +10 -7
  53. data/lib/axlsx/drawing/axis.rb +4 -4
  54. data/lib/axlsx/drawing/chart.rb +2 -3
  55. data/lib/axlsx/drawing/d_lbls.rb +21 -31
  56. data/lib/axlsx/drawing/drawing.rb +6 -0
  57. data/lib/axlsx/drawing/hyperlink.rb +40 -32
  58. data/lib/axlsx/drawing/marker.rb +13 -13
  59. data/lib/axlsx/drawing/num_data.rb +6 -6
  60. data/lib/axlsx/drawing/num_data_source.rb +17 -16
  61. data/lib/axlsx/drawing/one_cell_anchor.rb +20 -22
  62. data/lib/axlsx/drawing/pic.rb +25 -27
  63. data/lib/axlsx/drawing/picture_locking.rb +12 -44
  64. data/lib/axlsx/drawing/scaling.rb +13 -13
  65. data/lib/axlsx/drawing/scatter_chart.rb +3 -3
  66. data/lib/axlsx/drawing/series.rb +3 -6
  67. data/lib/axlsx/drawing/str_data.rb +3 -3
  68. data/lib/axlsx/drawing/str_val.rb +7 -8
  69. data/lib/axlsx/drawing/view_3D.rb +52 -38
  70. data/lib/axlsx/drawing/vml_shape.rb +23 -23
  71. data/lib/axlsx/package.rb +14 -17
  72. data/lib/axlsx/stylesheet/border.rb +29 -20
  73. data/lib/axlsx/stylesheet/border_pr.rb +5 -4
  74. data/lib/axlsx/stylesheet/cell_alignment.rb +55 -29
  75. data/lib/axlsx/stylesheet/cell_protection.rb +7 -4
  76. data/lib/axlsx/stylesheet/cell_style.rb +19 -14
  77. data/lib/axlsx/stylesheet/color.rb +19 -16
  78. data/lib/axlsx/stylesheet/dxf.rb +4 -4
  79. data/lib/axlsx/stylesheet/font.rb +22 -22
  80. data/lib/axlsx/stylesheet/gradient_fill.rb +45 -21
  81. data/lib/axlsx/stylesheet/num_fmt.rb +22 -13
  82. data/lib/axlsx/stylesheet/pattern_fill.rb +12 -21
  83. data/lib/axlsx/stylesheet/styles.rb +11 -2
  84. data/lib/axlsx/stylesheet/table_style.rb +17 -16
  85. data/lib/axlsx/stylesheet/table_style_element.rb +15 -11
  86. data/lib/axlsx/stylesheet/table_styles.rb +14 -11
  87. data/lib/axlsx/stylesheet/xf.rb +28 -26
  88. data/lib/axlsx/util/accessors.rb +49 -0
  89. data/lib/axlsx/util/constants.rb +105 -4
  90. data/lib/axlsx/util/options_parser.rb +15 -0
  91. data/lib/axlsx/util/serialized_attributes.rb +46 -0
  92. data/lib/axlsx/util/simple_typed_list.rb +16 -2
  93. data/lib/axlsx/util/validators.rb +33 -8
  94. data/lib/axlsx/version.rb +2 -2
  95. data/lib/axlsx/workbook/defined_name.rb +13 -58
  96. data/lib/axlsx/workbook/workbook.rb +33 -2
  97. data/lib/axlsx/workbook/worksheet/auto_filter/auto_filter.rb +77 -0
  98. data/lib/axlsx/workbook/worksheet/auto_filter/filter_column.rb +94 -0
  99. data/lib/axlsx/workbook/worksheet/auto_filter/filters.rb +246 -0
  100. data/lib/axlsx/workbook/worksheet/cell.rb +82 -45
  101. data/lib/axlsx/workbook/worksheet/cfvo.rb +15 -15
  102. data/lib/axlsx/workbook/worksheet/cfvos.rb +18 -0
  103. data/lib/axlsx/workbook/worksheet/col.rb +34 -27
  104. data/lib/axlsx/workbook/worksheet/color_scale.rb +7 -13
  105. data/lib/axlsx/workbook/worksheet/comment.rb +14 -11
  106. data/lib/axlsx/workbook/worksheet/conditional_formatting.rb +11 -11
  107. data/lib/axlsx/workbook/worksheet/conditional_formatting_rule.rb +27 -25
  108. data/lib/axlsx/workbook/worksheet/data_bar.rb +44 -34
  109. data/lib/axlsx/workbook/worksheet/data_validation.rb +61 -62
  110. data/lib/axlsx/workbook/worksheet/date_time_converter.rb +1 -1
  111. data/lib/axlsx/workbook/worksheet/dimension.rb +0 -1
  112. data/lib/axlsx/workbook/worksheet/icon_set.rb +20 -20
  113. data/lib/axlsx/workbook/worksheet/page_margins.rb +21 -19
  114. data/lib/axlsx/workbook/worksheet/page_set_up_pr.rb +44 -0
  115. data/lib/axlsx/workbook/worksheet/page_setup.rb +152 -23
  116. data/lib/axlsx/workbook/worksheet/pane.rb +48 -51
  117. data/lib/axlsx/workbook/worksheet/print_options.rb +8 -30
  118. data/lib/axlsx/workbook/worksheet/protected_range.rb +16 -13
  119. data/lib/axlsx/workbook/worksheet/row.rb +48 -81
  120. data/lib/axlsx/workbook/worksheet/selection.rb +30 -38
  121. data/lib/axlsx/workbook/worksheet/sheet_calc_pr.rb +29 -0
  122. data/lib/axlsx/workbook/worksheet/sheet_pr.rb +49 -4
  123. data/lib/axlsx/workbook/worksheet/sheet_protection.rb +51 -155
  124. data/lib/axlsx/workbook/worksheet/sheet_view.rb +68 -234
  125. data/lib/axlsx/workbook/worksheet/table.rb +22 -17
  126. data/lib/axlsx/workbook/worksheet/table_style_info.rb +51 -0
  127. data/lib/axlsx/workbook/worksheet/worksheet.rb +51 -14
  128. data/lib/axlsx/workbook/worksheet/worksheet_drawing.rb +1 -1
  129. data/lib/axlsx/workbook/worksheet/worksheet_hyperlink.rb +79 -0
  130. data/lib/axlsx/workbook/worksheet/worksheet_hyperlinks.rb +38 -0
  131. data/lib/axlsx.rb +19 -5
  132. data/test/content_type/tc_default.rb +0 -11
  133. data/test/content_type/tc_override.rb +0 -13
  134. data/test/drawing/tc_d_lbls.rb +14 -4
  135. data/test/stylesheet/tc_styles.rb +13 -0
  136. data/test/tc_axlsx.rb +20 -2
  137. data/test/tc_package.rb +1 -0
  138. data/test/util/tc_validators.rb +22 -1
  139. data/test/workbook/tc_defined_name.rb +2 -2
  140. data/test/workbook/tc_workbook.rb +15 -0
  141. data/test/workbook/worksheet/auto_filter/tc_auto_filter.rb +38 -0
  142. data/test/workbook/worksheet/auto_filter/tc_filter_column.rb +76 -0
  143. data/test/workbook/worksheet/auto_filter/tc_filters.rb +50 -0
  144. data/test/workbook/worksheet/tc_cell.rb +35 -5
  145. data/test/workbook/worksheet/tc_col.rb +11 -1
  146. data/test/workbook/worksheet/tc_page_set_up_pr.rb +15 -0
  147. data/test/workbook/worksheet/tc_page_setup.rb +6 -1
  148. data/test/workbook/worksheet/tc_pane.rb +5 -45
  149. data/test/workbook/worksheet/tc_row.rb +4 -4
  150. data/test/workbook/worksheet/tc_selection.rb +9 -48
  151. data/test/workbook/worksheet/tc_sheet_calc_pr.rb +18 -0
  152. data/test/workbook/worksheet/tc_sheet_pr.rb +27 -0
  153. data/test/workbook/worksheet/{table/tc_table.rb → tc_table.rb} +6 -1
  154. data/test/workbook/worksheet/tc_table_style_info.rb +53 -0
  155. data/test/workbook/worksheet/tc_worksheet.rb +17 -3
  156. data/test/workbook/worksheet/tc_worksheet_hyperlink.rb +64 -0
  157. metadata +74 -10
  158. data/examples/example_streamed.xlsx +0 -0
  159. data/examples/no-use_autowidth.xlsx +0 -0
  160. data/examples/shared_strings_example.xlsx +0 -0
  161. data/lib/axlsx/workbook/worksheet/auto_filter.rb +0 -34
data/examples/example.rb CHANGED
@@ -1,20 +1,57 @@
1
1
  #!/usr/bin/env ruby -w -s
2
2
  # -*- coding: utf-8 -*-
3
- # $LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../lib"
3
+ $LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../lib"
4
4
 
5
5
  #```ruby
6
6
  require 'axlsx'
7
+ examples = []
8
+ examples << :basic
9
+ examples << :custom_styles
10
+ examples << :cell_style_override
11
+ examples << :custom_borders
12
+ examples << :surrounding_border
13
+ examples << :deep_custom_borders
14
+ examples << :row_column_style
15
+ examples << :fixed_column_width
16
+ examples << :merge_cells
17
+ examples << :images
18
+ examples << :format_dates
19
+ examples << :mbcs
20
+ examples << :formula
21
+ examples << :auto_filter
22
+ examples << :data_types
23
+ examples << :hyperlinks
24
+ examples << :number_currency_format
25
+ examples << :bar_chart
26
+ examples << :chart_gridlines
27
+ examples << :pie_chart
28
+ examples << :line_chart
29
+ examples << :scatter_chart
30
+ examples << :tables
31
+ examples << :fit_to_page
32
+ examples << :hide_gridlines
33
+ examples << :repeated_header
34
+ examples << :defined_name
35
+ examples << :printing
36
+ examples << :comments
37
+ examples << :panes
38
+ examples << :conditional_formatting
39
+ examples << :streaming
40
+ examples << :shared_strings
41
+ examples << :no_autowidth
7
42
 
8
43
  p = Axlsx::Package.new
9
44
  wb = p.workbook
10
45
  #```
11
46
 
12
- #A Simple Workbook
47
+ ## A Simple Workbook
13
48
 
14
49
  #```ruby
15
- wb.add_worksheet(:name => "Basic Worksheet") do |sheet|
16
- sheet.add_row ["First Column", "Second", "Third"]
17
- sheet.add_row [1, 2, 3]
50
+ if examples.include? :basic
51
+ wb.add_worksheet(:name => "Basic Worksheet") do |sheet|
52
+ sheet.add_row ["First Column", "Second", "Third"]
53
+ sheet.add_row [1, 2, 3]
54
+ end
18
55
  end
19
56
  #```
20
57
 
@@ -24,17 +61,18 @@ end
24
61
  # Each cell allows a single, predified style.
25
62
  # When using add_row, the value in the :style array at the same index as the cell's column will be applied to that cell.
26
63
  # Alternatively, you can apply a style to an entire row by using an integer value for :style.
27
-
28
- wb.styles do |s|
29
- black_cell = s.add_style :bg_color => "00", :fg_color => "FF", :sz => 14, :alignment => { :horizontal=> :center }
30
- blue_cell = s.add_style :bg_color => "0000FF", :fg_color => "FF", :sz => 20, :alignment => { :horizontal=> :center }
31
- wb.add_worksheet(:name => "Custom Styles") do |sheet|
32
-
33
- # Applies the black_cell style to the first and third cell, and the blue_cell style to the second.
34
- sheet.add_row ["Text Autowidth", "Second", "Third"], :style => [black_cell, blue_cell, black_cell]
35
-
36
- # Applies the thin border to all three cells
37
- sheet.add_row [1, 2, 3], :style => Axlsx::STYLE_THIN_BORDER
64
+ if examples.include? :custom_styles
65
+ wb.styles do |s|
66
+ black_cell = s.add_style :bg_color => "00", :fg_color => "FF", :sz => 14, :alignment => { :horizontal=> :center }
67
+ blue_cell = s.add_style :bg_color => "0000FF", :fg_color => "FF", :sz => 20, :alignment => { :horizontal=> :center }
68
+ wb.add_worksheet(:name => "Custom Styles") do |sheet|
69
+
70
+ # Applies the black_cell style to the first and third cell, and the blue_cell style to the second.
71
+ sheet.add_row ["Text Autowidth", "Second", "Third"], :style => [black_cell, blue_cell, black_cell]
72
+
73
+ # Applies the thin border to all three cells
74
+ sheet.add_row [1, 2, 3], :style => Axlsx::STYLE_THIN_BORDER
75
+ end
38
76
  end
39
77
  end
40
78
  #```
@@ -43,104 +81,172 @@ end
43
81
 
44
82
  #```ruby
45
83
  #Some of the style attributes can also be set at the cell level. Cell level styles take precedence over Custom Styles shown in the previous example.
46
-
47
- wb.add_worksheet(:name => "Cell Level Style Overrides") do |sheet|
48
-
49
- # this will set the font size for each cell.
50
- sheet.add_row ['col 1', 'col 2', 'col 3', 'col 4'], :sz => 16
51
-
52
- sheet.add_row [1, 2, 3, "=SUM(A2:C2)"]
53
-
54
- # You can also apply cell style overrides to a range of cells
55
- sheet["A1:D1"].each { |c| c.color = "FF0000" }
56
- sheet['A1:D2'].each { |c| c.style = Axlsx::STYLE_THIN_BORDER }
84
+ if examples.include? :cell_style_override
85
+ wb.add_worksheet(:name => "Cell Level Style Overrides") do |sheet|
86
+
87
+ # this will set the font size for each cell.
88
+ sheet.add_row ['col 1', 'col 2', 'col 3', 'col 4'], :sz => 16
89
+
90
+ sheet.add_row [1, 2, 3, "=SUM(A2:C2)"]
91
+ sheet.add_row %w(u shadow sz b i strike outline)
92
+ sheet.rows.last.cells[0].u = :double
93
+ sheet.rows.last.cells[1].shadow = true
94
+ sheet.rows.last.cells[2].sz = 20
95
+ sheet.rows.last.cells[3].b = true
96
+ sheet.rows.last.cells[4].i = true
97
+ sheet.rows.last.cells[5].strike = true
98
+ sheet.rows.last.cells[6].outline = 1
99
+ # You can also apply cell style overrides to a range of cells
100
+ sheet["A1:D1"].each { |c| c.color = "FF0000" }
101
+ sheet['A1:D2'].each { |c| c.style = Axlsx::STYLE_THIN_BORDER }
102
+ end
57
103
  end
58
- #```
104
+ ##```
59
105
 
60
106
  ##Using Custom Border Styles
61
107
 
62
108
  #```ruby
63
109
  #Axlsx defines a thin border style, but you can easily create and use your own.
64
- wb.styles do |s|
65
- red_border = s.add_style :border => { :style => :thick, :color =>"FFFF0000" }
66
- blue_border = s.add_style :border => { :style => :thick, :color =>"FF0000FF" }
67
-
68
- wb.add_worksheet(:name => "Custom Borders") do |sheet|
69
- sheet.add_row ["wrap", "me", "Up in Red"], :style => red_border
70
- sheet.add_row [1, 2, 3], :style => blue_border
110
+ if examples.include? :custom_borders
111
+ wb.styles do |s|
112
+ red_border = s.add_style :border => { :style => :thick, :color =>"FFFF0000", :edges => [:left, :right] }
113
+ blue_border = s.add_style :border => { :style => :thick, :color =>"FF0000FF"}
114
+
115
+ wb.add_worksheet(:name => "Custom Borders") do |sheet|
116
+ sheet.add_row ["wrap", "me", "Up in Red"], :style => red_border
117
+ sheet.add_row [1, 2, 3], :style => blue_border
118
+ end
71
119
  end
72
120
  end
73
- #```
74
121
 
122
+ #```ruby
123
+ # More Custom Borders
124
+ if examples.include? :surrounding_border
125
+
126
+ # Stuff like this is why I LOVE RUBY
127
+ # If you dont know about hash default values
128
+ # LEARN IT! LIVE IT! LOVE IT!
129
+ defaults = { :style => :thick, :color => "000000" }
130
+ borders = Hash.new do |hash, key|
131
+ hash[key] = wb.styles.add_style :border => defaults.merge( { :edges => key.to_s.split('_').map(&:to_sym) } )
132
+ end
133
+ top_row = [0, borders[:top_left], borders[:top], borders[:top], borders[:top_right]]
134
+ middle_row = [0, borders[:left], nil, nil, borders[:right]]
135
+ bottom_row = [0, borders[:bottom_left], borders[:bottom], borders[:bottom], borders[:bottom_right]]
75
136
 
76
- ##Styling Rows and Columns
137
+ wb.add_worksheet(:name => "Surrounding Border") do |ws|
138
+ ws.add_row []
139
+ ws.add_row ['', 1,2,3,4], :style => top_row
140
+ ws.add_row ['', 5,6,7,8], :style => middle_row
141
+ ws.add_row ['', 9, 10, 11, 12]
77
142
 
78
- #```ruby
79
- wb.styles do |s|
80
- head = s.add_style :bg_color => "00", :fg_color => "FF"
81
- percent = s.add_style :num_fmt => 9
82
- wb.add_worksheet(:name => "Columns and Rows") do |sheet|
83
- sheet.add_row ['col 1', 'col 2', 'col 3', 'col 4', 'col5']
84
- sheet.add_row [1, 2, 0.3, 4, 5.0]
85
- sheet.add_row [1, 2, 0.2, 4, 5.0]
86
- sheet.add_row [1, 2, 0.1, 4, 5.0]
143
+ #This works too!
144
+ ws.rows.last.style = bottom_row
87
145
 
88
- #apply the percent style to the column at index 2 skipping the first row.
89
- sheet.col_style 2, percent, :row_offset => 1
146
+ end
147
+ end
90
148
 
91
- # apply the head style to the first row.
92
- sheet.row_style 0, head
149
+ #```ruby
150
+ # Hacking border styles
151
+ if examples.include? :deep_custom_borders
152
+ wb.styles do |s|
153
+ top_bottom = s.add_style :border => { :style => :thick, :color =>"FFFF0000", :edges => [:top, :bottom] }
154
+ border = s.borders[s.cellXfs[top_bottom].borderId]
155
+ # edit existing border parts
156
+ border.prs.each do |part|
157
+ case part.name
158
+ when :top
159
+ part.color = Axlsx::Color.new(:rgb => "FFFF0000")
160
+ when :bottom
161
+ part.color = Axlsx::Color.new(:rgb => "FF00FF00")
162
+ end
163
+ end
164
+
165
+ border.prs << Axlsx::BorderPr.new(:name => :left, :color => Axlsx::Color.new(:rgb => '0000FF'), :style => :mediumDashed)
166
+ wb.add_worksheet(:name => 'hacked borders') do |sheet|
167
+ sheet.add_row [1,2,3], :style=>top_bottom
168
+ end
169
+ end
170
+ end
171
+ ##```
93
172
 
94
- #Hide the 5th column
95
- sheet.column_info[4].hidden = true
96
173
 
97
- #Set the second column outline level
98
- sheet.column_info[1].outlineLevel = 2
174
+ ##Styling Rows and Columns
99
175
 
100
- sheet.rows[3].hidden = true
101
- sheet.rows[1].outlineLevel = 2
176
+ #```ruby
177
+ if examples.include? :row_column_style
178
+ wb.styles do |s|
179
+ head = s.add_style :bg_color => "00", :fg_color => "FF"
180
+ percent = s.add_style :num_fmt => 9
181
+ wb.add_worksheet(:name => "Columns and Rows") do |sheet|
182
+ sheet.add_row ['col 1', 'col 2', 'col 3', 'col 4', 'col5']
183
+ sheet.add_row [1, 2, 0.3, 4, 5.0]
184
+ sheet.add_row [1, 2, 0.2, 4, 5.0]
185
+ sheet.add_row [1, 2, 0.1, 4, 5.0]
186
+
187
+ #apply the percent style to the column at index 2 skipping the first row.
188
+ sheet.col_style 2, percent, :row_offset => 1
189
+
190
+ # apply the head style to the first row.
191
+ sheet.row_style 0, head
192
+
193
+ #Hide the 5th column
194
+ sheet.column_info[4].hidden = true
195
+
196
+ #Set the second column outline level
197
+ sheet.column_info[1].outlineLevel = 2
198
+
199
+ sheet.rows[3].hidden = true
200
+ sheet.rows[1].outlineLevel = 2
201
+ end
102
202
  end
103
203
  end
104
- #```
204
+ ##```
105
205
 
106
206
 
107
207
  ##Specifying Column Widths
108
208
 
109
209
  #```ruby
110
- wb.add_worksheet(:name => "custom column widths") do |sheet|
111
- sheet.add_row ["I use autowidth and am very wide", "I use a custom width and am narrow"]
112
- sheet.add_row ['abcdefg', 'This is a very long text and should flow into the right cell', nil, 'xxx' ]
113
- sheet.column_widths nil, 3, 5, nil
210
+ if examples.include? :fixed_column_width
211
+ wb.add_worksheet(:name => "custom column widths") do |sheet|
212
+ sheet.add_row ["I use autowidth and am very wide", "I use a custom width and am narrow"]
213
+ sheet.add_row ['abcdefg', 'This is a very long text and should flow into the right cell', nil, 'xxx' ]
214
+ sheet.column_widths nil, 3, 5, nil
215
+ end
114
216
  end
115
- #```
217
+ ##```
116
218
 
117
219
  ##Merging Cells.
118
220
 
119
221
  #```ruby
120
- wb.add_worksheet(:name => 'Merging Cells') do |sheet|
121
- # cell level style overides when adding cells
122
- sheet.add_row ["col 1", "col 2", "col 3", "col 4"], :sz => 16
123
- sheet.add_row [1, 2, 3, "=SUM(A2:C2)"]
124
- sheet.add_row [2, 3, 4, "=SUM(A3:C3)"]
125
- sheet.add_row ["total", "", "", "=SUM(D2:D3)"]
126
- sheet.merge_cells("A4:C4")
127
- sheet["A1:D1"].each { |c| c.color = "FF0000"}
128
- sheet["A1:D4"].each { |c| c.style = Axlsx::STYLE_THIN_BORDER }
222
+ if examples.include? :merge_cells
223
+ wb.add_worksheet(:name => 'Merging Cells') do |sheet|
224
+ # cell level style overides when adding cells
225
+ sheet.add_row ["col 1", "col 2", "col 3", "col 4"], :sz => 16
226
+ sheet.add_row [1, 2, 3, "=SUM(A2:C2)"]
227
+ sheet.add_row [2, 3, 4, "=SUM(A3:C3)"]
228
+ sheet.add_row ["total", "", "", "=SUM(D2:D3)"]
229
+ sheet.merge_cells("A4:C4")
230
+ sheet["A1:D1"].each { |c| c.color = "FF0000"}
231
+ sheet["A1:D4"].each { |c| c.style = Axlsx::STYLE_THIN_BORDER }
232
+ end
129
233
  end
130
- #```
234
+ ##```
131
235
 
132
236
  ##Add an Image with a hyperlink
133
237
 
134
238
  #```ruby
135
- wb.add_worksheet(:name => "Image with Hyperlink") do |sheet|
136
- img = File.expand_path('../image1.jpeg', __FILE__)
137
- # specifying the :hyperlink option will add a hyper link to your image.
138
- # @note - Numbers does not support this part of the specification.
139
- sheet.add_image(:image_src => img, :noSelect => true, :noMove => true, :hyperlink=>"http://axlsx.blogspot.com") do |image|
140
- image.width=720
141
- image.height=666
142
- image.hyperlink.tooltip = "Labeled Link"
143
- image.start_at 2, 2
239
+ if examples.include? :images
240
+ wb.add_worksheet(:name => "Image with Hyperlink") do |sheet|
241
+ img = File.expand_path('../image1.jpeg', __FILE__)
242
+ # specifying the :hyperlink option will add a hyper link to your image.
243
+ # @note - Numbers does not support this part of the specification.
244
+ sheet.add_image(:image_src => img, :noSelect => true, :noMove => true, :hyperlink=>"http://axlsx.blogspot.com") do |image|
245
+ image.width=720
246
+ image.height=666
247
+ image.hyperlink.tooltip = "Labeled Link"
248
+ image.start_at 2, 2
249
+ end
144
250
  end
145
251
  end
146
252
  #```
@@ -148,15 +254,17 @@ end
148
254
  ##Using Custom Formatting and date1904
149
255
 
150
256
  #```ruby
151
- require 'date'
152
- wb.styles do |s|
153
- date = s.add_style(:format_code => "yyyy-mm-dd", :border => Axlsx::STYLE_THIN_BORDER)
154
- padded = s.add_style(:format_code => "00#", :border => Axlsx::STYLE_THIN_BORDER)
155
- percent = s.add_style(:format_code => "0000%", :border => Axlsx::STYLE_THIN_BORDER)
156
- # wb.date1904 = true # Use the 1904 date system (Used by Excel for Mac < 2011)
157
- wb.add_worksheet(:name => "Formatting Data") do |sheet|
158
- sheet.add_row ["Custom Formatted Date", "Percent Formatted Float", "Padded Numbers"], :style => Axlsx::STYLE_THIN_BORDER
159
- sheet.add_row [Date::strptime('2012-01-19','%Y-%m-%d'), 0.2, 32], :style => [date, percent, padded]
257
+ if examples.include? :format_dates
258
+ require 'date'
259
+ wb.styles do |s|
260
+ date = s.add_style(:format_code => "yyyy-mm-dd", :border => Axlsx::STYLE_THIN_BORDER)
261
+ padded = s.add_style(:format_code => "00#", :border => Axlsx::STYLE_THIN_BORDER)
262
+ percent = s.add_style(:format_code => "0000%", :border => Axlsx::STYLE_THIN_BORDER)
263
+ # wb.date1904 = true # Use the 1904 date system (Used by Excel for Mac < 2011)
264
+ wb.add_worksheet(:name => "Formatting Data") do |sheet|
265
+ sheet.add_row ["Custom Formatted Date", "Percent Formatted Float", "Padded Numbers"], :style => Axlsx::STYLE_THIN_BORDER
266
+ sheet.add_row [Date::strptime('2012-01-19','%Y-%m-%d'), 0.2, 32], :style => [date, percent, padded]
267
+ end
160
268
  end
161
269
  end
162
270
  #```
@@ -164,69 +272,110 @@ end
164
272
  ##Asian Language Support
165
273
 
166
274
  #```ruby
167
- wb.add_worksheet(:name => "日本語でのシート名") do |sheet|
168
- sheet.add_row ["日本語"]
169
- sheet.add_row ["华语/華語"]
170
- sheet.add_row ["한국어/조선말"]
275
+ if examples.include? :mbcs
276
+ wb.add_worksheet(:name => "日本語でのシート名") do |sheet|
277
+ sheet.add_row ["日本語"]
278
+ sheet.add_row ["华语/華語"]
279
+ sheet.add_row ["한국어/조선말"]
280
+ end
171
281
  end
172
- #```
282
+ ##```
173
283
 
174
284
  ##Using formula
175
285
 
176
286
  #```ruby
177
- wb.add_worksheet(:name => "Using Formulas") do |sheet|
178
- sheet.add_row ['col 1', 'col 2', 'col 3', 'col 4']
179
- sheet.add_row [1, 2, 3, "=SUM(A2:C2)"]
287
+ if examples.include? :formula
288
+ wb.add_worksheet(:name => "Using Formulas") do |sheet|
289
+ sheet.add_row ['col 1', 'col 2', 'col 3', 'col 4']
290
+ sheet.add_row [1, 2, 3, "=SUM(A2:C2)"]
291
+ end
180
292
  end
181
- #```
293
+ ##```
182
294
 
183
295
  ##Auto Filter
184
296
 
185
297
  #```ruby
186
- wb.add_worksheet(:name => "Auto Filter") do |sheet|
187
- sheet.add_row ["Build Matrix"]
188
- sheet.add_row ["Build", "Duration", "Finished", "Rvm"]
189
- sheet.add_row ["19.1", "1 min 32 sec", "about 10 hours ago", "1.8.7"]
190
- sheet.add_row ["19.2", "1 min 28 sec", "about 10 hours ago", "1.9.2"]
191
- sheet.add_row ["19.3", "1 min 35 sec", "about 10 hours ago", "1.9.3"]
192
- sheet.auto_filter = "A2:D5"
298
+ if examples.include? :auto_filter
299
+ wb.add_worksheet(:name => "Auto Filter") do |sheet|
300
+ sheet.add_row ["Build Matrix"]
301
+ sheet.add_row ["Build", "Duration", "Finished", "Rvm"]
302
+ sheet.add_row ["19.1", "1 min 32 sec", "about 10 hours ago", "1.8.7"]
303
+ sheet.add_row ["19.2", "1 min 28 sec", "about 10 hours ago", "1.9.2"]
304
+ sheet.add_row ["19.3", "1 min 35 sec", "about 10 hours ago", "1.9.3"]
305
+ sheet.auto_filter = "A2:D5"
306
+ sheet.auto_filter.add_column 3, :filters, :filter_items => ['1.9.2', '1.8.7']
307
+ end
193
308
  end
194
309
  #```
195
310
 
196
311
  ##Automatic cell types
197
312
 
313
+
198
314
  #```ruby
199
- wb.add_worksheet(:name => "Automatic cell types") do |sheet|
200
- date_format = wb.styles.add_style :format_code => 'YYYY-MM-DD'
201
- time_format = wb.styles.add_style :format_code => 'hh:mm:ss'
202
- sheet.add_row ["Date", "Time", "String", "Boolean", "Float", "Integer"]
203
- sheet.add_row [Date.today, Time.now, "value", true, 0.1, 1], :style => [date_format, time_format]
315
+ if examples.include? :data_types
316
+ wb.add_worksheet(:name => "Automatic cell types") do |sheet|
317
+ date_format = wb.styles.add_style :format_code => 'YYYY-MM-DD'
318
+ time_format = wb.styles.add_style :format_code => 'hh:mm:ss'
319
+ sheet.add_row ["Date", "Time", "String", "Boolean", "Float", "Integer"]
320
+ sheet.add_row [Date.today, Time.now, "value", true, 0.1, 1], :style => [date_format, time_format]
321
+ end
204
322
  end
205
- #```
206
323
 
324
+ # Hyperlinks in worksheet
325
+ if examples.include? :hyperlinks
326
+ wb.add_worksheet(:name => 'hyperlinks') do |sheet|
327
+ # external references
328
+ sheet.add_row ['axlsx']
329
+ #sheet.add_hyperlink :location => 'https://github.com/randym/axlsx', :ref => sheet.rows.first.cells.first
330
+ # internal references
331
+ sheet.add_hyperlink :location => "'Next Sheet'!A1", :ref => 'A2', :target => :sheet
332
+ sheet.add_row ['next sheet']
333
+ end
207
334
 
335
+ wb.add_worksheet(:name => 'Next Sheet') do |sheet|
336
+ sheet.add_row ['hello!']
337
+ end
338
+ end
339
+ ###```
340
+
341
+ ##Number formatting and currency
342
+ if examples.include? :number_currency_format
343
+ wb.add_worksheet(:name => "Formats and Currency") do |sheet|
344
+ currency = wb.styles.add_style :num_fmt => 5
345
+ red_negative = wb.styles.add_style :num_fmt => 8
346
+ comma = wb.styles.add_style :num_fmt => 3
347
+ super_funk = wb.styles.add_style :format_code => '[Green]#'
348
+ sheet.add_row %w(Currency RedNegative Comma Custom)
349
+ sheet.add_row [1500, -122.34, 123456789, 594829], :style=> [currency, red_negative, comma, super_funk]
350
+ end
351
+ end
208
352
  ##Generating A Bar Chart
209
353
 
210
354
  #```ruby
211
- wb.add_worksheet(:name => "Bar Chart") do |sheet|
212
- sheet.add_row ["A Simple Bar Chart"]
213
- %w(first second third).each { |label| sheet.add_row [label, rand(24)+1] }
214
- sheet.add_chart(Axlsx::Bar3DChart, :start_at => "A6", :end_at => "F20") do |chart|
215
- chart.add_series :data => sheet["B2:B4"], :labels => sheet["A2:A4"], :title => sheet["A1"]
355
+ if examples.include? :bar_chart
356
+ wb.add_worksheet(:name => "Bar Chart") do |sheet|
357
+ sheet.add_row ["A Simple Bar Chart"]
358
+ %w(first second third).each { |label| sheet.add_row [label, rand(24)+1] }
359
+ sheet.add_chart(Axlsx::Bar3DChart, :start_at => "A6", :end_at => "F20") do |chart|
360
+ chart.add_series :data => sheet["B2:B4"], :labels => sheet["A2:A4"], :title => sheet["A1"]
361
+ end
216
362
  end
217
363
  end
218
- #```
364
+
365
+ ##```
219
366
 
220
367
  ##Hide Gridlines in chart
221
368
 
222
369
  #```ruby
223
- wb.add_worksheet(:name => "Chart With No Gridlines") do |sheet|
224
- sheet.add_row ["Bar Chart without gridlines"]
225
- %w(first second third).each { |label| sheet.add_row [label, rand(24)+1] }
226
- sheet.add_chart(Axlsx::Bar3DChart, :start_at => "A6", :end_at => "F20") do |chart|
227
- chart.add_series :data => sheet["B2:B4"], :labels => sheet["A2:A4"]
228
- chart.valAxis.gridlines = false
229
- chart.catAxis.gridlines = false
370
+ if examples.include? :chart_gridlines
371
+ wb.add_worksheet(:name => "Chart With No Gridlines") do |sheet|
372
+ sheet.add_row ["Bar Chart without gridlines"]
373
+ %w(first second third).each { |label| sheet.add_row [label, rand(24)+1] }
374
+ sheet.add_chart(Axlsx::Bar3DChart, :start_at => "A6", :end_at => "F20") do |chart|
375
+ chart.add_series :data => sheet["B2:B4"], :labels => sheet["A2:A4"]
376
+ chart.valAxis.gridlines = false
377
+ chart.catAxis.gridlines = false
378
+ end
230
379
  end
231
380
  end
232
381
  #```
@@ -234,11 +383,13 @@ end
234
383
  ##Generating A Pie Chart
235
384
 
236
385
  #```ruby
237
- wb.add_worksheet(:name => "Pie Chart") do |sheet|
238
- sheet.add_row ["Simple Pie Chart"]
239
- %w(first second third).each { |label| sheet.add_row [label, rand(24)+1] }
240
- sheet.add_chart(Axlsx::Pie3DChart, :start_at => [0,5], :end_at => [10, 20], :title => "example 3: Pie Chart") do |chart|
241
- chart.add_series :data => sheet["B2:B4"], :labels => sheet["A2:A4"], :colors => ['FF0000', '00FF00', '0000FF']
386
+ if examples.include? :pie_chart
387
+ wb.add_worksheet(:name => "Pie Chart") do |sheet|
388
+ sheet.add_row ["Simple Pie Chart"]
389
+ %w(first second third).each { |label| sheet.add_row [label, rand(24)+1] }
390
+ sheet.add_chart(Axlsx::Pie3DChart, :start_at => [0,5], :end_at => [10, 20], :title => "example 3: Pie Chart") do |chart|
391
+ chart.add_series :data => sheet["B2:B4"], :labels => sheet["A2:A4"], :colors => ['FF0000', '00FF00', '0000FF']
392
+ end
242
393
  end
243
394
  end
244
395
  #```
@@ -246,19 +397,21 @@ end
246
397
  ##Generating A Line Chart
247
398
 
248
399
  #```ruby
249
- wb.add_worksheet(:name => "Line Chart") do |sheet|
250
- sheet.add_row ["Simple Line Chart"]
251
- sheet.add_row %w(first second)
252
- 4.times do
253
- sheet.add_row [ rand(24)+1, rand(24)+1]
254
- end
255
- sheet.add_chart(Axlsx::Line3DChart, :title => "Simple Line Chart", :rotX => 30, :rotY => 20) do |chart|
256
- chart.start_at 0, 5
257
- chart.end_at 10, 20
258
- chart.add_series :data => sheet["A3:A6"], :title => sheet["A2"]
259
- chart.add_series :data => sheet["B3:B6"], :title => sheet["B2"]
260
- chart.catAxis.title = 'X Axis'
261
- chart.valAxis.title = 'Y Axis'
400
+ if examples.include? :line_chart
401
+ wb.add_worksheet(:name => "Line Chart") do |sheet|
402
+ sheet.add_row ["Simple Line Chart"]
403
+ sheet.add_row %w(first second)
404
+ 4.times do
405
+ sheet.add_row [ rand(24)+1, rand(24)+1]
406
+ end
407
+ sheet.add_chart(Axlsx::Line3DChart, :title => "Simple Line Chart", :rotX => 30, :rotY => 20) do |chart|
408
+ chart.start_at 0, 5
409
+ chart.end_at 10, 20
410
+ chart.add_series :data => sheet["A3:A6"], :title => sheet["A2"]
411
+ chart.add_series :data => sheet["B3:B6"], :title => sheet["B2"]
412
+ chart.catAxis.title = 'X Axis'
413
+ chart.valAxis.title = 'Y Axis'
414
+ end
262
415
  end
263
416
  end
264
417
  #```
@@ -266,16 +419,18 @@ end
266
419
  ##Generating A Scatter Chart
267
420
 
268
421
  #```ruby
269
- wb.add_worksheet(:name => "Scatter Chart") do |sheet|
270
- sheet.add_row ["First", 1, 5, 7, 9]
271
- sheet.add_row ["", 1, 25, 49, 81]
272
- sheet.add_row ["Second", 5, 2, 14, 9]
273
- sheet.add_row ["", 5, 10, 15, 20]
274
- sheet.add_chart(Axlsx::ScatterChart, :title => "example 7: Scatter Chart") do |chart|
275
- chart.start_at 0, 4
276
- chart.end_at 10, 19
277
- chart.add_series :xData => sheet["B1:E1"], :yData => sheet["B2:E2"], :title => sheet["A1"]
278
- chart.add_series :xData => sheet["B3:E3"], :yData => sheet["B4:E4"], :title => sheet["A3"]
422
+ if examples.include? :scatter_chart
423
+ wb.add_worksheet(:name => "Scatter Chart") do |sheet|
424
+ sheet.add_row ["First", 1, 5, 7, 9]
425
+ sheet.add_row ["", 1, 25, 49, 81]
426
+ sheet.add_row ["Second", 5, 2, 14, 9]
427
+ sheet.add_row ["", 5, 10, 15, 20]
428
+ sheet.add_chart(Axlsx::ScatterChart, :title => "example 7: Scatter Chart") do |chart|
429
+ chart.start_at 0, 4
430
+ chart.end_at 10, 19
431
+ chart.add_series :xData => sheet["B1:E1"], :yData => sheet["B2:E2"], :title => sheet["A1"]
432
+ chart.add_series :xData => sheet["B3:E3"], :yData => sheet["B4:E4"], :title => sheet["A3"]
433
+ end
279
434
  end
280
435
  end
281
436
  #```
@@ -284,13 +439,15 @@ end
284
439
  ##Tables
285
440
 
286
441
  #```ruby
287
- wb.add_worksheet(:name => "Table") do |sheet|
288
- sheet.add_row ["Build Matrix"]
289
- sheet.add_row ["Build", "Duration", "Finished", "Rvm"]
290
- sheet.add_row ["19.1", "1 min 32 sec", "about 10 hours ago", "1.8.7"]
291
- sheet.add_row ["19.2", "1 min 28 sec", "about 10 hours ago", "1.9.2"]
292
- sheet.add_row ["19.3", "1 min 35 sec", "about 10 hours ago", "1.9.3"]
293
- sheet.add_table "A2:D5", :name => 'Build Matrix'
442
+ if examples.include? :tables
443
+ wb.add_worksheet(:name => "Table") do |sheet|
444
+ sheet.add_row ["Build Matrix"]
445
+ sheet.add_row ["Build", "Duration", "Finished", "Rvm"]
446
+ sheet.add_row ["19.1", "1 min 32 sec", "about 10 hours ago", "1.8.7"]
447
+ sheet.add_row ["19.2", "1 min 28 sec", "about 10 hours ago", "1.9.2"]
448
+ sheet.add_row ["19.3", "1 min 35 sec", "about 10 hours ago", "1.9.3"]
449
+ sheet.add_table "A2:D5", :name => 'Build Matrix', :style_info => { :name => "TableStyleMedium23" }
450
+ end
294
451
  end
295
452
  #```
296
453
 
@@ -298,9 +455,11 @@ end
298
455
  ##Fit to page printing
299
456
 
300
457
  #```ruby
301
- wb.add_worksheet(:name => "fit to page") do |sheet|
302
- sheet.add_row ['this all goes on one page']
303
- sheet.fit_to_page = true
458
+ if examples.include? :fit_to_page
459
+ wb.add_worksheet(:name => "fit to page") do |sheet|
460
+ sheet.add_row ['this all goes on one page']
461
+ sheet.fit_to_page = true
462
+ end
304
463
  end
305
464
  ##```
306
465
 
@@ -308,157 +467,188 @@ end
308
467
  ##Hide Gridlines in worksheet
309
468
 
310
469
  #```ruby
311
- wb.add_worksheet(:name => "No Gridlines") do |sheet|
312
- sheet.add_row ["This", "Sheet", "Hides", "Gridlines"]
313
- sheet.show_gridlines = false
470
+ if examples.include? :hide_gridlines
471
+ wb.add_worksheet(:name => "No Gridlines") do |sheet|
472
+ sheet.add_row ["This", "Sheet", "Hides", "Gridlines"]
473
+ sheet.show_gridlines = false
474
+ end
314
475
  end
315
476
  ##```
316
477
 
478
+ # Repeat printing of header rows.
317
479
  #```ruby
318
- wb.add_worksheet(:name => "repeated header") do |sheet|
319
- sheet.add_row %w(These Column Header Will Render On Every Printed Sheet)
320
- 200.times { sheet.add_row %w(1 2 3 4 5 6 7 8) }
321
- wb.add_defined_name("'repeated header'!$1:$1", :local_sheet_id => sheet.index, :name => '_xlnm.Print_Titles')
480
+ if examples.include? :repeated_header
481
+ wb.add_worksheet(:name => "repeated header") do |sheet|
482
+ sheet.add_row %w(These Column Header Will Render On Every Printed Sheet)
483
+ 200.times { sheet.add_row %w(1 2 3 4 5 6 7 8) }
484
+ wb.add_defined_name("'repeated header'!$1:$1", :local_sheet_id => sheet.index, :name => '_xlnm.Print_Titles')
485
+ end
322
486
  end
323
487
 
324
- # Sheet Protection and excluding cells from locking.
325
- unlocked = wb.styles.add_style :locked => false
326
- wb.add_worksheet(:name => 'Sheet Protection') do |sheet|
327
- sheet.sheet_protection.password = 'fish'
328
- sheet.add_row [1, 2 ,3] # These cells will be locked
329
- sheet.add_row [4, 5, 6], :style => unlocked # these cells will not!
488
+ # Defined Names in formula
489
+ if examples.include? :defined_name
490
+ wb.add_worksheet(:name => 'defined name') do |sheet|
491
+ sheet.add_row [1, 2, 17, '=FOOBAR']
492
+ wb.add_defined_name("'defined name'!$C1", :local_sheet_id => sheet.index, :name => 'FOOBAR')
493
+ end
330
494
  end
331
495
 
496
+ # Sheet Protection and excluding cells from locking.
497
+ if examples.include? :sheet_protection
498
+ unlocked = wb.styles.add_style :locked => false
499
+ wb.add_worksheet(:name => 'Sheet Protection') do |sheet|
500
+ sheet.sheet_protection.password = 'fish'
501
+ sheet.add_row [1, 2 ,3] # These cells will be locked
502
+ sheet.add_row [4, 5, 6], :style => unlocked # these cells will not!
503
+ end
504
+ end
332
505
 
333
506
  ##Specify page margins and other options for printing
334
507
 
335
508
  #```ruby
336
- margins = {:left => 3, :right => 3, :top => 1.2, :bottom => 1.2, :header => 0.7, :footer => 0.7}
337
- setup = {:fit_to_width => 1, :orientation => :landscape, :paper_width => "297mm", :paper_height => "210mm"}
338
- options = {:grid_lines => true, :headings => true, :horizontal_centered => true}
339
- wb.add_worksheet(:name => "print margins", :page_margins => margins, :page_setup => setup, :print_options => options) do |sheet|
340
- sheet.add_row ["this sheet uses customized print settings"]
509
+ if examples.include? :printing
510
+ margins = {:left => 3, :right => 3, :top => 1.2, :bottom => 1.2, :header => 0.7, :footer => 0.7}
511
+ setup = {:fit_to_width => 1, :orientation => :landscape, :paper_width => "297mm", :paper_height => "210mm"}
512
+ options = {:grid_lines => true, :headings => true, :horizontal_centered => true}
513
+ wb.add_worksheet(:name => "print margins", :page_margins => margins, :page_setup => setup, :print_options => options) do |sheet|
514
+ sheet.add_row ["this sheet uses customized print settings"]
515
+ end
341
516
  end
342
517
  #```
343
518
 
344
519
  ## Add Comments to your spreadsheet
345
520
  #``` ruby
346
- wb.add_worksheet(:name => 'comments') do |sheet|
347
- sheet.add_row ['Can we build it?']
348
- sheet.add_comment :ref => 'A1', :author => 'Bob', :text => 'Yes We Can!'
521
+ if examples.include? :comments
522
+ wb.add_worksheet(:name => 'comments') do |sheet|
523
+ sheet.add_row ['Can we build it?']
524
+ sheet.add_comment :ref => 'A1', :author => 'Bob', :text => 'Yes We Can!'
525
+ end
349
526
  end
350
527
 
351
528
  ## Frozen/Split panes
352
529
  ## ``` ruby
353
- wb.add_worksheet(:name => 'fixed headers') do |sheet|
354
- sheet.add_row(['', (0..99).map { |i| "column header #{i}" }].flatten )
355
- 100.times.with_index { |index| sheet << ["row header", (0..index).to_a].flatten }
356
- sheet.sheet_view.pane do |pane|
357
- pane.top_left_cell = "B2"
358
- pane.state = :frozen_split
359
- pane.y_split = 1
360
- pane.x_split = 1
361
- pane.active_pane = :bottom_right
530
+ if examples.include? :panes
531
+ wb.add_worksheet(:name => 'fixed headers') do |sheet|
532
+ sheet.add_row(['', (0..99).map { |i| "column header #{i}" }].flatten )
533
+ 100.times.with_index { |index| sheet << ["row header", (0..index).to_a].flatten }
534
+ sheet.sheet_view.pane do |pane|
535
+ pane.top_left_cell = "B2"
536
+ pane.state = :frozen_split
537
+ pane.y_split = 1
538
+ pane.x_split = 1
539
+ pane.active_pane = :bottom_right
540
+ end
362
541
  end
363
542
  end
364
543
 
365
544
  # conditional formatting
366
545
  #
367
- percent = wb.styles.add_style(:format_code => "0.00%", :border => Axlsx::STYLE_THIN_BORDER)
368
- money = wb.styles.add_style(:format_code => '0,000', :border => Axlsx::STYLE_THIN_BORDER)
369
-
370
- # define the style for conditional formatting
371
- profitable = wb.styles.add_style( :fg_color=>"FF428751",
372
- :type => :dxf)
373
-
374
- wb.add_worksheet(:name => "Conditional Cell Is") do |ws|
375
-
376
- # Generate 20 rows of data
377
- ws.add_row ["Previous Year Quarterly Profits (JPY)"]
378
- ws.add_row ["Quarter", "Profit", "% of Total"]
379
- offset = 3
380
- rows = 20
381
- offset.upto(rows + offset) do |i|
382
- ws.add_row ["Q#{i}", 10000*((rows/2-i) * (rows/2-i)), "=100*B#{i}/SUM(B3:B#{rows+offset})"], :style=>[nil, money, percent]
546
+ if examples.include? :conditional_formatting
547
+ percent = wb.styles.add_style(:format_code => "0.00%", :border => Axlsx::STYLE_THIN_BORDER)
548
+ money = wb.styles.add_style(:format_code => '0,000', :border => Axlsx::STYLE_THIN_BORDER)
549
+
550
+ # define the style for conditional formatting
551
+ profitable = wb.styles.add_style( :fg_color=>"FF428751",
552
+ :type => :dxf)
553
+
554
+ wb.add_worksheet(:name => "Conditional Cell Is") do |ws|
555
+
556
+ # Generate 20 rows of data
557
+ ws.add_row ["Previous Year Quarterly Profits (JPY)"]
558
+ ws.add_row ["Quarter", "Profit", "% of Total"]
559
+ offset = 3
560
+ rows = 20
561
+ offset.upto(rows + offset) do |i|
562
+ ws.add_row ["Q#{i}", 10000*((rows/2-i) * (rows/2-i)), "=100*B#{i}/SUM(B3:B#{rows+offset})"], :style=>[nil, money, percent]
563
+ end
564
+
565
+ # Apply conditional formatting to range B3:B100 in the worksheet
566
+ ws.add_conditional_formatting("B3:B100", { :type => :cellIs, :operator => :greaterThan, :formula => "100000", :dxfId => profitable, :priority => 1 })
383
567
  end
384
568
 
385
- # Apply conditional formatting to range B3:B100 in the worksheet
386
- ws.add_conditional_formatting("B3:B100", { :type => :cellIs, :operator => :greaterThan, :formula => "100000", :dxfId => profitable, :priority => 1 })
387
- end
388
-
389
- wb.add_worksheet(:name => "Conditional Color Scale") do |ws|
390
- ws.add_row ["Previous Year Quarterly Profits (JPY)"]
391
- ws.add_row ["Quarter", "Profit", "% of Total"]
392
- offset = 3
393
- rows = 20
394
- offset.upto(rows + offset) do |i|
395
- ws.add_row ["Q#{i}", 10000*((rows/2-i) * (rows/2-i)), "=100*B#{i}/SUM(B3:B#{rows+offset})"], :style=>[nil, money, percent]
569
+ wb.add_worksheet(:name => "Conditional Color Scale") do |ws|
570
+ ws.add_row ["Previous Year Quarterly Profits (JPY)"]
571
+ ws.add_row ["Quarter", "Profit", "% of Total"]
572
+ offset = 3
573
+ rows = 20
574
+ offset.upto(rows + offset) do |i|
575
+ ws.add_row ["Q#{i}", 10000*((rows/2-i) * (rows/2-i)), "=100*B#{i}/SUM(B3:B#{rows+offset})"], :style=>[nil, money, percent]
576
+ end
577
+ # Apply conditional formatting to range B3:B100 in the worksheet
578
+ color_scale = Axlsx::ColorScale.new
579
+ ws.add_conditional_formatting("B3:B100", { :type => :colorScale, :operator => :greaterThan, :formula => "100000", :dxfId => profitable, :priority => 1, :color_scale => color_scale })
396
580
  end
397
- # Apply conditional formatting to range B3:B100 in the worksheet
398
- color_scale = Axlsx::ColorScale.new
399
- ws.add_conditional_formatting("B3:B100", { :type => :colorScale, :operator => :greaterThan, :formula => "100000", :dxfId => profitable, :priority => 1, :color_scale => color_scale })
400
- end
401
581
 
402
582
 
403
- wb.add_worksheet(:name => "Conditional Data Bar") do |ws|
404
- ws.add_row ["Previous Year Quarterly Profits (JPY)"]
405
- ws.add_row ["Quarter", "Profit", "% of Total"]
406
- offset = 3
407
- rows = 20
408
- offset.upto(rows + offset) do |i|
409
- ws.add_row ["Q#{i}", 10000*((rows/2-i) * (rows/2-i)), "=100*B#{i}/SUM(B3:B#{rows+offset})"], :style=>[nil, money, percent]
583
+ wb.add_worksheet(:name => "Conditional Data Bar") do |ws|
584
+ ws.add_row ["Previous Year Quarterly Profits (JPY)"]
585
+ ws.add_row ["Quarter", "Profit", "% of Total"]
586
+ offset = 3
587
+ rows = 20
588
+ offset.upto(rows + offset) do |i|
589
+ ws.add_row ["Q#{i}", 10000*((rows/2-i) * (rows/2-i)), "=100*B#{i}/SUM(B3:B#{rows+offset})"], :style=>[nil, money, percent]
590
+ end
591
+ # Apply conditional formatting to range B3:B100 in the worksheet
592
+ data_bar = Axlsx::DataBar.new
593
+ ws.add_conditional_formatting("B3:B100", { :type => :dataBar, :dxfId => profitable, :priority => 1, :data_bar => data_bar })
410
594
  end
411
- # Apply conditional formatting to range B3:B100 in the worksheet
412
- data_bar = Axlsx::DataBar.new
413
- ws.add_conditional_formatting("B3:B100", { :type => :dataBar, :dxfId => profitable, :priority => 1, :data_bar => data_bar })
414
- end
415
595
 
416
- wb.add_worksheet(:name => "Conditional Format Icon Set") do |ws|
417
- ws.add_row ["Previous Year Quarterly Profits (JPY)"]
418
- ws.add_row ["Quarter", "Profit", "% of Total"]
419
- offset = 3
420
- rows = 20
421
- offset.upto(rows + offset) do |i|
422
- ws.add_row ["Q#{i}", 10000*((rows/2-i) * (rows/2-i)), "=100*B#{i}/SUM(B3:B#{rows+offset})"], :style=>[nil, money, percent]
596
+ wb.add_worksheet(:name => "Conditional Format Icon Set") do |ws|
597
+ ws.add_row ["Previous Year Quarterly Profits (JPY)"]
598
+ ws.add_row ["Quarter", "Profit", "% of Total"]
599
+ offset = 3
600
+ rows = 20
601
+ offset.upto(rows + offset) do |i|
602
+ ws.add_row ["Q#{i}", 10000*((rows/2-i) * (rows/2-i)), "=100*B#{i}/SUM(B3:B#{rows+offset})"], :style=>[nil, money, percent]
603
+ end
604
+ # Apply conditional formatting to range B3:B100 in the worksheet
605
+ icon_set = Axlsx::IconSet.new
606
+ ws.add_conditional_formatting("B3:B100", { :type => :iconSet, :dxfId => profitable, :priority => 1, :icon_set => icon_set })
423
607
  end
424
- # Apply conditional formatting to range B3:B100 in the worksheet
425
- icon_set = Axlsx::IconSet.new
426
- ws.add_conditional_formatting("B3:B100", { :type => :iconSet, :dxfId => profitable, :priority => 1, :icon_set => icon_set })
427
608
  end
428
609
 
429
610
  ##Validate and Serialize
430
611
 
431
612
  #```ruby
432
613
  # Serialize directly to file
614
+
433
615
  p.serialize("example.xlsx")
434
616
 
435
617
  # or
436
618
 
437
619
  #Serialize to a stream
438
- s = p.to_stream()
439
- File.open('example_streamed.xlsx', 'w') { |f| f.write(s.read) }
620
+ if examples.include? :streaming
621
+ s = p.to_stream()
622
+ File.open('example_streamed.xlsx', 'w') { |f| f.write(s.read) }
623
+ end
440
624
  #```
441
625
 
442
626
  ##Using Shared Strings
443
627
 
444
628
  #```ruby
445
629
  # This is required by Numbers
446
- p.use_shared_strings = true
447
- p.serialize("shared_strings_example.xlsx")
630
+ if examples.include? :shared_strings
631
+ p.use_shared_strings = true
632
+ p.serialize("shared_strings_example.xlsx")
633
+ end
448
634
  #```
449
635
 
450
-
636
+ #p.validate do |er|
637
+ #puts er.inspect
638
+ #end
451
639
  ##Disabling Autowidth
452
640
 
453
641
  #```ruby
454
- p = Axlsx::Package.new
455
- p.use_autowidth = false
456
- wb = p.workbook
457
- wb.add_worksheet(:name => "Manual Widths") do | sheet |
458
- sheet.add_row ['oh look! no autowidth']
642
+ if examples.include? :no_autowidth
643
+ p = Axlsx::Package.new
644
+ p.use_autowidth = false
645
+ wb = p.workbook
646
+ wb.add_worksheet(:name => "Manual Widths") do | sheet |
647
+ sheet.add_row ['oh look! no autowidth']
648
+ end
649
+ p.validate.each { |e| puts e.message }
650
+ p.serialize("no-use_autowidth.xlsx")
459
651
  end
460
- p.validate.each { |e| puts e.message }
461
- p.serialize("no-use_autowidth.xlsx")
462
652
  #```
463
653
 
464
654