caxlsx 3.0.0 → 3.1.0

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 (87) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +126 -31
  3. data/README.md +78 -170
  4. data/examples/{image1.jpeg → assets/image1.jpeg} +0 -0
  5. data/examples/generate.rb +15 -0
  6. data/lib/axlsx.rb +2 -3
  7. data/lib/axlsx/drawing/bar_chart.rb +3 -3
  8. data/lib/axlsx/drawing/bar_series.rb +3 -5
  9. data/lib/axlsx/drawing/d_lbls.rb +1 -1
  10. data/lib/axlsx/drawing/pie_series.rb +1 -1
  11. data/lib/axlsx/drawing/series_title.rb +3 -1
  12. data/lib/axlsx/drawing/title.rb +3 -2
  13. data/lib/axlsx/package.rb +53 -19
  14. data/lib/axlsx/rels/relationship.rb +26 -25
  15. data/lib/axlsx/stylesheet/font.rb +10 -2
  16. data/lib/axlsx/util/constants.rb +2 -1
  17. data/lib/axlsx/util/mime_type_utils.rb +1 -1
  18. data/lib/axlsx/util/validators.rb +2 -2
  19. data/lib/axlsx/util/zip_command.rb +73 -0
  20. data/lib/axlsx/version.rb +1 -1
  21. data/lib/axlsx/workbook/workbook.rb +0 -9
  22. data/lib/axlsx/workbook/worksheet/cell.rb +32 -5
  23. data/lib/axlsx/workbook/worksheet/col.rb +8 -4
  24. data/lib/axlsx/workbook/worksheet/data_validation.rb +4 -4
  25. data/lib/axlsx/workbook/worksheet/pivot_table.rb +7 -2
  26. data/lib/axlsx/workbook/worksheet/pivot_table_cache_definition.rb +1 -1
  27. data/lib/axlsx/workbook/worksheet/rich_text_run.rb +1 -1
  28. data/lib/axlsx/workbook/worksheet/row.rb +6 -3
  29. data/lib/axlsx/workbook/worksheet/table.rb +1 -1
  30. data/lib/axlsx/workbook/worksheet/worksheet.rb +17 -4
  31. data/lib/caxlsx.rb +2 -0
  32. data/test/drawing/tc_drawing.rb +2 -2
  33. data/test/drawing/tc_hyperlink.rb +1 -1
  34. data/test/drawing/tc_one_cell_anchor.rb +1 -1
  35. data/test/drawing/tc_pic.rb +4 -4
  36. data/test/drawing/tc_pie_series.rb +2 -1
  37. data/test/drawing/tc_series_title.rb +21 -0
  38. data/test/drawing/tc_title.rb +16 -0
  39. data/test/fixtures/image1.gif +0 -0
  40. data/test/fixtures/image1.jpeg +0 -0
  41. data/test/fixtures/image1.jpg +0 -0
  42. data/test/fixtures/image1.png +0 -0
  43. data/test/fixtures/image1_fake.jpg +0 -0
  44. data/test/rels/tc_relationship.rb +8 -0
  45. data/test/stylesheet/tc_font.rb +14 -2
  46. data/test/stylesheet/tc_styles.rb +27 -1
  47. data/test/tc_axlsx.rb +6 -0
  48. data/test/tc_helper.rb +0 -2
  49. data/test/tc_package.rb +82 -13
  50. data/test/util/tc_mime_type_utils.rb +1 -1
  51. data/test/util/tc_validators.rb +1 -1
  52. data/test/workbook/worksheet/tc_cell.rb +68 -2
  53. data/test/workbook/worksheet/tc_col.rb +16 -1
  54. data/test/workbook/worksheet/tc_pivot_table.rb +8 -0
  55. data/test/workbook/worksheet/tc_pivot_table_cache_definition.rb +8 -0
  56. data/test/workbook/worksheet/tc_rich_text_run.rb +3 -2
  57. data/test/workbook/worksheet/tc_row.rb +38 -0
  58. data/test/workbook/worksheet/tc_table.rb +10 -0
  59. data/test/workbook/worksheet/tc_worksheet.rb +24 -15
  60. metadata +130 -151
  61. data/examples/2010_comments.rb +0 -17
  62. data/examples/anchor_swapping.rb +0 -28
  63. data/examples/auto_filter.rb +0 -25
  64. data/examples/basic_charts.rb +0 -58
  65. data/examples/chart_colors.rb +0 -88
  66. data/examples/colored_links.rb +0 -59
  67. data/examples/conditional_formatting/example_conditional_formatting.rb +0 -89
  68. data/examples/conditional_formatting/getting_barred.rb +0 -37
  69. data/examples/conditional_formatting/hitting_the_high_notes.rb +0 -37
  70. data/examples/conditional_formatting/scaled_colors.rb +0 -39
  71. data/examples/conditional_formatting/stop_and_go.rb +0 -37
  72. data/examples/data_validation.rb +0 -67
  73. data/examples/example.rb +0 -885
  74. data/examples/extractive.rb +0 -45
  75. data/examples/ios_preview.rb +0 -14
  76. data/examples/merge_cells.rb +0 -17
  77. data/examples/no_grid_with_borders.rb +0 -18
  78. data/examples/page_setup.rb +0 -11
  79. data/examples/pivot_table.rb +0 -39
  80. data/examples/pivot_test.rb +0 -63
  81. data/examples/sheet_protection.rb +0 -10
  82. data/examples/skydrive/real_example.rb +0 -63
  83. data/examples/split.rb +0 -16
  84. data/examples/styles.rb +0 -66
  85. data/examples/underline.rb +0 -13
  86. data/examples/wrap_text.rb +0 -21
  87. data/lib/axlsx/util/parser.rb +0 -44
