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