writeexcel 0.5.0 → 0.6.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.
- data/.gitattributes +1 -1
- data/.gitignore +24 -24
- data/README.rdoc +34 -55
- data/VERSION +1 -1
- data/charts/chartex.rb +316 -316
- data/charts/demo1.rb +46 -46
- data/charts/demo2.rb +65 -65
- data/charts/demo3.rb +117 -117
- data/charts/demo4.rb +119 -119
- data/charts/demo5.rb +48 -48
- data/examples/a_simple.rb +43 -43
- data/examples/autofilter.rb +265 -265
- data/examples/bigfile.rb +30 -30
- data/examples/chart_area.rb +121 -121
- data/examples/chart_bar.rb +120 -120
- data/examples/chart_column.rb +120 -120
- data/examples/chart_line.rb +120 -120
- data/examples/chart_pie.rb +108 -108
- data/examples/chart_scatter.rb +121 -121
- data/examples/chart_stock.rb +148 -148
- data/examples/chess.rb +142 -142
- data/examples/colors.rb +129 -129
- data/examples/comments1.rb +27 -27
- data/examples/comments2.rb +352 -352
- data/examples/copyformat.rb +52 -52
- data/examples/data_validate.rb +279 -279
- data/examples/date_time.rb +87 -87
- data/examples/defined_name.rb +32 -32
- data/examples/demo.rb +124 -124
- data/examples/diag_border.rb +36 -36
- data/examples/formats.rb +490 -490
- data/examples/formula_result.rb +30 -30
- data/examples/header.rb +137 -137
- data/examples/hide_sheet.rb +29 -29
- data/examples/hyperlink.rb +43 -43
- data/examples/images.rb +63 -63
- data/examples/indent.rb +31 -31
- data/examples/merge1.rb +40 -40
- data/examples/merge2.rb +45 -45
- data/examples/merge3.rb +66 -66
- data/examples/merge4.rb +83 -83
- data/examples/merge5.rb +80 -80
- data/examples/merge6.rb +67 -67
- data/examples/outline.rb +255 -255
- data/examples/outline_collapsed.rb +209 -209
- data/examples/panes.rb +113 -113
- data/examples/properties.rb +34 -34
- data/examples/properties_jp.rb +33 -33
- data/examples/protection.rb +47 -47
- data/examples/regions.rb +53 -53
- data/examples/repeat.rb +43 -43
- data/examples/right_to_left.rb +27 -27
- data/examples/row_wrap.rb +53 -53
- data/examples/stats.rb +74 -74
- data/examples/stocks.rb +81 -81
- data/examples/tab_colors.rb +31 -31
- data/examples/utf8.rb +15 -15
- data/examples/write_arrays.rb +83 -83
- data/lib/writeexcel/biffwriter.rb +232 -232
- data/lib/writeexcel/caller_info.rb +12 -12
- data/lib/writeexcel/chart.rb +2190 -2177
- data/lib/writeexcel/charts/area.rb +154 -154
- data/lib/writeexcel/charts/bar.rb +177 -177
- data/lib/writeexcel/charts/column.rb +156 -156
- data/lib/writeexcel/charts/external.rb +66 -66
- data/lib/writeexcel/charts/line.rb +154 -154
- data/lib/writeexcel/charts/pie.rb +169 -169
- data/lib/writeexcel/charts/scatter.rb +192 -192
- data/lib/writeexcel/charts/stock.rb +213 -213
- data/lib/writeexcel/colors.rb +64 -64
- data/lib/writeexcel/compatibility.rb +0 -255
- data/lib/writeexcel/debug_info.rb +37 -33
- data/lib/writeexcel/excelformulaparser.rb +587 -587
- data/lib/writeexcel/format.rb +13 -4
- data/lib/writeexcel/formula.rb +26 -9
- data/lib/writeexcel/helper.rb +68 -64
- data/lib/writeexcel/olewriter.rb +311 -311
- data/lib/writeexcel/properties.rb +242 -240
- data/lib/writeexcel/storage_lite.rb +984 -978
- data/lib/writeexcel/workbook.rb +3210 -3192
- data/lib/writeexcel/worksheet.rb +143 -51
- data/lib/writeexcel/write_file.rb +44 -40
- data/lib/writeexcel.rb +1159 -1159
- data/test/helper.rb +31 -28
- data/test/perl_output/README +31 -31
- data/test/test_00_IEEE_double.rb +13 -13
- data/test/test_01_add_worksheet.rb +10 -10
- data/test/test_02_merge_formats.rb +53 -53
- data/test/test_04_dimensions.rb +392 -392
- data/test/test_05_rows.rb +179 -179
- data/test/test_06_extsst.rb +77 -77
- data/test/test_11_date_time.rb +479 -479
- data/test/test_12_date_only.rb +501 -501
- data/test/test_13_date_seconds.rb +481 -481
- data/test/test_21_escher.rb +637 -637
- data/test/test_22_mso_drawing_group.rb +745 -745
- data/test/test_23_note.rb +73 -73
- data/test/test_24_txo.rb +75 -75
- data/test/test_25_position_object.rb +84 -84
- data/test/test_26_autofilter.rb +314 -314
- data/test/test_27_autofilter.rb +131 -131
- data/test/test_28_autofilter.rb +161 -161
- data/test/test_29_process_jpg.rb +683 -683
- data/test/test_30_validation_dval.rb +77 -77
- data/test/test_31_validation_dv_strings.rb +126 -126
- data/test/test_32_validation_dv_formula.rb +206 -206
- data/test/test_40_property_types.rb +188 -188
- data/test/test_41_properties.rb +235 -235
- data/test/test_42_set_properties.rb +437 -437
- data/test/test_50_name_stored.rb +299 -299
- data/test/test_51_name_print_area.rb +357 -357
- data/test/test_52_name_print_titles.rb +454 -454
- data/test/test_53_autofilter.rb +203 -203
- data/test/test_60_chart_generic.rb +578 -578
- data/test/test_61_chart_subclasses.rb +95 -95
- data/test/test_62_chart_formats.rb +272 -272
- data/test/test_63_chart_area_formats.rb +649 -649
- data/test/test_biff.rb +75 -75
- data/test/test_compatibility.rb +12 -627
- data/test/test_example_match.rb +3144 -3144
- data/test/test_formula.rb +61 -61
- data/test/test_ole.rb +106 -106
- data/test/test_storage_lite.rb +125 -125
- data/test/test_workbook.rb +139 -139
- data/test/test_worksheet.rb +110 -110
- data/utils/add_magic_comment.rb +80 -80
- data/writeexcel.gemspec +4 -6
- data/writeexcel.rdoc +58 -15
- metadata +9 -6
- data/test/test_new_encoding.rb +0 -205
data/examples/merge6.rb
CHANGED
@@ -1,67 +1,67 @@
|
|
1
|
-
#!/usr/bin/ruby -w
|
2
|
-
# -*- coding: utf-8 -*-
|
3
|
-
|
4
|
-
###############################################################################
|
5
|
-
#
|
6
|
-
# Example of how to use the Spreadsheet::WriteExcel merge_cells() workbook
|
7
|
-
# method with Unicode strings.
|
8
|
-
#
|
9
|
-
#
|
10
|
-
# reverse('©'), December 2005, John McNamara, jmcnamara@cpan.org
|
11
|
-
#
|
12
|
-
# original written in Perl by John McNamara
|
13
|
-
# converted to Ruby by Hideo Nakamura, cxn03651@msj.biglobe.ne.jp
|
14
|
-
#
|
15
|
-
|
16
|
-
require 'writeexcel'
|
17
|
-
|
18
|
-
# Create a new workbook and add a worksheet
|
19
|
-
workbook = WriteExcel.new('merge6.xls')
|
20
|
-
worksheet = workbook.add_worksheet
|
21
|
-
|
22
|
-
# Increase the cell size of the merged cells to highlight the formatting.
|
23
|
-
(2..9).each { |i| worksheet.set_row(i, 36) }
|
24
|
-
worksheet.set_column('B:D', 25)
|
25
|
-
|
26
|
-
# Format for the merged cells.
|
27
|
-
format = workbook.add_format(
|
28
|
-
:border => 6,
|
29
|
-
:bold => 1,
|
30
|
-
:color => 'red',
|
31
|
-
:size => 20,
|
32
|
-
:valign => 'vcentre',
|
33
|
-
:align => 'left',
|
34
|
-
:indent => 1
|
35
|
-
)
|
36
|
-
|
37
|
-
###############################################################################
|
38
|
-
#
|
39
|
-
# Write an Ascii string.
|
40
|
-
#
|
41
|
-
|
42
|
-
worksheet.merge_range('B3:D4', 'ASCII: A simple string', format)
|
43
|
-
|
44
|
-
###############################################################################
|
45
|
-
#
|
46
|
-
# Write a UTF-16 Unicode string.
|
47
|
-
#
|
48
|
-
|
49
|
-
# A phrase in Cyrillic encoded as UTF-16BE.
|
50
|
-
utf16_str = [
|
51
|
-
'005500540046002d00310036003a0020' <<
|
52
|
-
'042d0442043e002004440440043004370430002004' <<
|
53
|
-
'3d043000200440044304410441043a043e043c0021'
|
54
|
-
].pack("H*")
|
55
|
-
|
56
|
-
# Note the extra parameter at the end to indicate UTF-16 encoding.
|
57
|
-
worksheet.merge_range('B6:D7', utf16_str, format, 1)
|
58
|
-
|
59
|
-
###############################################################################
|
60
|
-
#
|
61
|
-
# Write a UTF-8 Unicode string.
|
62
|
-
#
|
63
|
-
|
64
|
-
smiley = '☺' # chr 0x263a in perl
|
65
|
-
worksheet.merge_range('B9:D10', "UTF-8: A Unicode smiley #{smiley}", format)
|
66
|
-
|
67
|
-
workbook.close
|
1
|
+
#!/usr/bin/ruby -w
|
2
|
+
# -*- coding: utf-8 -*-
|
3
|
+
|
4
|
+
###############################################################################
|
5
|
+
#
|
6
|
+
# Example of how to use the Spreadsheet::WriteExcel merge_cells() workbook
|
7
|
+
# method with Unicode strings.
|
8
|
+
#
|
9
|
+
#
|
10
|
+
# reverse('©'), December 2005, John McNamara, jmcnamara@cpan.org
|
11
|
+
#
|
12
|
+
# original written in Perl by John McNamara
|
13
|
+
# converted to Ruby by Hideo Nakamura, cxn03651@msj.biglobe.ne.jp
|
14
|
+
#
|
15
|
+
|
16
|
+
require 'writeexcel'
|
17
|
+
|
18
|
+
# Create a new workbook and add a worksheet
|
19
|
+
workbook = WriteExcel.new('merge6.xls')
|
20
|
+
worksheet = workbook.add_worksheet
|
21
|
+
|
22
|
+
# Increase the cell size of the merged cells to highlight the formatting.
|
23
|
+
(2..9).each { |i| worksheet.set_row(i, 36) }
|
24
|
+
worksheet.set_column('B:D', 25)
|
25
|
+
|
26
|
+
# Format for the merged cells.
|
27
|
+
format = workbook.add_format(
|
28
|
+
:border => 6,
|
29
|
+
:bold => 1,
|
30
|
+
:color => 'red',
|
31
|
+
:size => 20,
|
32
|
+
:valign => 'vcentre',
|
33
|
+
:align => 'left',
|
34
|
+
:indent => 1
|
35
|
+
)
|
36
|
+
|
37
|
+
###############################################################################
|
38
|
+
#
|
39
|
+
# Write an Ascii string.
|
40
|
+
#
|
41
|
+
|
42
|
+
worksheet.merge_range('B3:D4', 'ASCII: A simple string', format)
|
43
|
+
|
44
|
+
###############################################################################
|
45
|
+
#
|
46
|
+
# Write a UTF-16 Unicode string.
|
47
|
+
#
|
48
|
+
|
49
|
+
# A phrase in Cyrillic encoded as UTF-16BE.
|
50
|
+
utf16_str = [
|
51
|
+
'005500540046002d00310036003a0020' <<
|
52
|
+
'042d0442043e002004440440043004370430002004' <<
|
53
|
+
'3d043000200440044304410441043a043e043c0021'
|
54
|
+
].pack("H*")
|
55
|
+
|
56
|
+
# Note the extra parameter at the end to indicate UTF-16 encoding.
|
57
|
+
worksheet.merge_range('B6:D7', utf16_str, format, 1)
|
58
|
+
|
59
|
+
###############################################################################
|
60
|
+
#
|
61
|
+
# Write a UTF-8 Unicode string.
|
62
|
+
#
|
63
|
+
|
64
|
+
smiley = '☺' # chr 0x263a in perl
|
65
|
+
worksheet.merge_range('B9:D10', "UTF-8: A Unicode smiley #{smiley}", format)
|
66
|
+
|
67
|
+
workbook.close
|
data/examples/outline.rb
CHANGED
@@ -1,255 +1,255 @@
|
|
1
|
-
#!/usr/bin/ruby -w
|
2
|
-
# -*- coding: utf-8 -*-
|
3
|
-
|
4
|
-
###############################################################################
|
5
|
-
#
|
6
|
-
# Example of how use Spreadsheet::WriteExcel to generate Excel outlines and
|
7
|
-
# grouping.
|
8
|
-
#
|
9
|
-
#
|
10
|
-
# Excel allows you to group rows or columns so that they can be hidden or
|
11
|
-
# displayed with a single mouse click. This feature is referred to as outlines.
|
12
|
-
#
|
13
|
-
# Outlines can reduce complex data down to a few salient sub-totals or
|
14
|
-
# summaries.
|
15
|
-
#
|
16
|
-
# This feature is best viewed in Excel but the following is an ASCII
|
17
|
-
# representation of what a worksheet with three outlines might look like.
|
18
|
-
# Rows 3-4 and rows 7-8 are grouped at level 2. Rows 2-9 are grouped at
|
19
|
-
# level 1. The lines at the left hand side are called outline level bars.
|
20
|
-
#
|
21
|
-
#
|
22
|
-
# ------------------------------------------
|
23
|
-
# 1 2 3 | | A | B | C | D | ...
|
24
|
-
# ------------------------------------------
|
25
|
-
# _ | 1 | A | | | | ...
|
26
|
-
# | _ | 2 | B | | | | ...
|
27
|
-
# | | | 3 | (C) | | | | ...
|
28
|
-
# | | | 4 | (D) | | | | ...
|
29
|
-
# | - | 5 | E | | | | ...
|
30
|
-
# | _ | 6 | F | | | | ...
|
31
|
-
# | | | 7 | (G) | | | | ...
|
32
|
-
# | | | 8 | (H) | | | | ...
|
33
|
-
# | - | 9 | I | | | | ...
|
34
|
-
# - | . | ... | ... | ... | ... | ...
|
35
|
-
#
|
36
|
-
#
|
37
|
-
# Clicking the minus sign on each of the level 2 outlines will collapse and
|
38
|
-
# hide the data as shown in the next figure. The minus sign changes to a plus
|
39
|
-
# sign to indicate that the data in the outline is hidden.
|
40
|
-
#
|
41
|
-
# ------------------------------------------
|
42
|
-
# 1 2 3 | | A | B | C | D | ...
|
43
|
-
# ------------------------------------------
|
44
|
-
# _ | 1 | A | | | | ...
|
45
|
-
# | | 2 | B | | | | ...
|
46
|
-
# | + | 5 | E | | | | ...
|
47
|
-
# | | 6 | F | | | | ...
|
48
|
-
# | + | 9 | I | | | | ...
|
49
|
-
# - | . | ... | ... | ... | ... | ...
|
50
|
-
#
|
51
|
-
#
|
52
|
-
# Clicking on the minus sign on the level 1 outline will collapse the remaining
|
53
|
-
# rows as follows:
|
54
|
-
#
|
55
|
-
# ------------------------------------------
|
56
|
-
# 1 2 3 | | A | B | C | D | ...
|
57
|
-
# ------------------------------------------
|
58
|
-
# | 1 | A | | | | ...
|
59
|
-
# + | . | ... | ... | ... | ... | ...
|
60
|
-
#
|
61
|
-
# See the main Spreadsheet::WriteExcel documentation for more information.
|
62
|
-
#
|
63
|
-
# reverse('©'), April 2003, John McNamara, jmcnamara@cpan.org
|
64
|
-
#
|
65
|
-
# original written in Perl by John McNamara
|
66
|
-
# converted to Ruby by Hideo Nakamura, cxn03651@msj.biglobe.ne.jp
|
67
|
-
#
|
68
|
-
|
69
|
-
require 'writeexcel'
|
70
|
-
|
71
|
-
# Create a new workbook and add some worksheets
|
72
|
-
workbook = WriteExcel.new('outline.xls')
|
73
|
-
worksheet1 = workbook.add_worksheet('Outlined Rows')
|
74
|
-
worksheet2 = workbook.add_worksheet('Collapsed Rows')
|
75
|
-
worksheet3 = workbook.add_worksheet('Outline Columns')
|
76
|
-
worksheet4 = workbook.add_worksheet('Outline levels')
|
77
|
-
|
78
|
-
# Add a general format
|
79
|
-
bold = workbook.add_format(:bold => 1)
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
###############################################################################
|
84
|
-
#
|
85
|
-
# Example 1: Create a worksheet with outlined rows. It also includes SUBTOTAL()
|
86
|
-
# functions so that it looks like the type of automatic outlines that are
|
87
|
-
# generated when you use the Excel Data->SubTotals menu item.
|
88
|
-
#
|
89
|
-
|
90
|
-
|
91
|
-
# For outlines the important parameters are $hidden and $level. Rows with the
|
92
|
-
# same $level are grouped together. The group will be collapsed if $hidden is
|
93
|
-
# non-zero. $height and $XF are assigned default values if they are undef.
|
94
|
-
#
|
95
|
-
# The syntax is: set_row($row, $height, $XF, $hidden, $level, $collapsed)
|
96
|
-
#
|
97
|
-
worksheet1.set_row(1, nil, nil, 0, 2)
|
98
|
-
worksheet1.set_row(2, nil, nil, 0, 2)
|
99
|
-
worksheet1.set_row(3, nil, nil, 0, 2)
|
100
|
-
worksheet1.set_row(4, nil, nil, 0, 2)
|
101
|
-
worksheet1.set_row(5, nil, nil, 0, 1)
|
102
|
-
|
103
|
-
worksheet1.set_row(6, nil, nil, 0, 2)
|
104
|
-
worksheet1.set_row(7, nil, nil, 0, 2)
|
105
|
-
worksheet1.set_row(8, nil, nil, 0, 2)
|
106
|
-
worksheet1.set_row(9, nil, nil, 0, 2)
|
107
|
-
worksheet1.set_row(10, nil, nil, 0, 1)
|
108
|
-
|
109
|
-
|
110
|
-
# Add a column format for clarity
|
111
|
-
worksheet1.set_column('A:A', 20)
|
112
|
-
|
113
|
-
# Add the data, labels and formulas
|
114
|
-
worksheet1.write('A1', 'Region', bold)
|
115
|
-
worksheet1.write('A2', 'North')
|
116
|
-
worksheet1.write('A3', 'North')
|
117
|
-
worksheet1.write('A4', 'North')
|
118
|
-
worksheet1.write('A5', 'North')
|
119
|
-
worksheet1.write('A6', 'North Total', bold)
|
120
|
-
|
121
|
-
worksheet1.write('B1', 'Sales', bold)
|
122
|
-
worksheet1.write('B2', 1000)
|
123
|
-
worksheet1.write('B3', 1200)
|
124
|
-
worksheet1.write('B4', 900)
|
125
|
-
worksheet1.write('B5', 1200)
|
126
|
-
worksheet1.write('B6', '=SUBTOTAL(9,B2:B5)', bold)
|
127
|
-
|
128
|
-
worksheet1.write('A7', 'South')
|
129
|
-
worksheet1.write('A8', 'South')
|
130
|
-
worksheet1.write('A9', 'South')
|
131
|
-
worksheet1.write('A10', 'South')
|
132
|
-
worksheet1.write('A11', 'South Total', bold)
|
133
|
-
|
134
|
-
worksheet1.write('B7', 400)
|
135
|
-
worksheet1.write('B8', 600)
|
136
|
-
worksheet1.write('B9', 500)
|
137
|
-
worksheet1.write('B10', 600)
|
138
|
-
worksheet1.write('B11', '=SUBTOTAL(9,B7:B10)', bold)
|
139
|
-
|
140
|
-
worksheet1.write('A12', 'Grand Total', bold)
|
141
|
-
worksheet1.write('B12', '=SUBTOTAL(9,B2:B10)', bold)
|
142
|
-
|
143
|
-
|
144
|
-
###############################################################################
|
145
|
-
#
|
146
|
-
# Example 2: Create a worksheet with outlined rows. This is the same as the
|
147
|
-
# previous example except that the rows are collapsed.
|
148
|
-
# Note: We need to indicate the row that contains the collapsed symbol '+'
|
149
|
-
# with the optional parameter, $collapsed.
|
150
|
-
|
151
|
-
# The group will be collapsed if $hidden is non-zero.
|
152
|
-
# The syntax is: set_row($row, $height, $XF, $hidden, $level, $collapsed)
|
153
|
-
#
|
154
|
-
worksheet2.set_row(1, nil, nil, 1, 2)
|
155
|
-
worksheet2.set_row(2, nil, nil, 1, 2)
|
156
|
-
worksheet2.set_row(3, nil, nil, 1, 2)
|
157
|
-
worksheet2.set_row(4, nil, nil, 1, 2)
|
158
|
-
worksheet2.set_row(5, nil, nil, 1, 1)
|
159
|
-
|
160
|
-
worksheet2.set_row(6, nil, nil, 1, 2)
|
161
|
-
worksheet2.set_row(7, nil, nil, 1, 2)
|
162
|
-
worksheet2.set_row(8, nil, nil, 1, 2)
|
163
|
-
worksheet2.set_row(9, nil, nil, 1, 2)
|
164
|
-
worksheet2.set_row(10, nil, nil, 1, 1)
|
165
|
-
worksheet2.set_row(11, nil, nil, 0, 0, 1)
|
166
|
-
|
167
|
-
|
168
|
-
# Add a column format for clarity
|
169
|
-
worksheet2.set_column('A:A', 20)
|
170
|
-
|
171
|
-
# Add the data, labels and formulas
|
172
|
-
worksheet2.write('A1', 'Region', bold)
|
173
|
-
worksheet2.write('A2', 'North')
|
174
|
-
worksheet2.write('A3', 'North')
|
175
|
-
worksheet2.write('A4', 'North')
|
176
|
-
worksheet2.write('A5', 'North')
|
177
|
-
worksheet2.write('A6', 'North Total', bold)
|
178
|
-
|
179
|
-
worksheet2.write('B1', 'Sales', bold)
|
180
|
-
worksheet2.write('B2', 1000)
|
181
|
-
worksheet2.write('B3', 1200)
|
182
|
-
worksheet2.write('B4', 900)
|
183
|
-
worksheet2.write('B5', 1200)
|
184
|
-
worksheet2.write('B6', '=SUBTOTAL(9,B2:B5)', bold)
|
185
|
-
|
186
|
-
worksheet2.write('A7', 'South')
|
187
|
-
worksheet2.write('A8', 'South')
|
188
|
-
worksheet2.write('A9', 'South')
|
189
|
-
worksheet2.write('A10', 'South')
|
190
|
-
worksheet2.write('A11', 'South Total', bold)
|
191
|
-
|
192
|
-
worksheet2.write('B7', 400)
|
193
|
-
worksheet2.write('B8', 600)
|
194
|
-
worksheet2.write('B9', 500)
|
195
|
-
worksheet2.write('B10', 600)
|
196
|
-
worksheet2.write('B11', '=SUBTOTAL(9,B7:B10)', bold)
|
197
|
-
|
198
|
-
worksheet2.write('A12', 'Grand Total', bold)
|
199
|
-
worksheet2.write('B12', '=SUBTOTAL(9,B2:B10)', bold)
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
###############################################################################
|
204
|
-
#
|
205
|
-
# Example 3: Create a worksheet with outlined columns.
|
206
|
-
#
|
207
|
-
data = [
|
208
|
-
['Month', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', ' Total'],
|
209
|
-
['North', 50, 20, 15, 25, 65, 80, '=SUM(B2:G2)'],
|
210
|
-
['South', 10, 20, 30, 50, 50, 50, '=SUM(B3:G3)'],
|
211
|
-
['East', 45, 75, 50, 15, 75, 100, '=SUM(B4:G4)'],
|
212
|
-
['West', 15, 15, 55, 35, 20, 50, '=SUM(B5:G6)']
|
213
|
-
]
|
214
|
-
|
215
|
-
# Add bold format to the first row
|
216
|
-
worksheet3.set_row(0, nil, bold)
|
217
|
-
|
218
|
-
# Syntax: set_column(col1, col2, width, XF, hidden, level, collapsed)
|
219
|
-
worksheet3.set_column('A:A', 10, bold )
|
220
|
-
worksheet3.set_column('B:G', 5, nil, 0, 1)
|
221
|
-
worksheet3.set_column('H:H', 10)
|
222
|
-
|
223
|
-
# Write the data and a formula
|
224
|
-
worksheet3.write_col('A1', data)
|
225
|
-
worksheet3.write('H6', '=SUM(H2:H5)', bold)
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
###############################################################################
|
230
|
-
#
|
231
|
-
# Example 4: Show all possible outline levels.
|
232
|
-
#
|
233
|
-
levels = [
|
234
|
-
"Level 1", "Level 2", "Level 3", "Level 4",
|
235
|
-
"Level 5", "Level 6", "Level 7", "Level 6",
|
236
|
-
"Level 5", "Level 4", "Level 3", "Level 2", "Level 1"
|
237
|
-
]
|
238
|
-
|
239
|
-
worksheet4.write_col('A1', levels)
|
240
|
-
|
241
|
-
worksheet4.set_row(0, nil, nil, nil, 1)
|
242
|
-
worksheet4.set_row(1, nil, nil, nil, 2)
|
243
|
-
worksheet4.set_row(2, nil, nil, nil, 3)
|
244
|
-
worksheet4.set_row(3, nil, nil, nil, 4)
|
245
|
-
worksheet4.set_row(4, nil, nil, nil, 5)
|
246
|
-
worksheet4.set_row(5, nil, nil, nil, 6)
|
247
|
-
worksheet4.set_row(6, nil, nil, nil, 7)
|
248
|
-
worksheet4.set_row(7, nil, nil, nil, 6)
|
249
|
-
worksheet4.set_row(8, nil, nil, nil, 5)
|
250
|
-
worksheet4.set_row(9, nil, nil, nil, 4)
|
251
|
-
worksheet4.set_row(10, nil, nil, nil, 3)
|
252
|
-
worksheet4.set_row(11, nil, nil, nil, 2)
|
253
|
-
worksheet4.set_row(12, nil, nil, nil, 1)
|
254
|
-
|
255
|
-
workbook.close
|
1
|
+
#!/usr/bin/ruby -w
|
2
|
+
# -*- coding: utf-8 -*-
|
3
|
+
|
4
|
+
###############################################################################
|
5
|
+
#
|
6
|
+
# Example of how use Spreadsheet::WriteExcel to generate Excel outlines and
|
7
|
+
# grouping.
|
8
|
+
#
|
9
|
+
#
|
10
|
+
# Excel allows you to group rows or columns so that they can be hidden or
|
11
|
+
# displayed with a single mouse click. This feature is referred to as outlines.
|
12
|
+
#
|
13
|
+
# Outlines can reduce complex data down to a few salient sub-totals or
|
14
|
+
# summaries.
|
15
|
+
#
|
16
|
+
# This feature is best viewed in Excel but the following is an ASCII
|
17
|
+
# representation of what a worksheet with three outlines might look like.
|
18
|
+
# Rows 3-4 and rows 7-8 are grouped at level 2. Rows 2-9 are grouped at
|
19
|
+
# level 1. The lines at the left hand side are called outline level bars.
|
20
|
+
#
|
21
|
+
#
|
22
|
+
# ------------------------------------------
|
23
|
+
# 1 2 3 | | A | B | C | D | ...
|
24
|
+
# ------------------------------------------
|
25
|
+
# _ | 1 | A | | | | ...
|
26
|
+
# | _ | 2 | B | | | | ...
|
27
|
+
# | | | 3 | (C) | | | | ...
|
28
|
+
# | | | 4 | (D) | | | | ...
|
29
|
+
# | - | 5 | E | | | | ...
|
30
|
+
# | _ | 6 | F | | | | ...
|
31
|
+
# | | | 7 | (G) | | | | ...
|
32
|
+
# | | | 8 | (H) | | | | ...
|
33
|
+
# | - | 9 | I | | | | ...
|
34
|
+
# - | . | ... | ... | ... | ... | ...
|
35
|
+
#
|
36
|
+
#
|
37
|
+
# Clicking the minus sign on each of the level 2 outlines will collapse and
|
38
|
+
# hide the data as shown in the next figure. The minus sign changes to a plus
|
39
|
+
# sign to indicate that the data in the outline is hidden.
|
40
|
+
#
|
41
|
+
# ------------------------------------------
|
42
|
+
# 1 2 3 | | A | B | C | D | ...
|
43
|
+
# ------------------------------------------
|
44
|
+
# _ | 1 | A | | | | ...
|
45
|
+
# | | 2 | B | | | | ...
|
46
|
+
# | + | 5 | E | | | | ...
|
47
|
+
# | | 6 | F | | | | ...
|
48
|
+
# | + | 9 | I | | | | ...
|
49
|
+
# - | . | ... | ... | ... | ... | ...
|
50
|
+
#
|
51
|
+
#
|
52
|
+
# Clicking on the minus sign on the level 1 outline will collapse the remaining
|
53
|
+
# rows as follows:
|
54
|
+
#
|
55
|
+
# ------------------------------------------
|
56
|
+
# 1 2 3 | | A | B | C | D | ...
|
57
|
+
# ------------------------------------------
|
58
|
+
# | 1 | A | | | | ...
|
59
|
+
# + | . | ... | ... | ... | ... | ...
|
60
|
+
#
|
61
|
+
# See the main Spreadsheet::WriteExcel documentation for more information.
|
62
|
+
#
|
63
|
+
# reverse('©'), April 2003, John McNamara, jmcnamara@cpan.org
|
64
|
+
#
|
65
|
+
# original written in Perl by John McNamara
|
66
|
+
# converted to Ruby by Hideo Nakamura, cxn03651@msj.biglobe.ne.jp
|
67
|
+
#
|
68
|
+
|
69
|
+
require 'writeexcel'
|
70
|
+
|
71
|
+
# Create a new workbook and add some worksheets
|
72
|
+
workbook = WriteExcel.new('outline.xls')
|
73
|
+
worksheet1 = workbook.add_worksheet('Outlined Rows')
|
74
|
+
worksheet2 = workbook.add_worksheet('Collapsed Rows')
|
75
|
+
worksheet3 = workbook.add_worksheet('Outline Columns')
|
76
|
+
worksheet4 = workbook.add_worksheet('Outline levels')
|
77
|
+
|
78
|
+
# Add a general format
|
79
|
+
bold = workbook.add_format(:bold => 1)
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
###############################################################################
|
84
|
+
#
|
85
|
+
# Example 1: Create a worksheet with outlined rows. It also includes SUBTOTAL()
|
86
|
+
# functions so that it looks like the type of automatic outlines that are
|
87
|
+
# generated when you use the Excel Data->SubTotals menu item.
|
88
|
+
#
|
89
|
+
|
90
|
+
|
91
|
+
# For outlines the important parameters are $hidden and $level. Rows with the
|
92
|
+
# same $level are grouped together. The group will be collapsed if $hidden is
|
93
|
+
# non-zero. $height and $XF are assigned default values if they are undef.
|
94
|
+
#
|
95
|
+
# The syntax is: set_row($row, $height, $XF, $hidden, $level, $collapsed)
|
96
|
+
#
|
97
|
+
worksheet1.set_row(1, nil, nil, 0, 2)
|
98
|
+
worksheet1.set_row(2, nil, nil, 0, 2)
|
99
|
+
worksheet1.set_row(3, nil, nil, 0, 2)
|
100
|
+
worksheet1.set_row(4, nil, nil, 0, 2)
|
101
|
+
worksheet1.set_row(5, nil, nil, 0, 1)
|
102
|
+
|
103
|
+
worksheet1.set_row(6, nil, nil, 0, 2)
|
104
|
+
worksheet1.set_row(7, nil, nil, 0, 2)
|
105
|
+
worksheet1.set_row(8, nil, nil, 0, 2)
|
106
|
+
worksheet1.set_row(9, nil, nil, 0, 2)
|
107
|
+
worksheet1.set_row(10, nil, nil, 0, 1)
|
108
|
+
|
109
|
+
|
110
|
+
# Add a column format for clarity
|
111
|
+
worksheet1.set_column('A:A', 20)
|
112
|
+
|
113
|
+
# Add the data, labels and formulas
|
114
|
+
worksheet1.write('A1', 'Region', bold)
|
115
|
+
worksheet1.write('A2', 'North')
|
116
|
+
worksheet1.write('A3', 'North')
|
117
|
+
worksheet1.write('A4', 'North')
|
118
|
+
worksheet1.write('A5', 'North')
|
119
|
+
worksheet1.write('A6', 'North Total', bold)
|
120
|
+
|
121
|
+
worksheet1.write('B1', 'Sales', bold)
|
122
|
+
worksheet1.write('B2', 1000)
|
123
|
+
worksheet1.write('B3', 1200)
|
124
|
+
worksheet1.write('B4', 900)
|
125
|
+
worksheet1.write('B5', 1200)
|
126
|
+
worksheet1.write('B6', '=SUBTOTAL(9,B2:B5)', bold)
|
127
|
+
|
128
|
+
worksheet1.write('A7', 'South')
|
129
|
+
worksheet1.write('A8', 'South')
|
130
|
+
worksheet1.write('A9', 'South')
|
131
|
+
worksheet1.write('A10', 'South')
|
132
|
+
worksheet1.write('A11', 'South Total', bold)
|
133
|
+
|
134
|
+
worksheet1.write('B7', 400)
|
135
|
+
worksheet1.write('B8', 600)
|
136
|
+
worksheet1.write('B9', 500)
|
137
|
+
worksheet1.write('B10', 600)
|
138
|
+
worksheet1.write('B11', '=SUBTOTAL(9,B7:B10)', bold)
|
139
|
+
|
140
|
+
worksheet1.write('A12', 'Grand Total', bold)
|
141
|
+
worksheet1.write('B12', '=SUBTOTAL(9,B2:B10)', bold)
|
142
|
+
|
143
|
+
|
144
|
+
###############################################################################
|
145
|
+
#
|
146
|
+
# Example 2: Create a worksheet with outlined rows. This is the same as the
|
147
|
+
# previous example except that the rows are collapsed.
|
148
|
+
# Note: We need to indicate the row that contains the collapsed symbol '+'
|
149
|
+
# with the optional parameter, $collapsed.
|
150
|
+
|
151
|
+
# The group will be collapsed if $hidden is non-zero.
|
152
|
+
# The syntax is: set_row($row, $height, $XF, $hidden, $level, $collapsed)
|
153
|
+
#
|
154
|
+
worksheet2.set_row(1, nil, nil, 1, 2)
|
155
|
+
worksheet2.set_row(2, nil, nil, 1, 2)
|
156
|
+
worksheet2.set_row(3, nil, nil, 1, 2)
|
157
|
+
worksheet2.set_row(4, nil, nil, 1, 2)
|
158
|
+
worksheet2.set_row(5, nil, nil, 1, 1)
|
159
|
+
|
160
|
+
worksheet2.set_row(6, nil, nil, 1, 2)
|
161
|
+
worksheet2.set_row(7, nil, nil, 1, 2)
|
162
|
+
worksheet2.set_row(8, nil, nil, 1, 2)
|
163
|
+
worksheet2.set_row(9, nil, nil, 1, 2)
|
164
|
+
worksheet2.set_row(10, nil, nil, 1, 1)
|
165
|
+
worksheet2.set_row(11, nil, nil, 0, 0, 1)
|
166
|
+
|
167
|
+
|
168
|
+
# Add a column format for clarity
|
169
|
+
worksheet2.set_column('A:A', 20)
|
170
|
+
|
171
|
+
# Add the data, labels and formulas
|
172
|
+
worksheet2.write('A1', 'Region', bold)
|
173
|
+
worksheet2.write('A2', 'North')
|
174
|
+
worksheet2.write('A3', 'North')
|
175
|
+
worksheet2.write('A4', 'North')
|
176
|
+
worksheet2.write('A5', 'North')
|
177
|
+
worksheet2.write('A6', 'North Total', bold)
|
178
|
+
|
179
|
+
worksheet2.write('B1', 'Sales', bold)
|
180
|
+
worksheet2.write('B2', 1000)
|
181
|
+
worksheet2.write('B3', 1200)
|
182
|
+
worksheet2.write('B4', 900)
|
183
|
+
worksheet2.write('B5', 1200)
|
184
|
+
worksheet2.write('B6', '=SUBTOTAL(9,B2:B5)', bold)
|
185
|
+
|
186
|
+
worksheet2.write('A7', 'South')
|
187
|
+
worksheet2.write('A8', 'South')
|
188
|
+
worksheet2.write('A9', 'South')
|
189
|
+
worksheet2.write('A10', 'South')
|
190
|
+
worksheet2.write('A11', 'South Total', bold)
|
191
|
+
|
192
|
+
worksheet2.write('B7', 400)
|
193
|
+
worksheet2.write('B8', 600)
|
194
|
+
worksheet2.write('B9', 500)
|
195
|
+
worksheet2.write('B10', 600)
|
196
|
+
worksheet2.write('B11', '=SUBTOTAL(9,B7:B10)', bold)
|
197
|
+
|
198
|
+
worksheet2.write('A12', 'Grand Total', bold)
|
199
|
+
worksheet2.write('B12', '=SUBTOTAL(9,B2:B10)', bold)
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
###############################################################################
|
204
|
+
#
|
205
|
+
# Example 3: Create a worksheet with outlined columns.
|
206
|
+
#
|
207
|
+
data = [
|
208
|
+
['Month', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', ' Total'],
|
209
|
+
['North', 50, 20, 15, 25, 65, 80, '=SUM(B2:G2)'],
|
210
|
+
['South', 10, 20, 30, 50, 50, 50, '=SUM(B3:G3)'],
|
211
|
+
['East', 45, 75, 50, 15, 75, 100, '=SUM(B4:G4)'],
|
212
|
+
['West', 15, 15, 55, 35, 20, 50, '=SUM(B5:G6)']
|
213
|
+
]
|
214
|
+
|
215
|
+
# Add bold format to the first row
|
216
|
+
worksheet3.set_row(0, nil, bold)
|
217
|
+
|
218
|
+
# Syntax: set_column(col1, col2, width, XF, hidden, level, collapsed)
|
219
|
+
worksheet3.set_column('A:A', 10, bold )
|
220
|
+
worksheet3.set_column('B:G', 5, nil, 0, 1)
|
221
|
+
worksheet3.set_column('H:H', 10)
|
222
|
+
|
223
|
+
# Write the data and a formula
|
224
|
+
worksheet3.write_col('A1', data)
|
225
|
+
worksheet3.write('H6', '=SUM(H2:H5)', bold)
|
226
|
+
|
227
|
+
|
228
|
+
|
229
|
+
###############################################################################
|
230
|
+
#
|
231
|
+
# Example 4: Show all possible outline levels.
|
232
|
+
#
|
233
|
+
levels = [
|
234
|
+
"Level 1", "Level 2", "Level 3", "Level 4",
|
235
|
+
"Level 5", "Level 6", "Level 7", "Level 6",
|
236
|
+
"Level 5", "Level 4", "Level 3", "Level 2", "Level 1"
|
237
|
+
]
|
238
|
+
|
239
|
+
worksheet4.write_col('A1', levels)
|
240
|
+
|
241
|
+
worksheet4.set_row(0, nil, nil, nil, 1)
|
242
|
+
worksheet4.set_row(1, nil, nil, nil, 2)
|
243
|
+
worksheet4.set_row(2, nil, nil, nil, 3)
|
244
|
+
worksheet4.set_row(3, nil, nil, nil, 4)
|
245
|
+
worksheet4.set_row(4, nil, nil, nil, 5)
|
246
|
+
worksheet4.set_row(5, nil, nil, nil, 6)
|
247
|
+
worksheet4.set_row(6, nil, nil, nil, 7)
|
248
|
+
worksheet4.set_row(7, nil, nil, nil, 6)
|
249
|
+
worksheet4.set_row(8, nil, nil, nil, 5)
|
250
|
+
worksheet4.set_row(9, nil, nil, nil, 4)
|
251
|
+
worksheet4.set_row(10, nil, nil, nil, 3)
|
252
|
+
worksheet4.set_row(11, nil, nil, nil, 2)
|
253
|
+
worksheet4.set_row(12, nil, nil, nil, 1)
|
254
|
+
|
255
|
+
workbook.close
|