@@ -1,39 +0,0 @@
1
- #!/usr/bin/env ruby -w -s
2
- # -*- coding: utf-8 -*-
3
- $LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../lib"
4
- require 'axlsx'
5
- p = Axlsx::Package.new
6
- p.workbook do |wb|
7
- # define your regular styles
8
- styles = wb.styles
9
- title = styles.add_style :sz => 15, :b => true, :u => true
10
- default = styles.add_style :border => Axlsx::STYLE_THIN_BORDER
11
- header = styles.add_style :bg_color => '00', :fg_color => 'FF', :b => true
12
- money = styles.add_style :format_code => '#,###,##0', :border => Axlsx::STYLE_THIN_BORDER
13
- percent = styles.add_style :num_fmt => Axlsx::NUM_FMT_PERCENT, :border => Axlsx::STYLE_THIN_BORDER
14
-
15
- # define the style for conditional formatting - its the :dxf bit that counts!
16
- profitable = styles.add_style :fg_color => 'FF428751', :sz => 12, :type => :dxf, :b => true
17
-
18
- wb.add_worksheet(:name => 'Scaled Colors') do |ws|
19
- ws.add_row ['A$$le Q1 Revenue Historical Analysis (USD)'], :style => title
20
- ws.add_row
21
- ws.add_row ['Quarter', 'Profit', '% of Total'], :style => header
22
- ws.add_row ['Q1-2010', '15680000000', '=B4/SUM(B4:B7)'], :style => [default, money, percent]
23
- ws.add_row ['Q1-2011', '26740000000', '=B5/SUM(B4:B7)'], :style => [default, money, percent]
24
- ws.add_row ['Q1-2012', '46330000000', '=B6/SUM(B4:B7)'], :style => [default, money, percent]
25
- ws.add_row ['Q1-2013(est)', '72230000000', '=B7/SUM(B4:B7)'], :style => [default, money, percent]
26
-
27
- ws.merge_cells 'A1:C1'
28
-
29
- # Apply conditional formatting to range B4:B7 in the worksheet
30
- color_scale = Axlsx::ColorScale.new
31
- ws.add_conditional_formatting 'B4:B7', { :type => :colorScale,
32
- :operator => :greaterThan,
33
- :formula => '27000000000',
34
- :dxfId => profitable,
35
- :priority => 1,
36
- :color_scale => color_scale }
37
- end
38
- end
39
- p.serialize 'scaled_colors.xlsx'
@@ -1,37 +0,0 @@
1
- #!/usr/bin/env ruby -w -s
2
- # -*- coding: utf-8 -*-
3
- $LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../lib"
4
- require 'axlsx'
5
- p = Axlsx::Package.new
6
- p.workbook do |wb|
7
- # define your regular styles
8
- styles = wb.styles
9
- title = styles.add_style :sz => 15, :b => true, :u => true
10
- default = styles.add_style :border => Axlsx::STYLE_THIN_BORDER
11
- header = styles.add_style :bg_color => '00', :fg_color => 'FF', :b => true
12
- money = styles.add_style :format_code => '#,###,##0', :border => Axlsx::STYLE_THIN_BORDER
13
- percent = styles.add_style :num_fmt => Axlsx::NUM_FMT_PERCENT, :border => Axlsx::STYLE_THIN_BORDER
14
-
15
- # define the style for conditional formatting - its the :dxf bit that counts!
16
- profitable = styles.add_style :fg_color => 'FF428751', :sz => 12, :type => :dxf, :b => true
17
-
18
- wb.add_worksheet(:name => 'Downtown traffic') do |ws|
19
- ws.add_row ['A$$le Q1 Revenue Historical Analysis (USD)'], :style => title
20
- ws.add_row
21
- ws.add_row ['Quarter', 'Profit', '% of Total'], :style => header
22
- ws.add_row ['Q1-2010', '15680000000', '=B4/SUM(B4:B7)'], :style => [default, money, percent]
23
- ws.add_row ['Q1-2011', '26740000000', '=B5/SUM(B4:B7)'], :style => [default, money, percent]
24
- ws.add_row ['Q1-2012', '46330000000', '=B6/SUM(B4:B7)'], :style => [default, money, percent]
25
- ws.add_row ['Q1-2013(est)', '72230000000', '=B7/SUM(B4:B7)'], :style => [default, money, percent]
26
-
27
- ws.merge_cells 'A1:C1'
28
-
29
- # Apply conditional formatting to range B3:B7 in the worksheet
30
- icon_set = Axlsx::IconSet.new
31
- ws.add_conditional_formatting 'B3:B7', { :type => :iconSet,
32
- :dxfId => profitable,
33
- :priority => 1,
34
- :icon_set => icon_set }
35
- end
36
- end
37
- p.serialize 'stop_and_go.xlsx'
@@ -1,67 +0,0 @@
1
- #!/usr/bin/env ruby -w -s
2
- # -*- coding: utf-8 -*-
3
- $LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../lib"
4
- require 'axlsx'
5
-
6
- p = Axlsx::Package.new
7
- p.workbook.add_worksheet do |ws|
8
-
9
- ws.add_row ["between", "lessThan", "bound list", "raw list"]
10
-
11
- 4.times do |i|
12
- ws.add_row [nil, nil, nil, nil, (i+1) * 2]
13
- end
14
-
15
- ws.add_data_validation("A2:A5", {
16
- :type => :whole,
17
- :operator => :between,
18
- :formula1 => '5',
19
- :formula2 => '10',
20
- :showErrorMessage => true,
21
- :errorTitle => 'Wrong input',
22
- :error => 'Only values between 5 and 10',
23
- :errorStyle => :information,
24
- :showInputMessage => true,
25
- :promptTitle => 'Be careful!',
26
- :prompt => %{We really want a value between 5 and 10,
27
- but it is OK if you want to break the rules.
28
- }})
29
-
30
- ws.add_data_validation("B1:B5", {
31
- :type => :textLength,
32
- :operator => :lessThan,
33
- :formula1 => '10',
34
- :showErrorMessage => true,
35
- :errorTitle => 'Text is too long',
36
- :error => 'Max text length is 10 characters',
37
- :errorStyle => :stop,
38
- :showInputMessage => true,
39
- :promptTitle => 'Text length',
40
- :prompt => 'Max text length is 10 characters'})
41
-
42
- ws.add_data_validation("C2:C5", {
43
- :type => :list,
44
- :formula1 => 'E2:E5',
45
- :showDropDown => false,
46
- :showErrorMessage => true,
47
- :errorTitle => '',
48
- :error => 'Only values from E2:E5',
49
- :errorStyle => :stop,
50
- :showInputMessage => true,
51
- :promptTitle => '',
52
- :prompt => 'Only values from E2:E5'})
53
-
54
- ws.add_data_validation("D2:D5", {
55
- :type => :list,
56
- :formula1 => '"Red, Orange, NavyBlue"',
57
- :showDropDown => false,
58
- :showErrorMessage => true,
59
- :errorTitle => '',
60
- :error => 'Please use the dropdown selector to choose the value',
61
- :errorStyle => :stop,
62
- :showInputMessage => true,
63
- :prompt => '& Choose the value from the dropdown'})
64
-
65
- end
66
-
67
- p.serialize 'data_validation.xlsx'
data/examples/example.rb DELETED
@@ -1,885 +0,0 @@
1
- #!/usr/bin/env ruby -w -s
2
- # -*- coding: utf-8 -*-
3
- $LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../lib"
4
-
5
- #```ruby
6
- require 'axlsx'
7
- examples = []
8
- examples << :basic
9
- examples << :custom_styles
10
- examples << :wrap_text
11
- examples << :cell_style_override
12
- examples << :custom_borders
13
- examples << :surrounding_border
14
- examples << :deep_custom_borders
15
- examples << :row_column_style
16
- examples << :fixed_column_width
17
- examples << :height
18
- examples << :outline_level
19
- examples << :merge_cells
20
- examples << :images
21
- examples << :format_dates
22
- examples << :mbcs
23
- examples << :formula
24
- examples << :auto_filter
25
- examples << :sheet_protection
26
- examples << :data_types
27
- examples << :override_data_types
28
- examples << :hyperlinks
29
- examples << :number_currency_format
30
- examples << :venezuela_currency
31
- examples << :bar_chart
32
- examples << :chart_gridlines
33
- examples << :pie_chart
34
- examples << :line_chart
35
- examples << :scatter_chart
36
- examples << :tables
37
- examples << :fit_to_page
38
- examples << :hide_gridlines
39
- examples << :repeated_header
40
- examples << :defined_name
41
- examples << :printing
42
- examples << :header_footer
43
- examples << :comments
44
- examples << :panes
45
- examples << :book_view
46
- examples << :sheet_view
47
- examples << :hiding_sheets
48
- examples << :conditional_formatting
49
- examples << :streaming
50
- examples << :shared_strings
51
- examples << :no_autowidth
52
- examples << :cached_formula
53
- examples << :page_breaks
54
- examples << :rich_text
55
- examples << :tab_color
56
-
57
- p = Axlsx::Package.new
58
- wb = p.workbook
59
- #```
60
-
61
- ## A Simple Workbook
62
-
63
- #```ruby
64
- if examples.include? :basic
65
- wb.add_worksheet(:name => "Basic Worksheet") do |sheet|
66
- sheet.add_row ["First Column", "Second", "Third"]
67
- sheet.add_row [1, 2, 3]
68
- sheet.add_row [' preserving whitespace']
69
- end
70
- end
71
- #```
72
-
73
- #Using Custom Styles
74
-
75
- #```ruby
76
- # Each cell allows a single, predified style.
77
- # 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.
78
- # Alternatively, you can apply a style to an entire row by using an integer value for :style.
79
- if examples.include? :custom_styles
80
- wb.styles do |s|
81
- black_cell = s.add_style :bg_color => "00", :fg_color => "FF", :sz => 14, :alignment => { :horizontal=> :center }
82
- blue_cell = s.add_style :bg_color => "0000FF", :fg_color => "FF", :sz => 20, :alignment => { :horizontal=> :center }
83
- wb.add_worksheet(:name => "Custom Styles") do |sheet|
84
-
85
- # Applies the black_cell style to the first and third cell, and the blue_cell style to the second.
86
- sheet.add_row ["Text Autowidth", "Second", "Third"], :style => [black_cell, blue_cell, black_cell]
87
-
88
- # Applies the thin border to all three cells
89
- sheet.add_row [1, 2, 3], :style => Axlsx::STYLE_THIN_BORDER
90
- end
91
- end
92
- end
93
-
94
-
95
- #```ruby
96
- # A simple example of wrapping text. Seems this may not be working in Libre Office so here is an example for me to play with.
97
- if examples.include? :wrap_text
98
- wb.styles do |s|
99
- wrap_text = s.add_style :fg_color=> "FFFFFF",
100
- :b => true,
101
- :bg_color => "004586",
102
- :sz => 12,
103
- :border => { :style => :thin, :color => "00" },
104
- :alignment => { :horizontal => :center,
105
- :vertical => :center ,
106
- :wrap_text => true}
107
- wb.add_worksheet(:name => 'wrap text') do |sheet|
108
- sheet.add_row ['Torp, White and Cronin'], :style=>wrap_text
109
- sheet.column_info.first.width = 5
110
- end
111
- end
112
- end
113
-
114
- ##Styling Cell Overrides
115
-
116
- #```ruby
117
- #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.
118
- if examples.include? :cell_style_override
119
- wb.add_worksheet(:name => "Cell Level Style Overrides") do |sheet|
120
-
121
- # this will set the font size for each cell.
122
- sheet.add_row ['col 1', 'col 2', 'col 3', 'col 4'], :sz => 16
123
-
124
- sheet.add_row [1, 2, 3, "=SUM(A2:C2)"]
125
- sheet.add_row %w(u shadow sz b i strike outline)
126
- sheet.rows.last.cells[0].u = :double
127
- sheet.rows.last.cells[1].shadow = true
128
- sheet.rows.last.cells[2].sz = 20
129
- sheet.rows.last.cells[3].b = true
130
- sheet.rows.last.cells[4].i = true
131
- sheet.rows.last.cells[5].strike = true
132
- sheet.rows.last.cells[6].outline = 1
133
- # You can also apply cell style overrides to a range of cells
134
- sheet["A1:D1"].each { |c| c.color = "FF0000" }
135
- sheet['A1:D2'].each { |c| c.style = Axlsx::STYLE_THIN_BORDER }
136
- end
137
- end
138
- ##```
139
-
140
- ##Using Custom Border Styles
141
-
142
- #```ruby
143
- #Axlsx defines a thin border style, but you can easily create and use your own.
144
- if examples.include? :custom_borders
145
- wb.styles do |s|
146
- red_border = s.add_style :border => { :style => :thick, :color =>"FFFF0000", :edges => [:left, :right] }
147
- blue_border = s.add_style :border => { :style => :thick, :color =>"FF0000FF"}
148
-
149
- wb.add_worksheet(:name => "Custom Borders") do |sheet|
150
- sheet.add_row ["wrap", "me", "Up in Red"], :style => red_border
151
- sheet.add_row [1, 2, 3], :style => blue_border
152
- end
153
- end
154
- end
155
-
156
- #```ruby
157
- # More Custom Borders
158
- if examples.include? :surrounding_border
159
-
160
- # Stuff like this is why I LOVE RUBY
161
- # If you dont know about hash default values
162
- # LEARN IT! LIVE IT! LOVE IT!
163
- defaults = { :style => :thick, :color => "000000" }
164
- borders = Hash.new do |hash, key|
165
- hash[key] = wb.styles.add_style :border => defaults.merge( { :edges => key.to_s.split('_').map(&:to_sym) } )
166
- end
167
- top_row = [0, borders[:top_left], borders[:top], borders[:top], borders[:top_right]]
168
- middle_row = [0, borders[:left], nil, nil, borders[:right]]
169
- bottom_row = [0, borders[:bottom_left], borders[:bottom], borders[:bottom], borders[:bottom_right]]
170
-
171
- wb.add_worksheet(:name => "Surrounding Border") do |ws|
172
- ws.add_row []
173
- ws.add_row ['', 1,2,3,4], :style => top_row
174
- ws.add_row ['', 5,6,7,8], :style => middle_row
175
- ws.add_row ['', 9, 10, 11, 12]
176
-
177
- #This works too!
178
- ws.rows.last.style = bottom_row
179
-
180
- end
181
- end
182
-
183
- #```ruby
184
- # Hacking border styles
185
- if examples.include? :deep_custom_borders
186
- wb.styles do |s|
187
- top_bottom = s.add_style :border => { :style => :thick, :color =>"FFFF0000", :edges => [:top, :bottom] }
188
- border = s.borders[s.cellXfs[top_bottom].borderId]
189
- # edit existing border parts
190
- border.prs.each do |part|
191
- case part.name
192
- when :top
193
- part.color = Axlsx::Color.new(:rgb => "FFFF0000")
194
- when :bottom
195
- part.color = Axlsx::Color.new(:rgb => "FF00FF00")
196
- end
197
- end
198
-
199
- border.prs << Axlsx::BorderPr.new(:name => :left, :color => Axlsx::Color.new(:rgb => '0000FF'), :style => :mediumDashed)
200
- wb.add_worksheet(:name => 'hacked borders') do |sheet|
201
- sheet.add_row [1,2,3], :style=>top_bottom
202
- end
203
- end
204
- end
205
- ##```
206
-
207
-
208
- ##Styling Rows and Columns
209
-
210
- #```ruby
211
- if examples.include? :row_column_style
212
- wb.styles do |s|
213
- head = s.add_style :bg_color => "00", :fg_color => "FF"
214
- percent = s.add_style :num_fmt => 9
215
- wb.add_worksheet(:name => "Columns and Rows") do |sheet|
216
- # Note: you must add rows to the document *BEFORE* applying column styles to them
217
- sheet.add_row ['col 1', 'col 2', 'col 3', 'col 4', 'col5']
218
- sheet.add_row [1, 2, 0.3, 4, 5.0]
219
- sheet.add_row [1, 2, 0.2, 4, 5.0]
220
- sheet.add_row [1, 2, 0.1, 4, 5.0]
221
-
222
- #apply the percent style to the column at index 2 skipping the first row.
223
- sheet.col_style 2, percent, :row_offset => 1
224
-
225
- # apply the head style to the first row.
226
- sheet.row_style 0, head
227
-
228
- #Hide the 5th column
229
- sheet.column_info[4].hidden = true
230
-
231
- #Set the second column outline level
232
- sheet.column_info[1].outlineLevel = 2
233
-
234
- sheet.rows[3].hidden = true
235
- sheet.rows[1].outlineLevel = 2
236
- end
237
- end
238
- end
239
- ##```
240
-
241
-
242
- ##Specifying Column Widths
243
-
244
- #```ruby
245
- if examples.include? :fixed_column_width
246
- wb.add_worksheet(:name => "custom column widths") do |sheet|
247
- sheet.add_row ["I use autowidth and am very wide", "I use a custom width and am narrow"]
248
- sheet.add_row ['abcdefg', 'This is a very long text and should flow into the right cell', nil, 'xxx' ]
249
- sheet.column_widths nil, 3, 5, nil
250
- end
251
- end
252
-
253
-
254
- ##Specifying Row height
255
-
256
- #```ruby
257
- if examples.include? :height
258
- wb.styles do |s|
259
- head = s.add_style :bg_color => "00", :fg_color => "FF"
260
- wb.add_worksheet(:name => "fixed row height") do |sheet|
261
- sheet.add_row ["This row will have a fixed height", "It will overwite the default row height"], :height => 30
262
- sheet.add_row ["This row can have a different height too"], :height => 10, :style => head
263
- end
264
- end
265
- end
266
-
267
-
268
- #```ruby
269
- if examples.include? :outline_level
270
- wb.add_worksheet(name: 'outline_level') do |sheet|
271
- sheet.add_row [1, 2, 3, Time.now, 5, 149455.15]
272
- sheet.add_row [1, 2, 5, 6, 5,14100.19]
273
- sheet.add_row [9500002267, 1212, 1212, 5,14100.19]
274
- sheet.outline_level_rows 0, 2
275
- sheet.outline_level_columns 0, 2
276
- end
277
- end
278
- ##```
279
-
280
- ##Merging Cells.
281
-
282
- #```ruby
283
- if examples.include? :merge_cells
284
- wb.add_worksheet(:name => 'Merging Cells') do |sheet|
285
- # cell level style overides when adding cells
286
- sheet.add_row ["col 1", "col 2", "col 3", "col 4"], :sz => 16
287
- sheet.add_row [1, 2, 3, "=SUM(A2:C2)"]
288
- sheet.add_row [2, 3, 4, "=SUM(A3:C3)"]
289
- sheet.add_row ["total", "", "", "=SUM(D2:D3)"]
290
- sheet.merge_cells("A4:C4")
291
- sheet["A1:D1"].each { |c| c.color = "FF0000"}
292
- sheet["A1:D4"].each { |c| c.style = Axlsx::STYLE_THIN_BORDER }
293
- end
294
- end
295
- ##```
296
-
297
- ##Add an Image with a hyperlink
298
-
299
- #```ruby
300
- if examples.include? :images
301
- wb.add_worksheet(:name => "Image with Hyperlink") do |sheet|
302
- img = File.expand_path('../image1.jpeg', __FILE__)
303
- # specifying the :hyperlink option will add a hyper link to your image.
304
- #
305
- # @note - Numbers does not support this part of the specification.
306
-
307
- sheet.add_image(:image_src => img, :noSelect => true, :noMove => true, :hyperlink=>"http://axlsx.blogspot.com") do |image|
308
- image.width=720
309
- image.height=666
310
- image.hyperlink.tooltip = "Labeled Link"
311
- image.start_at 0, 0
312
- end
313
-
314
- # position in block
315
- sheet.add_image(:image_src => img, :noSelect => true, :noMove => true, :hyperlink=>"http://axlsx.blogspot.com") do |image|
316
- image.start_at 22, 14
317
- image.end_at 23, 17
318
- end
319
- # all in one go
320
- sheet.add_image(:image_src => img, :start_at => [15, 33], :end_at => [20, 37], :noSelect => true, :noMove => true, :hyperlink=>"http://axlsx.blogspot.com")
321
- end
322
- end
323
- #```
324
-
325
- ##Using Custom Formatting and date1904
326
-
327
- #```ruby
328
- if examples.include? :format_dates
329
- require 'date'
330
- wb.styles do |s|
331
- date = s.add_style(:format_code => "yyyy-mm-dd", :border => Axlsx::STYLE_THIN_BORDER)
332
- padded = s.add_style(:format_code => "00#", :border => Axlsx::STYLE_THIN_BORDER)
333
- percent = s.add_style(:format_code => "0000%", :border => Axlsx::STYLE_THIN_BORDER)
334
- # wb.date1904 = true # Use the 1904 date system (Used by Excel for Mac < 2011)
335
- wb.add_worksheet(:name => "Formatting Data") do |sheet|
336
- sheet.add_row ["Custom Formatted Date", "Percent Formatted Float", "Padded Numbers"], :style => Axlsx::STYLE_THIN_BORDER
337
- sheet.add_row [Date::strptime('2012-01-19','%Y-%m-%d'), 0.2, 32], :style => [date, percent, padded]
338
- end
339
- end
340
- end
341
- #```
342
-
343
- ##Asian Language Support
344
-
345
- #```ruby
346
- if examples.include? :mbcs
347
- wb.styles.fonts.first.name = 'Arial Unicode MS'
348
- wb.add_worksheet(:name => "日本語でのシート名") do |sheet|
349
- sheet.add_row ["日本語"]
350
- sheet.add_row ["华语/華語"]
351
- sheet.add_row ["한국어/조선말"]
352
- end
353
- end
354
- ##```
355
-
356
- ##Using formula
357
-
358
- #```ruby
359
- if examples.include? :formula
360
- wb.add_worksheet(:name => "Using Formulas") do |sheet|
361
- sheet.add_row ['col 1', 'col 2', 'col 3', 'col 4']
362
- sheet.add_row [1, 2, 3, "=SUM(A2:C2)"]
363
- end
364
- end
365
- ##```
366
-
367
- ##Auto Filter
368
-
369
- #```ruby
370
- if examples.include? :auto_filter
371
- wb.add_worksheet(:name => "Auto Filter") do |sheet|
372
- sheet.add_row ["Build Matrix"]
373
- sheet.add_row ["Build", "Duration", "Finished", "Rvm"]
374
- sheet.add_row ["19.1", "1 min 32 sec", "about 10 hours ago", "1.8.7"]
375
- sheet.add_row ["19.2", "1 min 28 sec", "about 10 hours ago", "1.9.2"]
376
- sheet.add_row ["19.3", "1 min 35 sec", "about 10 hours ago", "1.9.3"]
377
- sheet.auto_filter = "A2:D5"
378
- sheet.auto_filter.add_column 3, :filters, :filter_items => ['1.9.2', '1.8.7']
379
- end
380
- end
381
- #```
382
-
383
- ##Automatic cell types
384
-
385
-
386
- #```ruby
387
- if examples.include? :data_types
388
- wb.add_worksheet(:name => "Automatic cell types") do |sheet|
389
- date_format = wb.styles.add_style :format_code => 'YYYY-MM-DD'
390
- time_format = wb.styles.add_style :format_code => 'hh:mm:ss'
391
- sheet.add_row ["Date", "Time", "String", "Boolean", "Float", "Integer"]
392
- sheet.add_row [Date.today, Time.now, "value", true, 0.1, 1], :style => [date_format, time_format]
393
- end
394
- end
395
-
396
- #```ruby
397
- if examples.include? :override_data_types
398
- wb.add_worksheet(:name => "Override Data Type") do |sheet|
399
- sheet.add_row ['dont eat my zeros!', '0088'] , :types => [nil, :string]
400
- end
401
- end
402
- # Hyperlinks in worksheet
403
- if examples.include? :hyperlinks
404
- wb.add_worksheet(:name => 'hyperlinks') do |sheet|
405
- # external references
406
- sheet.add_row ['axlsx']
407
- sheet.add_hyperlink :location => 'https://github.com/randym/axlsx', :ref => sheet.rows.first.cells.first
408
- # internal references
409
- sheet.add_hyperlink :location => "'Next Sheet'!A1", :ref => 'A2', :target => :sheet
410
- sheet.add_row ['next sheet']
411
- end
412
-
413
- wb.add_worksheet(:name => 'Next Sheet') do |sheet|
414
- sheet.add_row ['hello!']
415
- end
416
- end
417
- ###```
418
-
419
- ##Number formatting and currency
420
- if examples.include? :number_currency_format
421
- wb.add_worksheet(:name => "Formats and Currency") do |sheet|
422
- currency = wb.styles.add_style :num_fmt => 5
423
- red_negative = wb.styles.add_style :num_fmt => 8
424
- comma = wb.styles.add_style :num_fmt => 3
425
- super_funk = wb.styles.add_style :format_code => '[Green]#'
426
- sheet.add_row %w(Currency RedNegative Comma Custom)
427
- sheet.add_row [1500, -122.34, 123456789, 594829], :style=> [currency, red_negative, comma, super_funk]
428
- end
429
- end
430
-
431
- ## Venezuala currency
432
- if examples.include? :venezuela_currency
433
- wb.add_worksheet(:name => 'Venezuala_currency') do |sheet|
434
- number = wb.styles.add_style :format_code => '#.##0\,00'
435
- sheet.add_row [2.5] , :style => [number]
436
- end
437
- end
438
-
439
- ##Generating A Bar Chart
440
-
441
- #```ruby
442
- if examples.include? :bar_chart
443
- wb.add_worksheet(:name => "Bar Chart") do |sheet|
444
- sheet.add_row ["A Simple Bar Chart"]
445
- %w(first second third).each { |label| sheet.add_row [label, rand(24)+1] }
446
- sheet.add_chart(Axlsx::Bar3DChart, :start_at => "A6", :end_at => "F20") do |chart|
447
- chart.add_series :data => sheet["B2:B4"], :labels => sheet["A2:A4"], :title => sheet["A1"], :colors => ["00FF00", "0000FF"]
448
- end
449
- end
450
- end
451
-
452
- ##```
453
-
454
- ##Hide Gridlines in chart
455
-
456
- #```ruby
457
- if examples.include? :chart_gridlines
458
- wb.add_worksheet(:name => "Chart With No Gridlines") do |sheet|
459
- sheet.add_row ["Bar Chart without gridlines"]
460
- %w(first second third).each { |label| sheet.add_row [label, rand(24)+1] }
461
- sheet.add_chart(Axlsx::Bar3DChart, :start_at => "A6", :end_at => "F20") do |chart|
462
- chart.add_series :data => sheet["B2:B4"], :labels => sheet["A2:A4"], :colors => ["00FF00", "FF0000"]
463
- chart.valAxis.gridlines = false
464
- chart.catAxis.gridlines = false
465
- end
466
- end
467
- end
468
- #```
469
-
470
- ##Generating A Pie Chart
471
-
472
- #```ruby
473
- if examples.include? :pie_chart
474
- wb.add_worksheet(:name => "Pie Chart") do |sheet|
475
- sheet.add_row ["Simple Pie Chart"]
476
- %w(first second third).each { |label| sheet.add_row [label, rand(24)+1] }
477
- sheet.add_chart(Axlsx::Pie3DChart, :start_at => [0,5], :end_at => [10, 20], :title => "example 3: Pie Chart") do |chart|
478
- chart.add_series :data => sheet["B2:B4"], :labels => sheet["A2:A4"], :colors => ['FF0000', '00FF00', '0000FF']
479
- end
480
- end
481
- end
482
- #```
483
-
484
- ##Generating A Line Chart
485
-
486
- #```ruby
487
- if examples.include? :line_chart
488
- wb.add_worksheet(:name => "Line Chart") do |sheet|
489
- sheet.add_row ["Simple Line Chart"]
490
- sheet.add_row %w(first second)
491
- 4.times do
492
- sheet.add_row [ rand(24)+1, rand(24)+1]
493
- end
494
- sheet.add_chart(Axlsx::Line3DChart, :title => "Simple 3D Line Chart", :rotX => 30, :rotY => 20) do |chart|
495
- chart.start_at 0, 5
496
- chart.end_at 10, 20
497
- chart.add_series :data => sheet["A3:A6"], :title => sheet["A2"], :color => "0000FF"
498
- chart.add_series :data => sheet["B3:B6"], :title => sheet["B2"], :color => "FF0000"
499
- chart.catAxis.title = 'X Axis'
500
- chart.valAxis.title = 'Y Axis'
501
- end
502
- sheet.add_chart(Axlsx::LineChart, :title => "Simple Line Chart", :rotX => 30, :rotY => 20) do |chart|
503
- chart.start_at 0, 21
504
- chart.end_at 10, 41
505
- chart.add_series :data => sheet["A3:A6"], :title => sheet["A2"], :color => "FF0000", :show_marker => true, :smooth => true
506
- chart.add_series :data => sheet["B3:B6"], :title => sheet["B2"], :color => "00FF00"
507
- chart.catAxis.title = 'X Axis'
508
- chart.valAxis.title = 'Y Axis'
509
- end
510
-
511
- end
512
- end
513
- #```
514
-
515
- ##Generating A Scatter Chart
516
-
517
- #```ruby
518
- if examples.include? :scatter_chart
519
- wb.add_worksheet(:name => "Scatter Chart") do |sheet|
520
- sheet.add_row ["First", 1, 5, 7, 9]
521
- sheet.add_row ["", 1, 25, 49, 81]
522
- sheet.add_row ["Second", 5, 2, 14, 9]
523
- sheet.add_row ["", 5, 10, 15, 20]
524
- sheet.add_chart(Axlsx::ScatterChart, :title => "example 7: Scatter Chart") do |chart|
525
- chart.start_at 0, 4
526
- chart.end_at 10, 19
527
- chart.add_series :xData => sheet["B1:E1"], :yData => sheet["B2:E2"], :title => sheet["A1"], :color => "FF0000"
528
- chart.add_series :xData => sheet["B3:E3"], :yData => sheet["B4:E4"], :title => sheet["A3"], :color => "00FF00"
529
- end
530
- end
531
- end
532
- #```
533
-
534
-
535
- ##Tables
536
-
537
- #```ruby
538
- if examples.include? :tables
539
- wb.add_worksheet(:name => "Table") do |sheet|
540
- sheet.add_row ["Build Matrix"]
541
- sheet.add_row ["Build", "Duration", "Finished", "Rvm"]
542
- sheet.add_row ["19.1", "1 min 32 sec", "about 10 hours ago", "1.8.7"]
543
- sheet.add_row ["19.2", "1 min 28 sec", "about 10 hours ago", "1.9.2"]
544
- sheet.add_row ["19.3", "1 min 35 sec", "about 10 hours ago", "1.9.3"]
545
- sheet.add_table "A2:D5", :name => 'Build Matrix', :style_info => { :name => "TableStyleMedium23" }
546
- end
547
- end
548
- #```
549
-
550
-
551
- ##Fit to page printing
552
-
553
- #```ruby
554
- if examples.include? :fit_to_page
555
- wb.add_worksheet(:name => "fit to page") do |sheet|
556
- sheet.add_row ['this all goes on one page']
557
- sheet.page_setup.fit_to :width => 1, :height => 1
558
- end
559
- end
560
- ##```
561
-
562
-
563
- ##Hide Gridlines in worksheet
564
-
565
- #```ruby
566
- if examples.include? :hide_gridlines
567
- wb.add_worksheet(:name => "No Gridlines") do |sheet|
568
- sheet.add_row ["This", "Sheet", "Hides", "Gridlines"]
569
- sheet.sheet_view.show_grid_lines = false
570
- end
571
- end
572
- ##```
573
-
574
- # Repeat printing of header rows.
575
- #```ruby
576
- if examples.include? :repeated_header
577
- wb.add_worksheet(:name => "repeated header") do |sheet|
578
- sheet.add_row %w(These Column Header Will Render On Every Printed Sheet)
579
- 200.times { sheet.add_row %w(1 2 3 4 5 6 7 8) }
580
- wb.add_defined_name("'repeated header'!$1:$1", :local_sheet_id => sheet.index, :name => '_xlnm.Print_Titles')
581
- end
582
- end
583
-
584
- # Defined Names in formula
585
- if examples.include? :defined_name
586
- wb.add_worksheet(:name => 'defined name') do |sheet|
587
- sheet.add_row [1, 2, 17, '=FOOBAR']
588
- wb.add_defined_name("'defined name'!$C1", :local_sheet_id => sheet.index, :name => 'FOOBAR')
589
- wb.add_defined_name("'defined name'!$A$1:$C$1", :local_sheet_id => sheet.index, :name => '_xlnm.Print_Area')
590
- end
591
- end
592
-
593
- # Sheet Protection and excluding cells from locking.
594
- if examples.include? :sheet_protection
595
- unlocked = wb.styles.add_style :locked => false
596
- wb.add_worksheet(:name => 'Sheet Protection') do |sheet|
597
- sheet.sheet_protection do |protection|
598
- protection.password = 'fish'
599
- protection.auto_filter = false
600
- end
601
-
602
- sheet.add_row [1, 2 ,3], :style => unlocked # These cells will be locked
603
- sheet.add_row [4, 5, 6]
604
- sheet.add_row [7, 8, 9]
605
- sheet.auto_filter = "A1:C3"
606
- end
607
-
608
- end
609
-
610
- ##Specify page margins and other options for printing
611
-
612
- #```ruby
613
- if examples.include? :printing
614
- margins = {:left => 3, :right => 3, :top => 1.2, :bottom => 1.2, :header => 0.7, :footer => 0.7}
615
- setup = {:fit_to_width => 1, :orientation => :landscape, :paper_width => "297mm", :paper_height => "210mm"}
616
- options = {:grid_lines => true, :headings => true, :horizontal_centered => true}
617
- wb.add_worksheet(:name => "print margins", :page_margins => margins, :page_setup => setup, :print_options => options) do |sheet|
618
- sheet.add_row ["this sheet uses customized print settings"]
619
- end
620
- end
621
- #```
622
-
623
- ## Add headers and footers to a worksheet
624
- #``` ruby
625
- if examples.include? :header_footer
626
- header_footer = {:different_first => false, :odd_header => '&L&F : &A&R&D &T', :odd_footer => '&C&Pof&N'}
627
- wb.add_worksheet(:name => "header footer", :header_footer => header_footer) do |sheet|
628
- sheet.add_row ["this sheet has a header and a footer"]
629
- end
630
- end
631
- #```
632
-
633
- ## Add Comments to your spreadsheet
634
- #``` ruby
635
- if examples.include? :comments
636
- wb.add_worksheet(:name => 'comments') do |sheet|
637
- sheet.add_row ['Can we build it?']
638
- sheet.add_comment :ref => 'A1', :author => 'Bob', :text => 'Yes We Can!'
639
- sheet.add_comment :ref => 'A2', :author => 'Bob', :text => 'Yes We Can! - but I dont think you need to know about it!', :visible => false
640
-
641
- end
642
- end
643
-
644
- ## Frozen/Split panes
645
- ## ``` ruby
646
- if examples.include? :panes
647
- wb.add_worksheet(:name => 'panes') do |sheet|
648
- sheet.add_row(['', (0..99).map { |i| "column header #{i}" }].flatten )
649
- 100.times.with_index { |index| sheet << ["row header", (0..index).to_a].flatten }
650
- sheet.sheet_view.pane do |pane|
651
- pane.top_left_cell = "B2"
652
- pane.state = :frozen_split
653
- pane.y_split = 1
654
- pane.x_split = 1
655
- pane.active_pane = :bottom_right
656
- end
657
- end
658
- end
659
-
660
- if examples.include? :sheet_view
661
- ws = wb.add_worksheet(:name => 'SheetView - Split')
662
- ws.sheet_view do |vs|
663
- vs.pane do |pane|
664
- pane.active_pane = :top_right
665
- pane.state = :split
666
- pane.x_split = 11080
667
- pane.y_split = 5000
668
- pane.top_left_cell = 'C44'
669
- end
670
-
671
- vs.add_selection(:top_left, { :active_cell => 'A2', :sqref => 'A2' })
672
- vs.add_selection(:top_right, { :active_cell => 'I10', :sqref => 'I10' })
673
- vs.add_selection(:bottom_left, { :active_cell => 'E55', :sqref => 'E55' })
674
- vs.add_selection(:bottom_right, { :active_cell => 'I57', :sqref => 'I57' })
675
- end
676
-
677
- ws = wb.add_worksheet :name => "Sheetview - Frozen"
678
- ws.sheet_view do |vs|
679
- vs.pane do |pane|
680
- pane.state = :frozen
681
- pane.x_split = 3
682
- pane.y_split = 4
683
- end
684
- end
685
- end
686
-
687
- ## Book Views
688
- #
689
- ## Book views let you specify which sheet the show as active when the user opens the work book as well as a bunch of other
690
- ## tuning values for the UI @see Axlsx::WorkbookView
691
- ## ```ruby
692
- if examples.include? :book_view
693
- # when you open example.xml the second sheet is selected, and the horizontal scroll bar is much smaller showing more sheets
694
- wb.add_view tab_ratio: 800, active_tab: 1
695
- end
696
-
697
- ## Hiding Sheets
698
- ##
699
- ## Sheets can be hidden with the state attribute
700
- if examples.include? :hiding_sheets
701
- wb.add_worksheet name: 'hidden', state: :hidden do |sheet|
702
- sheet.add_row ['you cant see me!']
703
- end
704
- wb.add_worksheet name: 'very hidden', state: :very_hidden do |sheet|
705
- sheet.add_row ['you really cant see me!']
706
- end
707
- end
708
- # conditional formatting
709
- #
710
- if examples.include? :conditional_formatting
711
- percent = wb.styles.add_style(:format_code => "0.00%", :border => Axlsx::STYLE_THIN_BORDER)
712
- money = wb.styles.add_style(:format_code => '0,000', :border => Axlsx::STYLE_THIN_BORDER)
713
- status = wb.styles.add_style(:border => Axlsx::STYLE_THIN_BORDER)
714
-
715
- # define the style for conditional formatting
716
- profitable = wb.styles.add_style( :fg_color => "FF428751", :type => :dxf )
717
- unprofitable = wb.styles.add_style( :fg_color => "FF0000", :type => :dxf )
718
-
719
- wb.add_worksheet(:name => "Conditional Cell Is") do |sheet|
720
-
721
- # Generate 20 rosheet of data
722
- sheet.add_row ["Previous Year Quarterly Profits (JPY)"]
723
- sheet.add_row ["Quarter", "Profit", "% of Total"]
724
- offset = 3
725
- rosheet = 20
726
- offset.upto(rosheet + offset) do |i|
727
- sheet.add_row ["Q#{i}", 10000*((rosheet/2-i) * (rosheet/2-i)), "=100*B#{i}/SUM(B3:B#{rosheet+offset})"], :style=>[nil, money, percent]
728
- end
729
-
730
- # Apply conditional formatting to range B3:B100 in the worksheet
731
- sheet.add_conditional_formatting("B3:B100", { :type => :cellIs, :operator => :greaterThan, :formula => "100000", :dxfId => profitable, :priority => 1 })
732
- # Apply conditional using the between operator; NOTE: supply an array to :formula for between/notBetween
733
- sheet.add_conditional_formatting("C3:C100", { :type => :cellIs, :operator => :between, :formula => ["0.00%","100.00%"], :dxfId => unprofitable, :priority => 1 })
734
- end
735
-
736
- wb.add_worksheet(:name => "Conditional Color Scale") do |sheet|
737
- sheet.add_row ["Previous Year Quarterly Profits (JPY)"]
738
- sheet.add_row ["Quarter", "Profit", "% of Total"]
739
- offset = 3
740
- rosheet = 20
741
- offset.upto(rosheet + offset) do |i|
742
- sheet.add_row ["Q#{i}", 10000*((rosheet/2-i) * (rosheet/2-i)), "=100*B#{i}/SUM(B3:B#{rosheet+offset})"], :style=>[nil, money, percent]
743
- end
744
- # color scale has two_tone and three_tone class methods to setup the excel defaults (2011)
745
- # alternatively, you can pass in {:type => [:min, :max, :percent], :val => [whatever], :color =>[Some RGB String] to create a customized color scale object
746
-
747
- color_scale = Axlsx::ColorScale.three_tone
748
- sheet.add_conditional_formatting("B3:B100", { :type => :colorScale, :operator => :greaterThan, :formula => "100000", :dxfId => profitable, :priority => 1, :color_scale => color_scale })
749
- end
750
-
751
-
752
- wb.add_worksheet(:name => "Conditional Data Bar") do |sheet|
753
- sheet.add_row ["Previous Year Quarterly Profits (JPY)"]
754
- sheet.add_row ["Quarter", "Profit", "% of Total"]
755
- offset = 3
756
- rows = 20
757
- offset.upto(rows + offset) do |i|
758
- sheet.add_row ["Q#{i}", 10000*((rows/2-i) * (rows/2-i)), "=100*B#{i}/SUM(B3:B#{rows+offset})"], :style=>[nil, money, percent]
759
- end
760
- # Apply conditional formatting to range B3:B100 in the worksheet
761
- data_bar = Axlsx::DataBar.new
762
- sheet.add_conditional_formatting("B3:B100", { :type => :dataBar, :dxfId => profitable, :priority => 1, :data_bar => data_bar })
763
- end
764
-
765
- wb.add_worksheet(:name => "Conditional Format Icon Set") do |sheet|
766
- sheet.add_row ["Previous Year Quarterly Profits (JPY)"]
767
- sheet.add_row ["Quarter", "Profit", "% of Total"]
768
- offset = 3
769
- rows = 20
770
- offset.upto(rows + offset) do |i|
771
- sheet.add_row ["Q#{i}", 10000*((rows/2-i) * (rows/2-i)), "=100*B#{i}/SUM(B3:B#{rows+offset})"], :style=>[nil, money, percent]
772
- end
773
- # Apply conditional formatting to range B3:B100 in the worksheet
774
- icon_set = Axlsx::IconSet.new
775
- sheet.add_conditional_formatting("B3:B100", { :type => :iconSet, :dxfId => profitable, :priority => 1, :icon_set => icon_set })
776
- end
777
-
778
- wb.add_worksheet(:name => "Contains Text") do |sheet|
779
- sheet.add_row ["Previous Year Quarterly Profits (JPY)"]
780
- sheet.add_row ["Quarter", "Profit", "% of Total", "Status"]
781
- offset = 3
782
- rows = 20
783
- offset.upto(rows + offset) do |i|
784
- sheet.add_row ["Q#{i}", 10000*((rows/2-i) * (rows/2-i)), "=100*B#{i}/SUM(B3:B#{rows+offset})", (10000*((rows/2-i) * (rows/2-i))) > 100000 ? "PROFIT" : "LOSS"], :style=>[nil, money, percent, status]
785
- end
786
- # Apply conditional formatting to range D3:D100 in the worksheet to match words.
787
- sheet.add_conditional_formatting("D3:D100", { :type => :containsText, :operator => :equal, :text => "PROFIT", :dxfId => profitable, :priority => 1 })
788
- sheet.add_conditional_formatting("D3:D100", { :type => :containsText, :operator => :equal, :text => "LOSS", :dxfId => unprofitable, :priority => 1 })
789
- end
790
- end
791
-
792
- # Page Breaks
793
- if examples.include? :page_breaks
794
- ws = wb.add_worksheet(:name => "page breaks") do |sheet|
795
- sheet.add_row ["A"] * 10
796
- sheet.add_row ["A"] * 10
797
- sheet.add_page_break("B2")
798
- sheet.sheet_view.view = :page_layout # so you can see the breaks!
799
- end
800
- end
801
-
802
- #Validate and Serialize
803
-
804
- #```ruby
805
- # Serialize directly to file
806
-
807
- p.serialize("example.xlsx")
808
-
809
- # or
810
-
811
- #Serialize to a stream
812
- if examples.include? :streaming
813
- s = p.to_stream()
814
- File.open('example_streamed.xlsx', 'w') { |f| f.write(s.read) }
815
- end
816
- #```
817
-
818
- ##Using Shared Strings
819
-
820
- #```ruby
821
- # This is required by Numbers
822
- if examples.include? :shared_strings
823
- p.use_shared_strings = true
824
- p.serialize("shared_strings_example.xlsx")
825
- end
826
- #```
827
-
828
- #```ruby
829
- if examples.include? :no_autowidth
830
- p = Axlsx::Package.new
831
- p.use_autowidth = false
832
- wb = p.workbook
833
- wb.add_worksheet(:name => "Manual Widths") do | sheet |
834
- sheet.add_row ['oh look! no autowidth']
835
- end
836
- p.serialize("no-use_autowidth.xlsx")
837
- end
838
- #```
839
-
840
- #```ruby
841
- if examples.include? :cached_formula
842
- p = Axlsx::Package.new
843
- p.use_shared_strings = true
844
- wb = p.workbook
845
- wb.add_worksheet(:name => "cached formula") do | sheet |
846
- sheet.add_row [1, 2, '=A1+B1'], :formula_values => [nil, nil, 3]
847
- end
848
- p.serialize 'cached_formula.xlsx'
849
- end
850
- #```
851
-
852
- #```ruby
853
- if examples.include? :rich_text
854
- p = Axlsx::Package.new
855
- p.use_shared_strings = true
856
- wb = p.workbook
857
- wrap_text = wb.styles.add_style({:alignment => {:horizontal => :center, :vertical => :center, :wrap_text => true}} )
858
- rt = Axlsx::RichText.new
859
- rt.add_run('I\'m bold, ', :b => true)
860
- rt.add_run('I\'m italic, ', :i => true)
861
- rt.add_run('I\'m strike' + "\n", :strike => true)
862
- rt.add_run('I\'m bold, italic and strike' + "\n", :b => true, :i => true, :strike => true)
863
- rt.add_run('I\'m style-less :D')
864
- wb.add_worksheet(:name => "RichText") do | sheet |
865
- sheet.add_row [rt], :style => wrap_text
866
- end
867
- p.serialize 'rich_text.xlsx'
868
- end
869
- #```
870
-
871
- ##Change tab color of sheet
872
-
873
- #```ruby
874
- if examples.include? :tab_color
875
- p = Axlsx::Package.new
876
- p.use_shared_strings = true
877
- wb = p.workbook
878
- wb.add_worksheet(:name => "Change Tab Color") do |sheet|
879
- sheet.add_row ["Check", "out", "the", "Tab Color", "below!"]
880
- sheet.sheet_pr.tab_color = "FFFF6666"
881
- end
882
- p.serialize 'tab_color.xlsx'
883
- end
884
- ##```
885
-