axlsx 1.2.0 → 2.0.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.
- checksums.yaml +7 -0
- data/.yardopts +3 -2
- data/.yardopts_guide +19 -0
- data/CHANGELOG.md +64 -1
- data/README.md +105 -87
- data/Rakefile +0 -5
- data/examples/2010_comments.rb +17 -0
- data/examples/anchor_swapping.rb +28 -0
- data/examples/auto_filter.rb +16 -0
- data/examples/basic_charts.rb +4 -0
- data/examples/colored_links.rb +59 -0
- data/examples/conditional_formatting/example_conditional_formatting.rb +4 -2
- data/examples/example.rb +569 -258
- data/examples/ios_preview.rb +14 -0
- data/examples/page_setup.rb +11 -0
- data/examples/pivot_table.rb +39 -0
- data/examples/skydrive/real_example.rb +8 -8
- data/examples/styles.rb +66 -0
- data/examples/underline.rb +13 -0
- data/examples/wrap_text.rb +21 -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/doc_props/core.rb +6 -1
- data/lib/axlsx/drawing/axes.rb +61 -0
- data/lib/axlsx/drawing/axis.rb +20 -17
- data/lib/axlsx/drawing/bar_3D_chart.rb +31 -22
- data/lib/axlsx/drawing/cat_axis.rb +2 -10
- data/lib/axlsx/drawing/chart.rb +33 -6
- data/lib/axlsx/drawing/d_lbls.rb +21 -31
- data/lib/axlsx/drawing/drawing.rb +11 -17
- data/lib/axlsx/drawing/graphic_frame.rb +3 -3
- data/lib/axlsx/drawing/hyperlink.rb +42 -36
- data/lib/axlsx/drawing/line_3D_chart.rb +28 -70
- data/lib/axlsx/drawing/line_chart.rb +99 -0
- data/lib/axlsx/drawing/line_series.rb +20 -2
- data/lib/axlsx/drawing/marker.rb +38 -18
- 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 +28 -21
- data/lib/axlsx/drawing/pic.rb +43 -45
- data/lib/axlsx/drawing/picture_locking.rb +12 -44
- data/lib/axlsx/drawing/pie_3D_chart.rb +2 -1
- data/lib/axlsx/drawing/scaling.rb +13 -13
- data/lib/axlsx/drawing/scatter_chart.rb +32 -21
- data/lib/axlsx/drawing/ser_axis.rb +15 -16
- 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/two_cell_anchor.rb +7 -27
- data/lib/axlsx/drawing/val_axis.rb +14 -13
- data/lib/axlsx/drawing/view_3D.rb +52 -38
- data/lib/axlsx/drawing/vml_shape.rb +20 -79
- data/lib/axlsx/package.rb +58 -28
- data/lib/axlsx/rels/relationship.rb +75 -9
- data/lib/axlsx/rels/relationships.rb +8 -1
- 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 +20 -17
- 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 +23 -14
- data/lib/axlsx/stylesheet/pattern_fill.rb +12 -21
- data/lib/axlsx/stylesheet/styles.rb +37 -22
- 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 +64 -0
- data/lib/axlsx/util/constants.rb +127 -6
- data/lib/axlsx/util/options_parser.rb +15 -0
- data/lib/axlsx/util/serialized_attributes.rb +79 -0
- data/lib/axlsx/util/simple_typed_list.rb +50 -15
- data/lib/axlsx/util/validators.rb +51 -16
- data/lib/axlsx/version.rb +2 -2
- data/lib/axlsx/workbook/defined_name.rb +14 -59
- data/lib/axlsx/workbook/shared_strings_table.rb +13 -4
- data/lib/axlsx/workbook/workbook.rb +85 -6
- 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/break.rb +37 -0
- data/lib/axlsx/workbook/worksheet/cell.rb +109 -107
- data/lib/axlsx/workbook/worksheet/cell_serializer.rb +144 -0
- data/lib/axlsx/workbook/worksheet/cfvo.rb +15 -15
- data/lib/axlsx/workbook/worksheet/cfvos.rb +15 -0
- data/lib/axlsx/workbook/worksheet/col.rb +43 -29
- data/lib/axlsx/workbook/worksheet/col_breaks.rb +35 -0
- data/lib/axlsx/workbook/worksheet/color_scale.rb +62 -28
- data/lib/axlsx/workbook/worksheet/comment.rb +28 -37
- data/lib/axlsx/workbook/worksheet/comments.rb +3 -3
- data/lib/axlsx/workbook/worksheet/conditional_formatting.rb +11 -11
- data/lib/axlsx/workbook/worksheet/conditional_formatting_rule.rb +31 -27
- data/lib/axlsx/workbook/worksheet/data_bar.rb +75 -41
- data/lib/axlsx/workbook/worksheet/data_validation.rb +61 -62
- data/lib/axlsx/workbook/worksheet/date_time_converter.rb +6 -5
- data/lib/axlsx/workbook/worksheet/dimension.rb +0 -1
- data/lib/axlsx/workbook/worksheet/header_footer.rb +54 -0
- 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/pivot_table.rb +273 -0
- data/lib/axlsx/workbook/worksheet/pivot_table_cache_definition.rb +66 -0
- data/lib/axlsx/workbook/worksheet/pivot_tables.rb +24 -0
- 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 +59 -83
- data/lib/axlsx/workbook/worksheet/row_breaks.rb +33 -0
- 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_format_pr.rb +60 -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 +25 -19
- data/lib/axlsx/workbook/worksheet/table_style_info.rb +51 -0
- data/lib/axlsx/workbook/worksheet/tables.rb +1 -1
- data/lib/axlsx/workbook/worksheet/worksheet.rb +174 -35
- data/lib/axlsx/workbook/worksheet/worksheet_comments.rb +6 -5
- data/lib/axlsx/workbook/worksheet/worksheet_drawing.rb +4 -10
- data/lib/axlsx/workbook/worksheet/worksheet_hyperlink.rb +74 -0
- data/lib/axlsx/workbook/worksheet/worksheet_hyperlinks.rb +38 -0
- data/lib/axlsx.rb +55 -5
- data/lib/schema/sml.xsd +4 -0
- data/test/axlsx.qcachegrind +2226 -0
- data/test/benchmark.rb +2 -3
- data/test/content_type/tc_default.rb +0 -11
- data/test/content_type/tc_override.rb +0 -13
- data/test/doc_props/tc_core.rb +7 -0
- data/test/drawing/tc_axes.rb +8 -0
- data/test/drawing/tc_axis.rb +15 -17
- data/test/drawing/tc_bar_3D_chart.rb +6 -0
- data/test/drawing/tc_cat_axis.rb +9 -9
- data/test/drawing/tc_chart.rb +20 -0
- data/test/drawing/tc_d_lbls.rb +14 -4
- data/test/drawing/tc_drawing.rb +0 -5
- data/test/drawing/tc_graphic_frame.rb +5 -3
- data/test/drawing/tc_hyperlink.rb +0 -4
- data/test/drawing/tc_line_chart.rb +39 -0
- data/test/drawing/tc_line_series.rb +8 -2
- data/test/drawing/tc_pic.rb +14 -3
- data/test/drawing/tc_ser_axis.rb +13 -12
- data/test/drawing/tc_two_cell_anchor.rb +3 -3
- data/test/drawing/tc_val_axis.rb +6 -6
- data/test/drawing/tc_vml_shape.rb +9 -3
- data/test/profile.rb +11 -21
- data/test/rels/tc_relationship.rb +32 -9
- data/test/rels/tc_relationships.rb +12 -1
- data/test/stylesheet/tc_color.rb +6 -0
- data/test/stylesheet/tc_styles.rb +15 -2
- data/test/tc_axlsx.rb +35 -2
- data/test/tc_helper.rb +7 -1
- data/test/tc_package.rb +44 -5
- data/test/util/tc_serialized_attributes.rb +19 -0
- data/test/util/tc_validators.rb +26 -1
- data/test/workbook/tc_defined_name.rb +2 -2
- data/test/workbook/tc_shared_strings_table.rb +6 -0
- data/test/workbook/tc_workbook.rb +38 -1
- 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_break.rb +49 -0
- data/test/workbook/worksheet/tc_cell.rb +40 -6
- data/test/workbook/worksheet/tc_col.rb +11 -1
- data/test/workbook/worksheet/tc_color_scale.rb +31 -2
- data/test/workbook/worksheet/tc_comment.rb +21 -6
- data/test/workbook/worksheet/tc_comments.rb +2 -2
- data/test/workbook/worksheet/tc_conditional_formatting.rb +7 -0
- data/test/workbook/worksheet/tc_data_bar.rb +7 -0
- data/test/workbook/worksheet/tc_date_time_converter.rb +3 -11
- data/test/workbook/worksheet/tc_header_footer.rb +151 -0
- 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_pivot_table.rb +120 -0
- data/test/workbook/worksheet/tc_pivot_table_cache_definition.rb +54 -0
- 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_format_pr.rb +88 -0
- data/test/workbook/worksheet/tc_sheet_pr.rb +27 -0
- data/test/workbook/worksheet/tc_sheet_view.rb +39 -39
- data/test/workbook/worksheet/{table/tc_table.rb → tc_table.rb} +8 -3
- data/test/workbook/worksheet/tc_table_style_info.rb +53 -0
- data/test/workbook/worksheet/tc_worksheet.rb +131 -16
- data/test/workbook/worksheet/tc_worksheet_hyperlink.rb +55 -0
- metadata +97 -64
- data/examples/example.xlsx +0 -0
- data/examples/example_streamed.xlsx +0 -0
- data/examples/extractive.pdf +0 -0
- data/examples/image1.gif +0 -0
- data/examples/image1.jpg +0 -0
- data/examples/image1.png +0 -0
- data/examples/no-use_autowidth.xlsx +0 -0
- data/examples/sample.png +0 -0
- data/examples/scraping_html.rb +0 -91
- data/examples/shared_strings_example.xlsx +0 -0
- data/examples/sheet_view.rb +0 -34
- data/examples/two_cell_anchor_image.rb +0 -11
- data/lib/axlsx/workbook/worksheet/auto_filter.rb +0 -34
data/examples/example.rb
CHANGED
|
@@ -1,20 +1,66 @@
|
|
|
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 << :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 << :outline_level
|
|
18
|
+
examples << :merge_cells
|
|
19
|
+
examples << :images
|
|
20
|
+
examples << :format_dates
|
|
21
|
+
examples << :mbcs
|
|
22
|
+
examples << :formula
|
|
23
|
+
examples << :auto_filter
|
|
24
|
+
examples << :data_types
|
|
25
|
+
examples << :override_data_types
|
|
26
|
+
examples << :hyperlinks
|
|
27
|
+
examples << :number_currency_format
|
|
28
|
+
examples << :venezuela_currency
|
|
29
|
+
examples << :bar_chart
|
|
30
|
+
examples << :chart_gridlines
|
|
31
|
+
examples << :pie_chart
|
|
32
|
+
examples << :line_chart
|
|
33
|
+
examples << :scatter_chart
|
|
34
|
+
examples << :tables
|
|
35
|
+
examples << :fit_to_page
|
|
36
|
+
examples << :hide_gridlines
|
|
37
|
+
examples << :repeated_header
|
|
38
|
+
examples << :defined_name
|
|
39
|
+
examples << :printing
|
|
40
|
+
examples << :header_footer
|
|
41
|
+
examples << :comments
|
|
42
|
+
examples << :panes
|
|
43
|
+
examples << :sheet_view
|
|
44
|
+
examples << :conditional_formatting
|
|
45
|
+
examples << :streaming
|
|
46
|
+
examples << :shared_strings
|
|
47
|
+
examples << :no_autowidth
|
|
48
|
+
examples << :cached_formula
|
|
49
|
+
examples << :page_breaks
|
|
7
50
|
|
|
8
51
|
p = Axlsx::Package.new
|
|
9
52
|
wb = p.workbook
|
|
10
53
|
#```
|
|
11
54
|
|
|
12
|
-
|
|
55
|
+
## A Simple Workbook
|
|
13
56
|
|
|
14
57
|
#```ruby
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
58
|
+
if examples.include? :basic
|
|
59
|
+
wb.add_worksheet(:name => "Basic Worksheet") do |sheet|
|
|
60
|
+
sheet.add_row ["First Column", "Second", "Third"]
|
|
61
|
+
sheet.add_row [1, 2, 3]
|
|
62
|
+
sheet.add_row [' preserving whitespace']
|
|
63
|
+
end
|
|
18
64
|
end
|
|
19
65
|
#```
|
|
20
66
|
|
|
@@ -24,123 +70,222 @@ end
|
|
|
24
70
|
# Each cell allows a single, predified style.
|
|
25
71
|
# 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
72
|
# Alternatively, you can apply a style to an entire row by using an integer value for :style.
|
|
73
|
+
if examples.include? :custom_styles
|
|
74
|
+
wb.styles do |s|
|
|
75
|
+
black_cell = s.add_style :bg_color => "00", :fg_color => "FF", :sz => 14, :alignment => { :horizontal=> :center }
|
|
76
|
+
blue_cell = s.add_style :bg_color => "0000FF", :fg_color => "FF", :sz => 20, :alignment => { :horizontal=> :center }
|
|
77
|
+
wb.add_worksheet(:name => "Custom Styles") do |sheet|
|
|
78
|
+
|
|
79
|
+
# Applies the black_cell style to the first and third cell, and the blue_cell style to the second.
|
|
80
|
+
sheet.add_row ["Text Autowidth", "Second", "Third"], :style => [black_cell, blue_cell, black_cell]
|
|
81
|
+
|
|
82
|
+
# Applies the thin border to all three cells
|
|
83
|
+
sheet.add_row [1, 2, 3], :style => Axlsx::STYLE_THIN_BORDER
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
end
|
|
27
87
|
|
|
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
88
|
|
|
36
|
-
|
|
37
|
-
|
|
89
|
+
#```ruby
|
|
90
|
+
# 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.
|
|
91
|
+
if examples.include? :wrap_text
|
|
92
|
+
wb.styles do |s|
|
|
93
|
+
wrap_text = s.add_style :fg_color=> "FFFFFF",
|
|
94
|
+
:b => true,
|
|
95
|
+
:bg_color => "004586",
|
|
96
|
+
:sz => 12,
|
|
97
|
+
:border => { :style => :thin, :color => "00" },
|
|
98
|
+
:alignment => { :horizontal => :center,
|
|
99
|
+
:vertical => :center ,
|
|
100
|
+
:wrap_text => true}
|
|
101
|
+
wb.add_worksheet(:name => 'wrap text') do |sheet|
|
|
102
|
+
sheet.add_row ['Torp, White and Cronin'], :style=>wrap_text
|
|
103
|
+
sheet.column_info.first.width = 5
|
|
104
|
+
end
|
|
38
105
|
end
|
|
39
106
|
end
|
|
40
|
-
#```
|
|
41
107
|
|
|
42
108
|
##Styling Cell Overrides
|
|
43
109
|
|
|
44
110
|
#```ruby
|
|
45
111
|
#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
|
-
|
|
112
|
+
if examples.include? :cell_style_override
|
|
113
|
+
wb.add_worksheet(:name => "Cell Level Style Overrides") do |sheet|
|
|
114
|
+
|
|
115
|
+
# this will set the font size for each cell.
|
|
116
|
+
sheet.add_row ['col 1', 'col 2', 'col 3', 'col 4'], :sz => 16
|
|
117
|
+
|
|
118
|
+
sheet.add_row [1, 2, 3, "=SUM(A2:C2)"]
|
|
119
|
+
sheet.add_row %w(u shadow sz b i strike outline)
|
|
120
|
+
sheet.rows.last.cells[0].u = :double
|
|
121
|
+
sheet.rows.last.cells[1].shadow = true
|
|
122
|
+
sheet.rows.last.cells[2].sz = 20
|
|
123
|
+
sheet.rows.last.cells[3].b = true
|
|
124
|
+
sheet.rows.last.cells[4].i = true
|
|
125
|
+
sheet.rows.last.cells[5].strike = true
|
|
126
|
+
sheet.rows.last.cells[6].outline = 1
|
|
127
|
+
# You can also apply cell style overrides to a range of cells
|
|
128
|
+
sheet["A1:D1"].each { |c| c.color = "FF0000" }
|
|
129
|
+
sheet['A1:D2'].each { |c| c.style = Axlsx::STYLE_THIN_BORDER }
|
|
130
|
+
end
|
|
57
131
|
end
|
|
58
|
-
|
|
132
|
+
##```
|
|
59
133
|
|
|
60
134
|
##Using Custom Border Styles
|
|
61
135
|
|
|
62
136
|
#```ruby
|
|
63
137
|
#Axlsx defines a thin border style, but you can easily create and use your own.
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
138
|
+
if examples.include? :custom_borders
|
|
139
|
+
wb.styles do |s|
|
|
140
|
+
red_border = s.add_style :border => { :style => :thick, :color =>"FFFF0000", :edges => [:left, :right] }
|
|
141
|
+
blue_border = s.add_style :border => { :style => :thick, :color =>"FF0000FF"}
|
|
142
|
+
|
|
143
|
+
wb.add_worksheet(:name => "Custom Borders") do |sheet|
|
|
144
|
+
sheet.add_row ["wrap", "me", "Up in Red"], :style => red_border
|
|
145
|
+
sheet.add_row [1, 2, 3], :style => blue_border
|
|
146
|
+
end
|
|
71
147
|
end
|
|
72
148
|
end
|
|
73
|
-
#```
|
|
74
149
|
|
|
150
|
+
#```ruby
|
|
151
|
+
# More Custom Borders
|
|
152
|
+
if examples.include? :surrounding_border
|
|
153
|
+
|
|
154
|
+
# Stuff like this is why I LOVE RUBY
|
|
155
|
+
# If you dont know about hash default values
|
|
156
|
+
# LEARN IT! LIVE IT! LOVE IT!
|
|
157
|
+
defaults = { :style => :thick, :color => "000000" }
|
|
158
|
+
borders = Hash.new do |hash, key|
|
|
159
|
+
hash[key] = wb.styles.add_style :border => defaults.merge( { :edges => key.to_s.split('_').map(&:to_sym) } )
|
|
160
|
+
end
|
|
161
|
+
top_row = [0, borders[:top_left], borders[:top], borders[:top], borders[:top_right]]
|
|
162
|
+
middle_row = [0, borders[:left], nil, nil, borders[:right]]
|
|
163
|
+
bottom_row = [0, borders[:bottom_left], borders[:bottom], borders[:bottom], borders[:bottom_right]]
|
|
75
164
|
|
|
76
|
-
|
|
165
|
+
wb.add_worksheet(:name => "Surrounding Border") do |ws|
|
|
166
|
+
ws.add_row []
|
|
167
|
+
ws.add_row ['', 1,2,3,4], :style => top_row
|
|
168
|
+
ws.add_row ['', 5,6,7,8], :style => middle_row
|
|
169
|
+
ws.add_row ['', 9, 10, 11, 12]
|
|
77
170
|
|
|
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]
|
|
171
|
+
#This works too!
|
|
172
|
+
ws.rows.last.style = bottom_row
|
|
87
173
|
|
|
88
|
-
|
|
89
|
-
|
|
174
|
+
end
|
|
175
|
+
end
|
|
90
176
|
|
|
91
|
-
|
|
92
|
-
|
|
177
|
+
#```ruby
|
|
178
|
+
# Hacking border styles
|
|
179
|
+
if examples.include? :deep_custom_borders
|
|
180
|
+
wb.styles do |s|
|
|
181
|
+
top_bottom = s.add_style :border => { :style => :thick, :color =>"FFFF0000", :edges => [:top, :bottom] }
|
|
182
|
+
border = s.borders[s.cellXfs[top_bottom].borderId]
|
|
183
|
+
# edit existing border parts
|
|
184
|
+
border.prs.each do |part|
|
|
185
|
+
case part.name
|
|
186
|
+
when :top
|
|
187
|
+
part.color = Axlsx::Color.new(:rgb => "FFFF0000")
|
|
188
|
+
when :bottom
|
|
189
|
+
part.color = Axlsx::Color.new(:rgb => "FF00FF00")
|
|
190
|
+
end
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
border.prs << Axlsx::BorderPr.new(:name => :left, :color => Axlsx::Color.new(:rgb => '0000FF'), :style => :mediumDashed)
|
|
194
|
+
wb.add_worksheet(:name => 'hacked borders') do |sheet|
|
|
195
|
+
sheet.add_row [1,2,3], :style=>top_bottom
|
|
196
|
+
end
|
|
197
|
+
end
|
|
198
|
+
end
|
|
199
|
+
##```
|
|
93
200
|
|
|
94
|
-
#Hide the 5th column
|
|
95
|
-
sheet.column_info[4].hidden = true
|
|
96
201
|
|
|
97
|
-
|
|
98
|
-
sheet.column_info[1].outlineLevel = 2
|
|
202
|
+
##Styling Rows and Columns
|
|
99
203
|
|
|
100
|
-
|
|
101
|
-
|
|
204
|
+
#```ruby
|
|
205
|
+
if examples.include? :row_column_style
|
|
206
|
+
wb.styles do |s|
|
|
207
|
+
head = s.add_style :bg_color => "00", :fg_color => "FF"
|
|
208
|
+
percent = s.add_style :num_fmt => 9
|
|
209
|
+
wb.add_worksheet(:name => "Columns and Rows") do |sheet|
|
|
210
|
+
sheet.add_row ['col 1', 'col 2', 'col 3', 'col 4', 'col5']
|
|
211
|
+
sheet.add_row [1, 2, 0.3, 4, 5.0]
|
|
212
|
+
sheet.add_row [1, 2, 0.2, 4, 5.0]
|
|
213
|
+
sheet.add_row [1, 2, 0.1, 4, 5.0]
|
|
214
|
+
|
|
215
|
+
#apply the percent style to the column at index 2 skipping the first row.
|
|
216
|
+
sheet.col_style 2, percent, :row_offset => 1
|
|
217
|
+
|
|
218
|
+
# apply the head style to the first row.
|
|
219
|
+
sheet.row_style 0, head
|
|
220
|
+
|
|
221
|
+
#Hide the 5th column
|
|
222
|
+
sheet.column_info[4].hidden = true
|
|
223
|
+
|
|
224
|
+
#Set the second column outline level
|
|
225
|
+
sheet.column_info[1].outlineLevel = 2
|
|
226
|
+
|
|
227
|
+
sheet.rows[3].hidden = true
|
|
228
|
+
sheet.rows[1].outlineLevel = 2
|
|
229
|
+
end
|
|
102
230
|
end
|
|
103
231
|
end
|
|
104
|
-
|
|
232
|
+
##```
|
|
105
233
|
|
|
106
234
|
|
|
107
235
|
##Specifying Column Widths
|
|
108
236
|
|
|
109
237
|
#```ruby
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
238
|
+
if examples.include? :fixed_column_width
|
|
239
|
+
wb.add_worksheet(:name => "custom column widths") do |sheet|
|
|
240
|
+
sheet.add_row ["I use autowidth and am very wide", "I use a custom width and am narrow"]
|
|
241
|
+
sheet.add_row ['abcdefg', 'This is a very long text and should flow into the right cell', nil, 'xxx' ]
|
|
242
|
+
sheet.column_widths nil, 3, 5, nil
|
|
243
|
+
end
|
|
114
244
|
end
|
|
115
|
-
|
|
245
|
+
|
|
246
|
+
#```ruby
|
|
247
|
+
if examples.include? :outline_level
|
|
248
|
+
wb.add_worksheet(name: 'outline_level') do |sheet|
|
|
249
|
+
sheet.add_row [1, 2, 3, Time.now, 5, 149455.15]
|
|
250
|
+
sheet.add_row [1, 2, 5, 6, 5,14100.19]
|
|
251
|
+
sheet.add_row [9500002267, 1212, 1212, 5,14100.19]
|
|
252
|
+
sheet.outline_level_rows 0, 2
|
|
253
|
+
sheet.outline_level_columns 0, 2
|
|
254
|
+
end
|
|
255
|
+
end
|
|
256
|
+
##```
|
|
116
257
|
|
|
117
258
|
##Merging Cells.
|
|
118
259
|
|
|
119
260
|
#```ruby
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
261
|
+
if examples.include? :merge_cells
|
|
262
|
+
wb.add_worksheet(:name => 'Merging Cells') do |sheet|
|
|
263
|
+
# cell level style overides when adding cells
|
|
264
|
+
sheet.add_row ["col 1", "col 2", "col 3", "col 4"], :sz => 16
|
|
265
|
+
sheet.add_row [1, 2, 3, "=SUM(A2:C2)"]
|
|
266
|
+
sheet.add_row [2, 3, 4, "=SUM(A3:C3)"]
|
|
267
|
+
sheet.add_row ["total", "", "", "=SUM(D2:D3)"]
|
|
268
|
+
sheet.merge_cells("A4:C4")
|
|
269
|
+
sheet["A1:D1"].each { |c| c.color = "FF0000"}
|
|
270
|
+
sheet["A1:D4"].each { |c| c.style = Axlsx::STYLE_THIN_BORDER }
|
|
271
|
+
end
|
|
129
272
|
end
|
|
130
|
-
|
|
273
|
+
##```
|
|
131
274
|
|
|
132
275
|
##Add an Image with a hyperlink
|
|
133
276
|
|
|
134
277
|
#```ruby
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
image
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
278
|
+
if examples.include? :images
|
|
279
|
+
wb.add_worksheet(:name => "Image with Hyperlink") do |sheet|
|
|
280
|
+
img = File.expand_path('../image1.jpeg', __FILE__)
|
|
281
|
+
# specifying the :hyperlink option will add a hyper link to your image.
|
|
282
|
+
# @note - Numbers does not support this part of the specification.
|
|
283
|
+
sheet.add_image(:image_src => img, :noSelect => true, :noMove => true, :hyperlink=>"http://axlsx.blogspot.com") do |image|
|
|
284
|
+
image.width=720
|
|
285
|
+
image.height=666
|
|
286
|
+
image.hyperlink.tooltip = "Labeled Link"
|
|
287
|
+
image.start_at 2, 2
|
|
288
|
+
end
|
|
144
289
|
end
|
|
145
290
|
end
|
|
146
291
|
#```
|
|
@@ -148,15 +293,17 @@ end
|
|
|
148
293
|
##Using Custom Formatting and date1904
|
|
149
294
|
|
|
150
295
|
#```ruby
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
296
|
+
if examples.include? :format_dates
|
|
297
|
+
require 'date'
|
|
298
|
+
wb.styles do |s|
|
|
299
|
+
date = s.add_style(:format_code => "yyyy-mm-dd", :border => Axlsx::STYLE_THIN_BORDER)
|
|
300
|
+
padded = s.add_style(:format_code => "00#", :border => Axlsx::STYLE_THIN_BORDER)
|
|
301
|
+
percent = s.add_style(:format_code => "0000%", :border => Axlsx::STYLE_THIN_BORDER)
|
|
302
|
+
# wb.date1904 = true # Use the 1904 date system (Used by Excel for Mac < 2011)
|
|
303
|
+
wb.add_worksheet(:name => "Formatting Data") do |sheet|
|
|
304
|
+
sheet.add_row ["Custom Formatted Date", "Percent Formatted Float", "Padded Numbers"], :style => Axlsx::STYLE_THIN_BORDER
|
|
305
|
+
sheet.add_row [Date::strptime('2012-01-19','%Y-%m-%d'), 0.2, 32], :style => [date, percent, padded]
|
|
306
|
+
end
|
|
160
307
|
end
|
|
161
308
|
end
|
|
162
309
|
#```
|
|
@@ -164,69 +311,126 @@ end
|
|
|
164
311
|
##Asian Language Support
|
|
165
312
|
|
|
166
313
|
#```ruby
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
314
|
+
if examples.include? :mbcs
|
|
315
|
+
wb.styles.fonts.first.name = 'Arial Unicode MS'
|
|
316
|
+
wb.add_worksheet(:name => "日本語でのシート名") do |sheet|
|
|
317
|
+
sheet.add_row ["日本語"]
|
|
318
|
+
sheet.add_row ["华语/華語"]
|
|
319
|
+
sheet.add_row ["한국어/조선말"]
|
|
320
|
+
end
|
|
171
321
|
end
|
|
172
|
-
|
|
322
|
+
##```
|
|
173
323
|
|
|
174
324
|
##Using formula
|
|
175
325
|
|
|
176
326
|
#```ruby
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
327
|
+
if examples.include? :formula
|
|
328
|
+
wb.add_worksheet(:name => "Using Formulas") do |sheet|
|
|
329
|
+
sheet.add_row ['col 1', 'col 2', 'col 3', 'col 4']
|
|
330
|
+
sheet.add_row [1, 2, 3, "=SUM(A2:C2)"]
|
|
331
|
+
end
|
|
180
332
|
end
|
|
181
|
-
|
|
333
|
+
##```
|
|
182
334
|
|
|
183
335
|
##Auto Filter
|
|
184
336
|
|
|
185
337
|
#```ruby
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
338
|
+
if examples.include? :auto_filter
|
|
339
|
+
wb.add_worksheet(:name => "Auto Filter") do |sheet|
|
|
340
|
+
sheet.add_row ["Build Matrix"]
|
|
341
|
+
sheet.add_row ["Build", "Duration", "Finished", "Rvm"]
|
|
342
|
+
sheet.add_row ["19.1", "1 min 32 sec", "about 10 hours ago", "1.8.7"]
|
|
343
|
+
sheet.add_row ["19.2", "1 min 28 sec", "about 10 hours ago", "1.9.2"]
|
|
344
|
+
sheet.add_row ["19.3", "1 min 35 sec", "about 10 hours ago", "1.9.3"]
|
|
345
|
+
sheet.auto_filter = "A2:D5"
|
|
346
|
+
sheet.auto_filter.add_column 3, :filters, :filter_items => ['1.9.2', '1.8.7']
|
|
347
|
+
end
|
|
193
348
|
end
|
|
194
349
|
#```
|
|
195
350
|
|
|
196
351
|
##Automatic cell types
|
|
197
352
|
|
|
353
|
+
|
|
198
354
|
#```ruby
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
355
|
+
if examples.include? :data_types
|
|
356
|
+
wb.add_worksheet(:name => "Automatic cell types") do |sheet|
|
|
357
|
+
date_format = wb.styles.add_style :format_code => 'YYYY-MM-DD'
|
|
358
|
+
time_format = wb.styles.add_style :format_code => 'hh:mm:ss'
|
|
359
|
+
sheet.add_row ["Date", "Time", "String", "Boolean", "Float", "Integer"]
|
|
360
|
+
sheet.add_row [Date.today, Time.now, "value", true, 0.1, 1], :style => [date_format, time_format]
|
|
361
|
+
end
|
|
204
362
|
end
|
|
205
|
-
#```
|
|
206
363
|
|
|
364
|
+
#```ruby
|
|
365
|
+
if examples.include? :override_data_types
|
|
366
|
+
wb.add_worksheet(:name => "Override Data Type") do |sheet|
|
|
367
|
+
sheet.add_row ['dont eat my zeros!', '0088'] , :types => [nil, :string]
|
|
368
|
+
end
|
|
369
|
+
end
|
|
370
|
+
# Hyperlinks in worksheet
|
|
371
|
+
if examples.include? :hyperlinks
|
|
372
|
+
wb.add_worksheet(:name => 'hyperlinks') do |sheet|
|
|
373
|
+
# external references
|
|
374
|
+
sheet.add_row ['axlsx']
|
|
375
|
+
sheet.add_hyperlink :location => 'https://github.com/randym/axlsx', :ref => sheet.rows.first.cells.first
|
|
376
|
+
# internal references
|
|
377
|
+
sheet.add_hyperlink :location => "'Next Sheet'!A1", :ref => 'A2', :target => :sheet
|
|
378
|
+
sheet.add_row ['next sheet']
|
|
379
|
+
end
|
|
380
|
+
|
|
381
|
+
wb.add_worksheet(:name => 'Next Sheet') do |sheet|
|
|
382
|
+
sheet.add_row ['hello!']
|
|
383
|
+
end
|
|
384
|
+
end
|
|
385
|
+
###```
|
|
386
|
+
|
|
387
|
+
##Number formatting and currency
|
|
388
|
+
if examples.include? :number_currency_format
|
|
389
|
+
wb.add_worksheet(:name => "Formats and Currency") do |sheet|
|
|
390
|
+
currency = wb.styles.add_style :num_fmt => 5
|
|
391
|
+
red_negative = wb.styles.add_style :num_fmt => 8
|
|
392
|
+
comma = wb.styles.add_style :num_fmt => 3
|
|
393
|
+
super_funk = wb.styles.add_style :format_code => '[Green]#'
|
|
394
|
+
sheet.add_row %w(Currency RedNegative Comma Custom)
|
|
395
|
+
sheet.add_row [1500, -122.34, 123456789, 594829], :style=> [currency, red_negative, comma, super_funk]
|
|
396
|
+
end
|
|
397
|
+
end
|
|
398
|
+
|
|
399
|
+
## Venezuala currency
|
|
400
|
+
if examples.include? :venezuela_currency
|
|
401
|
+
wb.add_worksheet(:name => 'Venezuala_currency') do |sheet|
|
|
402
|
+
number = wb.styles.add_style :format_code => '#.##0\,00'
|
|
403
|
+
sheet.add_row [2.5] , :style => [number]
|
|
404
|
+
end
|
|
405
|
+
end
|
|
207
406
|
|
|
208
407
|
##Generating A Bar Chart
|
|
209
408
|
|
|
210
409
|
#```ruby
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
410
|
+
if examples.include? :bar_chart
|
|
411
|
+
wb.add_worksheet(:name => "Bar Chart") do |sheet|
|
|
412
|
+
sheet.add_row ["A Simple Bar Chart"]
|
|
413
|
+
%w(first second third).each { |label| sheet.add_row [label, rand(24)+1] }
|
|
414
|
+
sheet.add_chart(Axlsx::Bar3DChart, :start_at => "A6", :end_at => "F20") do |chart|
|
|
415
|
+
chart.add_series :data => sheet["B2:B4"], :labels => sheet["A2:A4"], :title => sheet["A1"], :colors => ["00FF00", "0000FF"]
|
|
416
|
+
end
|
|
216
417
|
end
|
|
217
418
|
end
|
|
218
|
-
|
|
419
|
+
|
|
420
|
+
##```
|
|
219
421
|
|
|
220
422
|
##Hide Gridlines in chart
|
|
221
423
|
|
|
222
424
|
#```ruby
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
425
|
+
if examples.include? :chart_gridlines
|
|
426
|
+
wb.add_worksheet(:name => "Chart With No Gridlines") do |sheet|
|
|
427
|
+
sheet.add_row ["Bar Chart without gridlines"]
|
|
428
|
+
%w(first second third).each { |label| sheet.add_row [label, rand(24)+1] }
|
|
429
|
+
sheet.add_chart(Axlsx::Bar3DChart, :start_at => "A6", :end_at => "F20") do |chart|
|
|
430
|
+
chart.add_series :data => sheet["B2:B4"], :labels => sheet["A2:A4"], :colors => ["00FF00", "FF0000"]
|
|
431
|
+
chart.valAxis.gridlines = false
|
|
432
|
+
chart.catAxis.gridlines = false
|
|
433
|
+
end
|
|
230
434
|
end
|
|
231
435
|
end
|
|
232
436
|
#```
|
|
@@ -234,11 +438,13 @@ end
|
|
|
234
438
|
##Generating A Pie Chart
|
|
235
439
|
|
|
236
440
|
#```ruby
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
441
|
+
if examples.include? :pie_chart
|
|
442
|
+
wb.add_worksheet(:name => "Pie Chart") do |sheet|
|
|
443
|
+
sheet.add_row ["Simple Pie Chart"]
|
|
444
|
+
%w(first second third).each { |label| sheet.add_row [label, rand(24)+1] }
|
|
445
|
+
sheet.add_chart(Axlsx::Pie3DChart, :start_at => [0,5], :end_at => [10, 20], :title => "example 3: Pie Chart") do |chart|
|
|
446
|
+
chart.add_series :data => sheet["B2:B4"], :labels => sheet["A2:A4"], :colors => ['FF0000', '00FF00', '0000FF']
|
|
447
|
+
end
|
|
242
448
|
end
|
|
243
449
|
end
|
|
244
450
|
#```
|
|
@@ -246,19 +452,30 @@ end
|
|
|
246
452
|
##Generating A Line Chart
|
|
247
453
|
|
|
248
454
|
#```ruby
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
455
|
+
if examples.include? :line_chart
|
|
456
|
+
wb.add_worksheet(:name => "Line Chart") do |sheet|
|
|
457
|
+
sheet.add_row ["Simple Line Chart"]
|
|
458
|
+
sheet.add_row %w(first second)
|
|
459
|
+
4.times do
|
|
460
|
+
sheet.add_row [ rand(24)+1, rand(24)+1]
|
|
461
|
+
end
|
|
462
|
+
sheet.add_chart(Axlsx::Line3DChart, :title => "Simple 3D Line Chart", :rotX => 30, :rotY => 20) do |chart|
|
|
463
|
+
chart.start_at 0, 5
|
|
464
|
+
chart.end_at 10, 20
|
|
465
|
+
chart.add_series :data => sheet["A3:A6"], :title => sheet["A2"], :color => "0000FF"
|
|
466
|
+
chart.add_series :data => sheet["B3:B6"], :title => sheet["B2"], :color => "FF0000"
|
|
467
|
+
chart.catAxis.title = 'X Axis'
|
|
468
|
+
chart.valAxis.title = 'Y Axis'
|
|
469
|
+
end
|
|
470
|
+
sheet.add_chart(Axlsx::LineChart, :title => "Simple Line Chart", :rotX => 30, :rotY => 20) do |chart|
|
|
471
|
+
chart.start_at 0, 21
|
|
472
|
+
chart.end_at 10, 41
|
|
473
|
+
chart.add_series :data => sheet["A3:A6"], :title => sheet["A2"], :color => "FF0000"
|
|
474
|
+
chart.add_series :data => sheet["B3:B6"], :title => sheet["B2"], :color => "00FF00"
|
|
475
|
+
chart.catAxis.title = 'X Axis'
|
|
476
|
+
chart.valAxis.title = 'Y Axis'
|
|
477
|
+
end
|
|
478
|
+
|
|
262
479
|
end
|
|
263
480
|
end
|
|
264
481
|
#```
|
|
@@ -266,16 +483,18 @@ end
|
|
|
266
483
|
##Generating A Scatter Chart
|
|
267
484
|
|
|
268
485
|
#```ruby
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
486
|
+
if examples.include? :scatter_chart
|
|
487
|
+
wb.add_worksheet(:name => "Scatter Chart") do |sheet|
|
|
488
|
+
sheet.add_row ["First", 1, 5, 7, 9]
|
|
489
|
+
sheet.add_row ["", 1, 25, 49, 81]
|
|
490
|
+
sheet.add_row ["Second", 5, 2, 14, 9]
|
|
491
|
+
sheet.add_row ["", 5, 10, 15, 20]
|
|
492
|
+
sheet.add_chart(Axlsx::ScatterChart, :title => "example 7: Scatter Chart") do |chart|
|
|
493
|
+
chart.start_at 0, 4
|
|
494
|
+
chart.end_at 10, 19
|
|
495
|
+
chart.add_series :xData => sheet["B1:E1"], :yData => sheet["B2:E2"], :title => sheet["A1"], :color => "FF0000"
|
|
496
|
+
chart.add_series :xData => sheet["B3:E3"], :yData => sheet["B4:E4"], :title => sheet["A3"], :color => "00FF00"
|
|
497
|
+
end
|
|
279
498
|
end
|
|
280
499
|
end
|
|
281
500
|
#```
|
|
@@ -284,13 +503,15 @@ end
|
|
|
284
503
|
##Tables
|
|
285
504
|
|
|
286
505
|
#```ruby
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
506
|
+
if examples.include? :tables
|
|
507
|
+
wb.add_worksheet(:name => "Table") do |sheet|
|
|
508
|
+
sheet.add_row ["Build Matrix"]
|
|
509
|
+
sheet.add_row ["Build", "Duration", "Finished", "Rvm"]
|
|
510
|
+
sheet.add_row ["19.1", "1 min 32 sec", "about 10 hours ago", "1.8.7"]
|
|
511
|
+
sheet.add_row ["19.2", "1 min 28 sec", "about 10 hours ago", "1.9.2"]
|
|
512
|
+
sheet.add_row ["19.3", "1 min 35 sec", "about 10 hours ago", "1.9.3"]
|
|
513
|
+
sheet.add_table "A2:D5", :name => 'Build Matrix', :style_info => { :name => "TableStyleMedium23" }
|
|
514
|
+
end
|
|
294
515
|
end
|
|
295
516
|
#```
|
|
296
517
|
|
|
@@ -298,9 +519,11 @@ end
|
|
|
298
519
|
##Fit to page printing
|
|
299
520
|
|
|
300
521
|
#```ruby
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
522
|
+
if examples.include? :fit_to_page
|
|
523
|
+
wb.add_worksheet(:name => "fit to page") do |sheet|
|
|
524
|
+
sheet.add_row ['this all goes on one page']
|
|
525
|
+
sheet.page_setup.fit_to :width => 1, :height => 1
|
|
526
|
+
end
|
|
304
527
|
end
|
|
305
528
|
##```
|
|
306
529
|
|
|
@@ -308,159 +531,247 @@ end
|
|
|
308
531
|
##Hide Gridlines in worksheet
|
|
309
532
|
|
|
310
533
|
#```ruby
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
534
|
+
if examples.include? :hide_gridlines
|
|
535
|
+
wb.add_worksheet(:name => "No Gridlines") do |sheet|
|
|
536
|
+
sheet.add_row ["This", "Sheet", "Hides", "Gridlines"]
|
|
537
|
+
sheet.sheet_view.show_grid_lines = false
|
|
538
|
+
end
|
|
314
539
|
end
|
|
315
540
|
##```
|
|
316
541
|
|
|
542
|
+
# Repeat printing of header rows.
|
|
317
543
|
#```ruby
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
544
|
+
if examples.include? :repeated_header
|
|
545
|
+
wb.add_worksheet(:name => "repeated header") do |sheet|
|
|
546
|
+
sheet.add_row %w(These Column Header Will Render On Every Printed Sheet)
|
|
547
|
+
200.times { sheet.add_row %w(1 2 3 4 5 6 7 8) }
|
|
548
|
+
wb.add_defined_name("'repeated header'!$1:$1", :local_sheet_id => sheet.index, :name => '_xlnm.Print_Titles')
|
|
549
|
+
end
|
|
322
550
|
end
|
|
323
551
|
|
|
324
|
-
#
|
|
325
|
-
|
|
326
|
-
wb.add_worksheet(:name => '
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
552
|
+
# Defined Names in formula
|
|
553
|
+
if examples.include? :defined_name
|
|
554
|
+
wb.add_worksheet(:name => 'defined name') do |sheet|
|
|
555
|
+
sheet.add_row [1, 2, 17, '=FOOBAR']
|
|
556
|
+
wb.add_defined_name("'defined name'!$C1", :local_sheet_id => sheet.index, :name => 'FOOBAR')
|
|
557
|
+
end
|
|
330
558
|
end
|
|
331
559
|
|
|
560
|
+
# Sheet Protection and excluding cells from locking.
|
|
561
|
+
if examples.include? :sheet_protection
|
|
562
|
+
unlocked = wb.styles.add_style :locked => false
|
|
563
|
+
wb.add_worksheet(:name => 'Sheet Protection') do |sheet|
|
|
564
|
+
sheet.sheet_protection.password = 'fish'
|
|
565
|
+
sheet.add_row [1, 2 ,3] # These cells will be locked
|
|
566
|
+
sheet.add_row [4, 5, 6], :style => unlocked # these cells will not!
|
|
567
|
+
end
|
|
568
|
+
end
|
|
332
569
|
|
|
333
570
|
##Specify page margins and other options for printing
|
|
334
571
|
|
|
335
572
|
#```ruby
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
573
|
+
if examples.include? :printing
|
|
574
|
+
margins = {:left => 3, :right => 3, :top => 1.2, :bottom => 1.2, :header => 0.7, :footer => 0.7}
|
|
575
|
+
setup = {:fit_to_width => 1, :orientation => :landscape, :paper_width => "297mm", :paper_height => "210mm"}
|
|
576
|
+
options = {:grid_lines => true, :headings => true, :horizontal_centered => true}
|
|
577
|
+
wb.add_worksheet(:name => "print margins", :page_margins => margins, :page_setup => setup, :print_options => options) do |sheet|
|
|
578
|
+
sheet.add_row ["this sheet uses customized print settings"]
|
|
579
|
+
end
|
|
580
|
+
end
|
|
581
|
+
#```
|
|
582
|
+
|
|
583
|
+
## Add headers and footers to a worksheet
|
|
584
|
+
#``` ruby
|
|
585
|
+
if examples.include? :header_footer
|
|
586
|
+
header_footer = {:different_first => false, :odd_header => '&L&F : &A&R&D &T', :odd_footer => '&C&Pof&N'}
|
|
587
|
+
wb.add_worksheet(:name => "header footer", :header_footer => header_footer) do |sheet|
|
|
588
|
+
sheet.add_row ["this sheet has a header and a footer"]
|
|
589
|
+
end
|
|
341
590
|
end
|
|
342
591
|
#```
|
|
343
592
|
|
|
344
|
-
## Add Comments to your spreadsheet
|
|
593
|
+
## Add Comments to your spreadsheet
|
|
345
594
|
#``` ruby
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
595
|
+
if examples.include? :comments
|
|
596
|
+
wb.add_worksheet(:name => 'comments') do |sheet|
|
|
597
|
+
sheet.add_row ['Can we build it?']
|
|
598
|
+
sheet.add_comment :ref => 'A1', :author => 'Bob', :text => 'Yes We Can!'
|
|
599
|
+
sheet.add_comment :ref => 'A2', :author => 'Bob', :text => 'Yes We Can! - but I dont think you need to know about it!', :visible => false
|
|
600
|
+
|
|
601
|
+
end
|
|
349
602
|
end
|
|
350
603
|
|
|
351
604
|
## Frozen/Split panes
|
|
352
605
|
## ``` ruby
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
pane
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
606
|
+
if examples.include? :panes
|
|
607
|
+
wb.add_worksheet(:name => 'panes') do |sheet|
|
|
608
|
+
sheet.add_row(['', (0..99).map { |i| "column header #{i}" }].flatten )
|
|
609
|
+
100.times.with_index { |index| sheet << ["row header", (0..index).to_a].flatten }
|
|
610
|
+
sheet.sheet_view.pane do |pane|
|
|
611
|
+
pane.top_left_cell = "B2"
|
|
612
|
+
pane.state = :frozen_split
|
|
613
|
+
pane.y_split = 1
|
|
614
|
+
pane.x_split = 1
|
|
615
|
+
pane.active_pane = :bottom_right
|
|
616
|
+
end
|
|
362
617
|
end
|
|
363
618
|
end
|
|
364
619
|
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
620
|
+
if examples.include? :sheet_view
|
|
621
|
+
ws = wb.add_worksheet(:name => 'SheetView - Split')
|
|
622
|
+
ws.sheet_view do |vs|
|
|
623
|
+
vs.pane do |pane|
|
|
624
|
+
pane.active_pane = :top_right
|
|
625
|
+
pane.state = :split
|
|
626
|
+
pane.x_split = 11080
|
|
627
|
+
pane.y_split = 5000
|
|
628
|
+
pane.top_left_cell = 'C44'
|
|
629
|
+
end
|
|
630
|
+
|
|
631
|
+
vs.add_selection(:top_left, { :active_cell => 'A2', :sqref => 'A2' })
|
|
632
|
+
vs.add_selection(:top_right, { :active_cell => 'I10', :sqref => 'I10' })
|
|
633
|
+
vs.add_selection(:bottom_left, { :active_cell => 'E55', :sqref => 'E55' })
|
|
634
|
+
vs.add_selection(:bottom_right, { :active_cell => 'I57', :sqref => 'I57' })
|
|
635
|
+
end
|
|
369
636
|
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
637
|
+
ws = wb.add_worksheet :name => "Sheetview - Frozen"
|
|
638
|
+
ws.sheet_view do |vs|
|
|
639
|
+
vs.pane do |pane|
|
|
640
|
+
pane.state = :frozen
|
|
641
|
+
pane.x_split = 3
|
|
642
|
+
pane.y_split = 4
|
|
643
|
+
end
|
|
644
|
+
end
|
|
645
|
+
end
|
|
373
646
|
|
|
374
|
-
wb.add_worksheet(:name => "Conditional Cell Is") do |ws|
|
|
375
647
|
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
648
|
+
# conditional formatting
|
|
649
|
+
#
|
|
650
|
+
if examples.include? :conditional_formatting
|
|
651
|
+
percent = wb.styles.add_style(:format_code => "0.00%", :border => Axlsx::STYLE_THIN_BORDER)
|
|
652
|
+
money = wb.styles.add_style(:format_code => '0,000', :border => Axlsx::STYLE_THIN_BORDER)
|
|
653
|
+
|
|
654
|
+
# define the style for conditional formatting
|
|
655
|
+
profitable = wb.styles.add_style( :fg_color => "FF428751", :type => :dxf )
|
|
656
|
+
unprofitable = wb.styles.add_style( :fg_color => "FF0000", :type => :dxf )
|
|
657
|
+
|
|
658
|
+
wb.add_worksheet(:name => "Conditional Cell Is") do |sheet|
|
|
659
|
+
|
|
660
|
+
# Generate 20 rosheet of data
|
|
661
|
+
sheet.add_row ["Previous Year Quarterly Profits (JPY)"]
|
|
662
|
+
sheet.add_row ["Quarter", "Profit", "% of Total"]
|
|
663
|
+
offset = 3
|
|
664
|
+
rosheet = 20
|
|
665
|
+
offset.upto(rosheet + offset) do |i|
|
|
666
|
+
sheet.add_row ["Q#{i}", 10000*((rosheet/2-i) * (rosheet/2-i)), "=100*B#{i}/SUM(B3:B#{rosheet+offset})"], :style=>[nil, money, percent]
|
|
667
|
+
end
|
|
668
|
+
|
|
669
|
+
# Apply conditional formatting to range B3:B100 in the worksheet
|
|
670
|
+
sheet.add_conditional_formatting("B3:B100", { :type => :cellIs, :operator => :greaterThan, :formula => "100000", :dxfId => profitable, :priority => 1 })
|
|
671
|
+
# Apply conditional using the between operator; NOTE: supply an array to :formula for between/notBetween
|
|
672
|
+
sheet.add_conditional_formatting("C3:C100", { :type => :cellIs, :operator => :between, :formula => ["0.00%","100.00%"], :dxfId => unprofitable, :priority => 1 })
|
|
383
673
|
end
|
|
384
674
|
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
675
|
+
wb.add_worksheet(:name => "Conditional Color Scale") do |sheet|
|
|
676
|
+
sheet.add_row ["Previous Year Quarterly Profits (JPY)"]
|
|
677
|
+
sheet.add_row ["Quarter", "Profit", "% of Total"]
|
|
678
|
+
offset = 3
|
|
679
|
+
rosheet = 20
|
|
680
|
+
offset.upto(rosheet + offset) do |i|
|
|
681
|
+
sheet.add_row ["Q#{i}", 10000*((rosheet/2-i) * (rosheet/2-i)), "=100*B#{i}/SUM(B3:B#{rosheet+offset})"], :style=>[nil, money, percent]
|
|
682
|
+
end
|
|
683
|
+
# color scale has two_tone and three_tone class methods to setup the excel defaults (2011)
|
|
684
|
+
# alternatively, you can pass in {:type => [:min, :max, :percent], :val => [whatever], :color =>[Some RGB String] to create a customized color scale object
|
|
685
|
+
|
|
686
|
+
color_scale = Axlsx::ColorScale.three_tone
|
|
687
|
+
sheet.add_conditional_formatting("B3:B100", { :type => :colorScale, :operator => :greaterThan, :formula => "100000", :dxfId => profitable, :priority => 1, :color_scale => color_scale })
|
|
396
688
|
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
689
|
|
|
402
690
|
|
|
403
|
-
wb.add_worksheet(:name => "Conditional Data Bar") do |
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
691
|
+
wb.add_worksheet(:name => "Conditional Data Bar") do |sheet|
|
|
692
|
+
sheet.add_row ["Previous Year Quarterly Profits (JPY)"]
|
|
693
|
+
sheet.add_row ["Quarter", "Profit", "% of Total"]
|
|
694
|
+
offset = 3
|
|
695
|
+
rows = 20
|
|
696
|
+
offset.upto(rows + offset) do |i|
|
|
697
|
+
sheet.add_row ["Q#{i}", 10000*((rows/2-i) * (rows/2-i)), "=100*B#{i}/SUM(B3:B#{rows+offset})"], :style=>[nil, money, percent]
|
|
698
|
+
end
|
|
699
|
+
# Apply conditional formatting to range B3:B100 in the worksheet
|
|
700
|
+
data_bar = Axlsx::DataBar.new
|
|
701
|
+
sheet.add_conditional_formatting("B3:B100", { :type => :dataBar, :dxfId => profitable, :priority => 1, :data_bar => data_bar })
|
|
702
|
+
end
|
|
703
|
+
|
|
704
|
+
wb.add_worksheet(:name => "Conditional Format Icon Set") do |sheet|
|
|
705
|
+
sheet.add_row ["Previous Year Quarterly Profits (JPY)"]
|
|
706
|
+
sheet.add_row ["Quarter", "Profit", "% of Total"]
|
|
707
|
+
offset = 3
|
|
708
|
+
rows = 20
|
|
709
|
+
offset.upto(rows + offset) do |i|
|
|
710
|
+
sheet.add_row ["Q#{i}", 10000*((rows/2-i) * (rows/2-i)), "=100*B#{i}/SUM(B3:B#{rows+offset})"], :style=>[nil, money, percent]
|
|
711
|
+
end
|
|
712
|
+
# Apply conditional formatting to range B3:B100 in the worksheet
|
|
713
|
+
icon_set = Axlsx::IconSet.new
|
|
714
|
+
sheet.add_conditional_formatting("B3:B100", { :type => :iconSet, :dxfId => profitable, :priority => 1, :icon_set => icon_set })
|
|
410
715
|
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
716
|
end
|
|
415
717
|
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
ws.
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
718
|
+
# Page Breaks
|
|
719
|
+
if examples.include? :page_breaks
|
|
720
|
+
ws = wb.add_worksheet(:name => "page breaks") do |sheet|
|
|
721
|
+
sheet.add_row ["A"] * 10
|
|
722
|
+
sheet.add_row ["A"] * 10
|
|
723
|
+
sheet.add_page_break("B2")
|
|
724
|
+
sheet.sheet_view.view = :page_layout # so you can see the breaks!
|
|
423
725
|
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
726
|
end
|
|
428
727
|
|
|
429
|
-
|
|
728
|
+
#Validate and Serialize
|
|
430
729
|
|
|
431
730
|
#```ruby
|
|
432
731
|
# Serialize directly to file
|
|
732
|
+
|
|
433
733
|
p.serialize("example.xlsx")
|
|
434
734
|
|
|
435
735
|
# or
|
|
436
736
|
|
|
437
737
|
#Serialize to a stream
|
|
438
|
-
|
|
439
|
-
|
|
738
|
+
if examples.include? :streaming
|
|
739
|
+
s = p.to_stream()
|
|
740
|
+
File.open('example_streamed.xlsx', 'w') { |f| f.write(s.read) }
|
|
741
|
+
end
|
|
440
742
|
#```
|
|
441
743
|
|
|
442
744
|
##Using Shared Strings
|
|
443
745
|
|
|
444
746
|
#```ruby
|
|
445
747
|
# This is required by Numbers
|
|
446
|
-
|
|
447
|
-
p.
|
|
748
|
+
if examples.include? :shared_strings
|
|
749
|
+
p.use_shared_strings = true
|
|
750
|
+
p.serialize("shared_strings_example.xlsx")
|
|
751
|
+
end
|
|
448
752
|
#```
|
|
449
753
|
|
|
450
|
-
|
|
451
|
-
##Disabling Autowidth
|
|
452
|
-
|
|
453
754
|
#```ruby
|
|
454
|
-
|
|
455
|
-
p
|
|
456
|
-
|
|
457
|
-
wb
|
|
458
|
-
|
|
755
|
+
if examples.include? :no_autowidth
|
|
756
|
+
p = Axlsx::Package.new
|
|
757
|
+
p.use_autowidth = false
|
|
758
|
+
wb = p.workbook
|
|
759
|
+
wb.add_worksheet(:name => "Manual Widths") do | sheet |
|
|
760
|
+
sheet.add_row ['oh look! no autowidth']
|
|
761
|
+
end
|
|
762
|
+
p.serialize("no-use_autowidth.xlsx")
|
|
459
763
|
end
|
|
460
|
-
p.validate.each { |e| puts e.message }
|
|
461
|
-
p.serialize("no-use_autowidth.xlsx")
|
|
462
764
|
#```
|
|
463
765
|
|
|
464
766
|
|
|
465
767
|
|
|
768
|
+
if examples.include? :cached_formula
|
|
769
|
+
p = Axlsx::Package.new
|
|
770
|
+
p.use_shared_strings = true
|
|
771
|
+
wb = p.workbook
|
|
772
|
+
wb.add_worksheet(:name => "cached formula") do | sheet |
|
|
773
|
+
sheet.add_row [1, 2, '=A1+B1'], :formula_values => [nil, nil, 3]
|
|
774
|
+
end
|
|
775
|
+
p.serialize 'cached_formula.xlsx'
|
|
776
|
+
end
|
|
466
777
|
|