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