axlsx 1.3.1 → 1.3.2

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