caxlsx 2.0.2 → 3.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +125 -30
- data/README.md +65 -151
- data/Rakefile +9 -11
- data/examples/{image1.jpeg → assets/image1.jpeg} +0 -0
- data/examples/generate.rb +15 -0
- data/lib/axlsx.rb +35 -17
- data/lib/axlsx/content_type/abstract_content_type.rb +1 -1
- data/lib/axlsx/content_type/content_type.rb +1 -1
- data/lib/axlsx/doc_props/app.rb +1 -1
- data/lib/axlsx/doc_props/core.rb +5 -5
- data/lib/axlsx/drawing/area_chart.rb +99 -0
- data/lib/axlsx/drawing/area_series.rb +110 -0
- data/lib/axlsx/drawing/axes.rb +1 -1
- data/lib/axlsx/drawing/axis.rb +12 -9
- data/lib/axlsx/drawing/bar_3D_chart.rb +13 -13
- data/lib/axlsx/drawing/bar_chart.rb +143 -0
- data/lib/axlsx/drawing/bar_series.rb +12 -14
- data/lib/axlsx/drawing/bubble_chart.rb +59 -0
- data/lib/axlsx/drawing/bubble_series.rb +63 -0
- data/lib/axlsx/drawing/cat_axis.rb +5 -5
- data/lib/axlsx/drawing/chart.rb +52 -8
- data/lib/axlsx/drawing/d_lbls.rb +4 -4
- data/lib/axlsx/drawing/drawing.rb +6 -1
- data/lib/axlsx/drawing/graphic_frame.rb +3 -3
- data/lib/axlsx/drawing/hyperlink.rb +1 -3
- data/lib/axlsx/drawing/line_3D_chart.rb +2 -2
- data/lib/axlsx/drawing/line_chart.rb +10 -10
- data/lib/axlsx/drawing/line_series.rb +32 -3
- data/lib/axlsx/drawing/marker.rb +1 -1
- data/lib/axlsx/drawing/num_data.rb +4 -4
- data/lib/axlsx/drawing/num_data_source.rb +6 -6
- data/lib/axlsx/drawing/num_val.rb +3 -1
- data/lib/axlsx/drawing/one_cell_anchor.rb +3 -2
- data/lib/axlsx/drawing/pic.rb +25 -19
- data/lib/axlsx/drawing/picture_locking.rb +1 -3
- data/lib/axlsx/drawing/pie_3D_chart.rb +5 -6
- data/lib/axlsx/drawing/pie_series.rb +6 -6
- data/lib/axlsx/drawing/scaling.rb +6 -6
- data/lib/axlsx/drawing/scatter_chart.rb +10 -10
- data/lib/axlsx/drawing/scatter_series.rb +40 -7
- data/lib/axlsx/drawing/ser_axis.rb +2 -2
- data/lib/axlsx/drawing/series.rb +3 -3
- data/lib/axlsx/drawing/series_title.rb +4 -2
- data/lib/axlsx/drawing/str_data.rb +3 -3
- data/lib/axlsx/drawing/str_val.rb +3 -1
- data/lib/axlsx/drawing/title.rb +23 -4
- data/lib/axlsx/drawing/two_cell_anchor.rb +6 -1
- data/lib/axlsx/drawing/val_axis.rb +1 -1
- data/lib/axlsx/drawing/view_3D.rb +2 -2
- data/lib/axlsx/drawing/vml_drawing.rb +1 -1
- data/lib/axlsx/package.rb +58 -47
- data/lib/axlsx/rels/relationship.rb +27 -26
- data/lib/axlsx/rels/relationships.rb +7 -4
- data/lib/axlsx/stylesheet/border_pr.rb +2 -2
- data/lib/axlsx/stylesheet/cell_alignment.rb +1 -3
- data/lib/axlsx/stylesheet/cell_protection.rb +1 -3
- data/lib/axlsx/stylesheet/cell_style.rb +1 -3
- data/lib/axlsx/stylesheet/color.rb +1 -3
- data/lib/axlsx/stylesheet/font.rb +11 -3
- data/lib/axlsx/stylesheet/gradient_stop.rb +1 -1
- data/lib/axlsx/stylesheet/num_fmt.rb +10 -3
- data/lib/axlsx/stylesheet/pattern_fill.rb +1 -1
- data/lib/axlsx/stylesheet/styles.rb +7 -7
- data/lib/axlsx/stylesheet/table_style_element.rb +1 -3
- data/lib/axlsx/util/accessors.rb +6 -6
- data/lib/axlsx/util/constants.rb +108 -99
- data/lib/axlsx/util/mime_type_utils.rb +11 -0
- data/lib/axlsx/util/options_parser.rb +2 -1
- data/lib/axlsx/util/serialized_attributes.rb +16 -6
- data/lib/axlsx/util/simple_typed_list.rb +28 -52
- data/lib/axlsx/util/storage.rb +4 -4
- data/lib/axlsx/util/validators.rb +31 -19
- data/lib/axlsx/util/zip_command.rb +73 -0
- data/lib/axlsx/version.rb +1 -1
- data/lib/axlsx/workbook/defined_name.rb +11 -12
- data/lib/axlsx/workbook/defined_names.rb +2 -2
- data/lib/axlsx/workbook/shared_strings_table.rb +5 -5
- data/lib/axlsx/workbook/workbook.rb +36 -20
- data/lib/axlsx/workbook/workbook_view.rb +80 -0
- data/lib/axlsx/workbook/workbook_views.rb +22 -0
- data/lib/axlsx/workbook/worksheet/auto_filter/auto_filter.rb +2 -2
- data/lib/axlsx/workbook/worksheet/auto_filter/filters.rb +1 -3
- data/lib/axlsx/workbook/worksheet/break.rb +1 -3
- data/lib/axlsx/workbook/worksheet/cell.rb +164 -75
- data/lib/axlsx/workbook/worksheet/cell_serializer.rb +63 -43
- data/lib/axlsx/workbook/worksheet/cfvo.rb +1 -3
- data/lib/axlsx/workbook/worksheet/cfvos.rb +4 -1
- data/lib/axlsx/workbook/worksheet/col.rb +14 -13
- data/lib/axlsx/workbook/worksheet/col_breaks.rb +2 -2
- data/lib/axlsx/workbook/worksheet/cols.rb +5 -2
- data/lib/axlsx/workbook/worksheet/comment.rb +5 -6
- data/lib/axlsx/workbook/worksheet/comments.rb +9 -12
- data/lib/axlsx/workbook/worksheet/conditional_formatting.rb +1 -1
- data/lib/axlsx/workbook/worksheet/conditional_formatting_rule.rb +1 -1
- data/lib/axlsx/workbook/worksheet/data_bar.rb +4 -6
- data/lib/axlsx/workbook/worksheet/data_validation.rb +8 -6
- data/lib/axlsx/workbook/worksheet/dimension.rb +2 -2
- data/lib/axlsx/workbook/worksheet/header_footer.rb +6 -8
- data/lib/axlsx/workbook/worksheet/icon_set.rb +3 -5
- data/lib/axlsx/workbook/worksheet/merged_cells.rb +4 -2
- data/lib/axlsx/workbook/worksheet/outline_pr.rb +33 -0
- data/lib/axlsx/workbook/worksheet/page_margins.rb +1 -3
- data/lib/axlsx/workbook/worksheet/page_set_up_pr.rb +1 -1
- data/lib/axlsx/workbook/worksheet/page_setup.rb +21 -23
- data/lib/axlsx/workbook/worksheet/pane.rb +1 -3
- data/lib/axlsx/workbook/worksheet/pivot_table.rb +44 -28
- data/lib/axlsx/workbook/worksheet/pivot_table_cache_definition.rb +4 -4
- data/lib/axlsx/workbook/worksheet/print_options.rb +1 -3
- data/lib/axlsx/workbook/worksheet/protected_range.rb +1 -3
- data/lib/axlsx/workbook/worksheet/protected_ranges.rb +5 -2
- data/lib/axlsx/workbook/worksheet/rich_text.rb +55 -0
- data/lib/axlsx/workbook/worksheet/rich_text_run.rb +250 -0
- data/lib/axlsx/workbook/worksheet/row.rb +42 -52
- data/lib/axlsx/workbook/worksheet/row_breaks.rb +2 -2
- data/lib/axlsx/workbook/worksheet/selection.rb +1 -3
- data/lib/axlsx/workbook/worksheet/sheet_data.rb +3 -1
- data/lib/axlsx/workbook/worksheet/sheet_pr.rb +21 -3
- data/lib/axlsx/workbook/worksheet/sheet_protection.rb +1 -3
- data/lib/axlsx/workbook/worksheet/table.rb +6 -6
- data/lib/axlsx/workbook/worksheet/table_style_info.rb +1 -3
- data/lib/axlsx/workbook/worksheet/tables.rb +4 -1
- data/lib/axlsx/workbook/worksheet/worksheet.rb +76 -81
- data/lib/axlsx/workbook/worksheet/worksheet_drawing.rb +10 -10
- data/lib/axlsx/workbook/worksheet/worksheet_hyperlinks.rb +3 -3
- data/lib/caxlsx.rb +2 -0
- data/test/drawing/tc_area_chart.rb +39 -0
- data/test/drawing/tc_area_series.rb +71 -0
- data/test/drawing/tc_axis.rb +27 -0
- data/test/drawing/tc_bar_chart.rb +71 -0
- data/test/drawing/tc_bubble_chart.rb +44 -0
- data/test/drawing/tc_bubble_series.rb +21 -0
- data/test/drawing/tc_chart.rb +23 -10
- data/test/drawing/tc_data_source.rb +6 -0
- data/test/drawing/tc_drawing.rb +4 -4
- data/test/drawing/tc_hyperlink.rb +1 -1
- data/test/drawing/tc_line_chart.rb +5 -5
- data/test/drawing/tc_line_series.rb +47 -6
- data/test/drawing/tc_one_cell_anchor.rb +1 -1
- data/test/drawing/tc_pic.rb +11 -15
- data/test/drawing/tc_pie_series.rb +2 -1
- data/test/drawing/tc_scatter_series.rb +36 -1
- data/test/drawing/tc_series_title.rb +21 -0
- data/test/drawing/tc_str_val.rb +9 -0
- data/test/drawing/tc_title.rb +21 -0
- data/test/fixtures/image1.gif +0 -0
- data/test/fixtures/image1.jpeg +0 -0
- data/test/fixtures/image1.jpg +0 -0
- data/test/fixtures/image1.png +0 -0
- data/test/fixtures/image1_fake.jpg +0 -0
- data/test/rels/tc_relationship.rb +8 -0
- data/test/stylesheet/tc_font.rb +14 -2
- data/test/stylesheet/tc_styles.rb +29 -3
- data/test/tc_axlsx.rb +37 -0
- data/test/tc_helper.rb +2 -0
- data/test/tc_package.rb +50 -13
- data/test/util/tc_mime_type_utils.rb +13 -0
- data/test/util/tc_simple_typed_list.rb +2 -3
- data/test/util/tc_validators.rb +35 -11
- data/test/workbook/tc_defined_name.rb +12 -4
- data/test/workbook/tc_shared_strings_table.rb +16 -1
- data/test/workbook/tc_workbook.rb +38 -3
- data/test/workbook/tc_workbook_view.rb +50 -0
- data/test/workbook/worksheet/auto_filter/tc_filters.rb +1 -1
- data/test/workbook/worksheet/tc_break.rb +1 -1
- data/test/workbook/worksheet/tc_cell.rb +143 -9
- data/test/workbook/worksheet/tc_col.rb +18 -3
- data/test/workbook/worksheet/tc_conditional_formatting.rb +2 -2
- data/test/workbook/worksheet/tc_data_bar.rb +1 -1
- data/test/workbook/worksheet/tc_data_validation.rb +11 -11
- data/test/workbook/worksheet/tc_header_footer.rb +2 -2
- data/test/workbook/worksheet/tc_icon_set.rb +1 -1
- data/test/workbook/worksheet/tc_outline_pr.rb +19 -0
- data/test/workbook/worksheet/tc_page_setup.rb +3 -3
- data/test/workbook/worksheet/tc_pivot_table.rb +21 -6
- data/test/workbook/worksheet/tc_print_options.rb +1 -1
- data/test/workbook/worksheet/tc_rich_text.rb +44 -0
- data/test/workbook/worksheet/tc_rich_text_run.rb +173 -0
- data/test/workbook/worksheet/tc_row.rb +24 -2
- data/test/workbook/worksheet/tc_sheet_calc_pr.rb +1 -1
- data/test/workbook/worksheet/tc_sheet_format_pr.rb +4 -4
- data/test/workbook/worksheet/tc_sheet_pr.rb +26 -4
- data/test/workbook/worksheet/tc_sheet_protection.rb +5 -5
- data/test/workbook/worksheet/tc_sheet_view.rb +4 -4
- data/test/workbook/worksheet/tc_table.rb +2 -3
- data/test/workbook/worksheet/tc_worksheet.rb +123 -60
- metadata +180 -128
- data/examples/2010_comments.rb +0 -17
- data/examples/anchor_swapping.rb +0 -28
- data/examples/auto_filter.rb +0 -16
- data/examples/basic_charts.rb +0 -58
- data/examples/chart_colors.rb +0 -88
- data/examples/colored_links.rb +0 -59
- data/examples/conditional_formatting/example_conditional_formatting.rb +0 -74
- data/examples/conditional_formatting/getting_barred.rb +0 -37
- data/examples/conditional_formatting/hitting_the_high_notes.rb +0 -37
- data/examples/conditional_formatting/scaled_colors.rb +0 -39
- data/examples/conditional_formatting/stop_and_go.rb +0 -37
- data/examples/data_validation.rb +0 -50
- data/examples/example.rb +0 -777
- data/examples/extractive.rb +0 -45
- data/examples/ios_preview.rb +0 -14
- data/examples/page_setup.rb +0 -11
- data/examples/pivot_table.rb +0 -39
- data/examples/sheet_protection.rb +0 -10
- data/examples/skydrive/real_example.rb +0 -63
- data/examples/styles.rb +0 -66
- data/examples/underline.rb +0 -13
- data/examples/wrap_text.rb +0 -21
- data/lib/axlsx/util/parser.rb +0 -44
@@ -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'
|
data/examples/data_validation.rb
DELETED
@@ -1,50 +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
|
-
ws.add_data_validation("A10", {
|
9
|
-
:type => :whole,
|
10
|
-
:operator => :between,
|
11
|
-
:formula1 => '5',
|
12
|
-
:formula2 => '10',
|
13
|
-
:showErrorMessage => true,
|
14
|
-
:errorTitle => 'Wrong input',
|
15
|
-
:error => 'Only values between 5 and 10',
|
16
|
-
:errorStyle => :information,
|
17
|
-
:showInputMessage => true,
|
18
|
-
:promptTitle => 'Be carful!',
|
19
|
-
:prompt => 'Only values between 5 and 10'})
|
20
|
-
|
21
|
-
ws.add_data_validation("B10", {
|
22
|
-
:type => :textLength,
|
23
|
-
:operator => :greaterThan,
|
24
|
-
:formula1 => '10',
|
25
|
-
:showErrorMessage => true,
|
26
|
-
:errorTitle => 'Text is too long',
|
27
|
-
:error => 'Max text length is 10 characters',
|
28
|
-
:errorStyle => :stop,
|
29
|
-
:showInputMessage => true,
|
30
|
-
:promptTitle => 'Text length',
|
31
|
-
:prompt => 'Max text length is 10 characters'})
|
32
|
-
|
33
|
-
8.times do |i|
|
34
|
-
ws.add_row [nil, nil, i*2]
|
35
|
-
end
|
36
|
-
|
37
|
-
ws.add_data_validation("C10", {
|
38
|
-
:type => :list,
|
39
|
-
:formula1 => 'C1:C8',
|
40
|
-
:showDropDown => false,
|
41
|
-
:showErrorMessage => true,
|
42
|
-
:errorTitle => '',
|
43
|
-
:error => 'Only values from C1:C8',
|
44
|
-
:errorStyle => :stop,
|
45
|
-
:showInputMessage => true,
|
46
|
-
:promptTitle => '',
|
47
|
-
:prompt => 'Only values from C1:C8'})
|
48
|
-
end
|
49
|
-
|
50
|
-
p.serialize 'data_validation.xlsx'
|
data/examples/example.rb
DELETED
@@ -1,777 +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 << :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
|
50
|
-
|
51
|
-
p = Axlsx::Package.new
|
52
|
-
wb = p.workbook
|
53
|
-
#```
|
54
|
-
|
55
|
-
## A Simple Workbook
|
56
|
-
|
57
|
-
#```ruby
|
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
|
64
|
-
end
|
65
|
-
#```
|
66
|
-
|
67
|
-
#Using Custom Styles
|
68
|
-
|
69
|
-
#```ruby
|
70
|
-
# Each cell allows a single, predified style.
|
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.
|
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
|
87
|
-
|
88
|
-
|
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
|
105
|
-
end
|
106
|
-
end
|
107
|
-
|
108
|
-
##Styling Cell Overrides
|
109
|
-
|
110
|
-
#```ruby
|
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.
|
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
|
131
|
-
end
|
132
|
-
##```
|
133
|
-
|
134
|
-
##Using Custom Border Styles
|
135
|
-
|
136
|
-
#```ruby
|
137
|
-
#Axlsx defines a thin border style, but you can easily create and use your own.
|
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
|
147
|
-
end
|
148
|
-
end
|
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]]
|
164
|
-
|
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]
|
170
|
-
|
171
|
-
#This works too!
|
172
|
-
ws.rows.last.style = bottom_row
|
173
|
-
|
174
|
-
end
|
175
|
-
end
|
176
|
-
|
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
|
-
##```
|
200
|
-
|
201
|
-
|
202
|
-
##Styling Rows and Columns
|
203
|
-
|
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
|
230
|
-
end
|
231
|
-
end
|
232
|
-
##```
|
233
|
-
|
234
|
-
|
235
|
-
##Specifying Column Widths
|
236
|
-
|
237
|
-
#```ruby
|
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
|
244
|
-
end
|
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
|
-
##```
|
257
|
-
|
258
|
-
##Merging Cells.
|
259
|
-
|
260
|
-
#```ruby
|
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
|
272
|
-
end
|
273
|
-
##```
|
274
|
-
|
275
|
-
##Add an Image with a hyperlink
|
276
|
-
|
277
|
-
#```ruby
|
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
|
289
|
-
end
|
290
|
-
end
|
291
|
-
#```
|
292
|
-
|
293
|
-
##Using Custom Formatting and date1904
|
294
|
-
|
295
|
-
#```ruby
|
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
|
307
|
-
end
|
308
|
-
end
|
309
|
-
#```
|
310
|
-
|
311
|
-
##Asian Language Support
|
312
|
-
|
313
|
-
#```ruby
|
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
|
321
|
-
end
|
322
|
-
##```
|
323
|
-
|
324
|
-
##Using formula
|
325
|
-
|
326
|
-
#```ruby
|
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
|
332
|
-
end
|
333
|
-
##```
|
334
|
-
|
335
|
-
##Auto Filter
|
336
|
-
|
337
|
-
#```ruby
|
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
|
348
|
-
end
|
349
|
-
#```
|
350
|
-
|
351
|
-
##Automatic cell types
|
352
|
-
|
353
|
-
|
354
|
-
#```ruby
|
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
|
362
|
-
end
|
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
|
406
|
-
|
407
|
-
##Generating A Bar Chart
|
408
|
-
|
409
|
-
#```ruby
|
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
|
417
|
-
end
|
418
|
-
end
|
419
|
-
|
420
|
-
##```
|
421
|
-
|
422
|
-
##Hide Gridlines in chart
|
423
|
-
|
424
|
-
#```ruby
|
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
|
434
|
-
end
|
435
|
-
end
|
436
|
-
#```
|
437
|
-
|
438
|
-
##Generating A Pie Chart
|
439
|
-
|
440
|
-
#```ruby
|
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
|
448
|
-
end
|
449
|
-
end
|
450
|
-
#```
|
451
|
-
|
452
|
-
##Generating A Line Chart
|
453
|
-
|
454
|
-
#```ruby
|
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
|
-
|
479
|
-
end
|
480
|
-
end
|
481
|
-
#```
|
482
|
-
|
483
|
-
##Generating A Scatter Chart
|
484
|
-
|
485
|
-
#```ruby
|
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
|
498
|
-
end
|
499
|
-
end
|
500
|
-
#```
|
501
|
-
|
502
|
-
|
503
|
-
##Tables
|
504
|
-
|
505
|
-
#```ruby
|
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
|
515
|
-
end
|
516
|
-
#```
|
517
|
-
|
518
|
-
|
519
|
-
##Fit to page printing
|
520
|
-
|
521
|
-
#```ruby
|
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
|
527
|
-
end
|
528
|
-
##```
|
529
|
-
|
530
|
-
|
531
|
-
##Hide Gridlines in worksheet
|
532
|
-
|
533
|
-
#```ruby
|
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
|
539
|
-
end
|
540
|
-
##```
|
541
|
-
|
542
|
-
# Repeat printing of header rows.
|
543
|
-
#```ruby
|
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
|
550
|
-
end
|
551
|
-
|
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
|
558
|
-
end
|
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
|
569
|
-
|
570
|
-
##Specify page margins and other options for printing
|
571
|
-
|
572
|
-
#```ruby
|
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
|
590
|
-
end
|
591
|
-
#```
|
592
|
-
|
593
|
-
## Add Comments to your spreadsheet
|
594
|
-
#``` ruby
|
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
|
602
|
-
end
|
603
|
-
|
604
|
-
## Frozen/Split panes
|
605
|
-
## ``` ruby
|
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
|
617
|
-
end
|
618
|
-
end
|
619
|
-
|
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
|
636
|
-
|
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
|
646
|
-
|
647
|
-
|
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 })
|
673
|
-
end
|
674
|
-
|
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 })
|
688
|
-
end
|
689
|
-
|
690
|
-
|
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 })
|
715
|
-
end
|
716
|
-
end
|
717
|
-
|
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!
|
725
|
-
end
|
726
|
-
end
|
727
|
-
|
728
|
-
#Validate and Serialize
|
729
|
-
|
730
|
-
#```ruby
|
731
|
-
# Serialize directly to file
|
732
|
-
|
733
|
-
p.serialize("example.xlsx")
|
734
|
-
|
735
|
-
# or
|
736
|
-
|
737
|
-
#Serialize to a stream
|
738
|
-
if examples.include? :streaming
|
739
|
-
s = p.to_stream()
|
740
|
-
File.open('example_streamed.xlsx', 'w') { |f| f.write(s.read) }
|
741
|
-
end
|
742
|
-
#```
|
743
|
-
|
744
|
-
##Using Shared Strings
|
745
|
-
|
746
|
-
#```ruby
|
747
|
-
# This is required by Numbers
|
748
|
-
if examples.include? :shared_strings
|
749
|
-
p.use_shared_strings = true
|
750
|
-
p.serialize("shared_strings_example.xlsx")
|
751
|
-
end
|
752
|
-
#```
|
753
|
-
|
754
|
-
#```ruby
|
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")
|
763
|
-
end
|
764
|
-
#```
|
765
|
-
|
766
|
-
|
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
|
777
|
-
|