writeexcel 0.3.4 → 0.3.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,112 @@
1
+ #!/usr/bin/ruby -w
2
+
3
+ #######################################################################
4
+ #
5
+ # Example of using the WriteExcel module to create worksheet panes.
6
+ #
7
+ # reverse('©'), May 2001, John McNamara, jmcnamara@cpan.org
8
+ #
9
+ # original written in Perl by John McNamara
10
+ # converted to Ruby by Hideo Nakamura, cxn03651@msj.biglobe.ne.jp
11
+ #
12
+
13
+ require 'writeexcel'
14
+
15
+ workbook = WriteExcel.new("panes.xls")
16
+
17
+ worksheet1 = workbook.add_worksheet('Panes 1')
18
+ worksheet2 = workbook.add_worksheet('Panes 2')
19
+ worksheet3 = workbook.add_worksheet('Panes 3')
20
+ worksheet4 = workbook.add_worksheet('Panes 4')
21
+
22
+ # Freeze panes
23
+ worksheet1.freeze_panes(1, 0) # 1 row
24
+
25
+ worksheet2.freeze_panes(0, 1) # 1 column
26
+ worksheet3.freeze_panes(1, 1) # 1 row and column
27
+
28
+ # Split panes.
29
+ # The divisions must be specified in terms of row and column dimensions.
30
+ # The default row height is 12.75 and the default column width is 8.43
31
+ #
32
+ worksheet4.split_panes(12.75, 8.43, 1, 1) # 1 row and column
33
+
34
+
35
+ #######################################################################
36
+ #
37
+ # Set up some formatting and text to highlight the panes
38
+ #
39
+
40
+ header = workbook.add_format
41
+ header.set_color('white')
42
+ header.set_align('center')
43
+ header.set_align('vcenter')
44
+ header.set_pattern
45
+ header.set_fg_color('green')
46
+
47
+ center = workbook.add_format
48
+ center.set_align('center')
49
+
50
+ #######################################################################
51
+ #
52
+ # Sheet 1
53
+ #
54
+
55
+ worksheet1.set_column('A:I', 16)
56
+ worksheet1.set_row(0, 20)
57
+ worksheet1.set_selection('C3')
58
+
59
+ (0..8).each { |i| worksheet1.write(0, i, 'Scroll down', header) }
60
+ (1..100).each do |i|
61
+ (0..8).each { |j| worksheet1.write(i, j, i + 1, center) }
62
+ end
63
+
64
+ #######################################################################
65
+ #
66
+ # Sheet 2
67
+ #
68
+
69
+ worksheet2.set_column('A:A', 16)
70
+ worksheet2.set_selection('C3')
71
+
72
+ (0..49).each do |i|
73
+ worksheet2.set_row(i, 15)
74
+ worksheet2.write(i, 0, 'Scroll right', header)
75
+ end
76
+
77
+ (0..49).each do |i|
78
+ (1..25).each { |j| worksheet2.write(i, j, j, center) }
79
+ end
80
+
81
+ #######################################################################
82
+ #
83
+ # Sheet 3
84
+ #
85
+
86
+ worksheet3.set_column('A:Z', 16)
87
+ worksheet3.set_selection('C3')
88
+
89
+ (1..25).each { |i| worksheet3.write(0, i, 'Scroll down', header) }
90
+
91
+ (1..49).each { |i| worksheet3.write(i, 0, 'Scroll right', header) }
92
+
93
+ (1..49).each do |i|
94
+ (1..25).each { |j| worksheet3.write(i, j, j, center) }
95
+ end
96
+
97
+ #######################################################################
98
+ #
99
+ # Sheet 4
100
+ #
101
+
102
+ worksheet4.set_selection('C3')
103
+
104
+ (1..25).each { |i| worksheet4.write(0, i, 'Scroll', center) }
105
+
106
+ (1..49).each { |i| worksheet4.write(i, 0, 'Scroll', center) }
107
+
108
+ (1..49).each do |i|
109
+ (1..25).each { |j| worksheet4.write(i, j, j, center) }
110
+ end
111
+
112
+ workbook.close
@@ -0,0 +1,26 @@
1
+ #!/usr/bin/ruby -w
2
+
3
+ #######################################################################
4
+ #
5
+ # Example of how to change the default worksheet direction from
6
+ # left-to-right to right-to-left as required by some eastern verions
7
+ # of Excel.
8
+ #
9
+ # reverse('©'), January 2006, John McNamara, jmcnamara@cpan.org
10
+ #
11
+ # original written in Perl by John McNamara
12
+ # converted to Ruby by Hideo Nakamura, cxn03651@msj.biglobe.ne.jp
13
+ #
14
+
15
+ require 'writeexcel'
16
+
17
+ workbook = WriteExcel.new("right_to_left.xls")
18
+ worksheet1 = workbook.add_worksheet
19
+ worksheet2 = workbook.add_worksheet
20
+
21
+ worksheet2.right_to_left
22
+
23
+ worksheet1.write(0, 0, 'Hello') # A1, B1, C1, ...
24
+ worksheet2.write(0, 0, 'Hello') # ..., C1, B1, A1
25
+
26
+ workbook.close
@@ -0,0 +1,52 @@
1
+ #!/usr/bin/ruby -w
2
+
3
+ ##############################################################################
4
+ #
5
+ # Demonstrates how to wrap data from one worksheet onto another.
6
+ #
7
+ # Excel has a row limit of 65536 rows. Sometimes the amount of row data to be
8
+ # written to a file is greater than this limit. In this case it is a useful
9
+ # technique to wrap the data from one worksheet onto the next so that we get
10
+ # something like the following:
11
+ #
12
+ # Sheet1 Row 1 - 65536
13
+ # Sheet2 Row 65537 - 131072
14
+ # Sheet3 Row 131073 - ...
15
+ #
16
+ # In order to achieve this we use a single worksheet reference and
17
+ # reinitialise it to point to a new worksheet when required.
18
+ #
19
+ # reverse('©'), May 2006, John McNamara, jmcnamara@cpan.org
20
+ #
21
+ # original written in Perl by John McNamara
22
+ # converted to Ruby by Hideo Nakamura, cxn03651@msj.biglobe.ne.jp
23
+ #
24
+
25
+ require 'writeexcel'
26
+
27
+ workbook = WriteExcel.new('row_wrap.xls')
28
+ worksheet = workbook.add_worksheet
29
+
30
+ # Worksheet formatting.
31
+ worksheet.set_column('A:A', 20)
32
+
33
+ # For the sake of this example we will use a small row limit. In order to use
34
+ # the entire row range set the row_limit to 65536.
35
+ row_limit = 10
36
+ row = 0
37
+
38
+ (1 .. 2 * row_limit + 10).each do |count|
39
+ # When we hit the row limit we redirect the output
40
+ # to a new worksheet and reset the row number.
41
+ if row == row_limit
42
+ worksheet = workbook.add_worksheet
43
+ row = 0
44
+
45
+ # Repeat any worksheet formatting.
46
+ worksheet.set_column('A:A', 20)
47
+ end
48
+ worksheet.write(row, 0, "This is row #{count}")
49
+ row += 1
50
+ end
51
+
52
+ workbook.close
@@ -93,58 +93,58 @@ class Formula < ExcelFormulaParser #:nodoc:
93
93
  exit "Unknown function #{token}() in formula\n" if _class.nil?
94
94
  _classary.push(_class)
95
95
  elsif (token == '_vol')
96
- parse_str = parse_str + convert_volatile()
96
+ parse_str += convert_volatile()
97
97
  elsif (token == 'ptgBool')
98
98
  token = args.shift
99
- parse_str = parse_str + convert_bool(token)
99
+ parse_str += convert_bool(token)
100
100
  elsif (token == '_num')
101
101
  token = args.shift
102
- parse_str = parse_str + convert_number(token)
102
+ parse_str += convert_number(token)
103
103
  elsif (token == '_str')
104
104
  token = args.shift
105
- parse_str = parse_str + convert_string(token)
105
+ parse_str += convert_string(token)
106
106
  elsif (token =~ /^_ref2d/)
107
107
  modifier = token.sub(/_ref2d/, '')
108
108
  _class = _classary[-1]
109
109
  _class = 0 if modifier == 'R'
110
110
  _class = 1 if modifier == 'V'
111
111
  token = args.shift
112
- parse_str = parse_str + convert_ref2d(token, _class)
112
+ parse_str += convert_ref2d(token, _class)
113
113
  elsif (token =~ /^_ref3d/)
114
114
  modifier = token.sub(/_ref3d/,'')
115
115
  _class = _classary[-1]
116
116
  _class = 0 if modifier == 'R'
117
117
  _class = 1 if modifier == 'V'
118
118
  token = args.shift
119
- parse_str = parse_str + convert_ref3d(token, _class)
119
+ parse_str += convert_ref3d(token, _class)
120
120
  elsif (token =~ /^_range2d/)
121
121
  modifier = token.sub(/_range2d/,'')
122
122
  _class = _classary[-1]
123
123
  _class = 0 if modifier == 'R'
124
124
  _class = 1 if modifier == 'V'
125
125
  token = args.shift
126
- parse_str = parse_str + convert_range2d(token, _class)
126
+ parse_str += convert_range2d(token, _class)
127
127
  elsif (token =~ /^_range3d/)
128
128
  modifier = token.sub(/_range3d/,'')
129
129
  _class = _classary[-1]
130
130
  _class = 0 if modifier == 'R'
131
131
  _class = 1 if modifier == 'V'
132
132
  token = args.shift
133
- parse_str = parse_str + convert_range3d(token, _class)
133
+ parse_str += convert_range3d(token, _class)
134
134
  elsif (token =~ /^_name/)
135
135
  modifier = token.sub(/_name/, '')
136
136
  _class = _classary[-1]
137
137
  _class = 0 if modifier == 'R'
138
138
  _class = 1 if modifier == 'V'
139
139
  token = args.shift
140
- parse_str = parse_str + convert_name(token, _class)
140
+ parse_str += convert_name(token, _class)
141
141
  elsif (token == '_func')
142
142
  token = args.shift
143
- parse_str = parse_str + convert_function(token, num_args.to_i)
143
+ parse_str += convert_function(token, num_args.to_i)
144
144
  _classary.pop
145
145
  num_args = 0 # Reset after use
146
146
  elsif @ptg[token]
147
- parse_str = parse_str + [@ptg[token]].pack("C")
147
+ parse_str += [@ptg[token]].pack("C")
148
148
  else
149
149
  # Unrecognised token
150
150
  return nil
@@ -189,12 +189,12 @@ class Formula < ExcelFormulaParser #:nodoc:
189
189
  s.unscan
190
190
  s.scan(/[A-Z0-9_.]+/)
191
191
  q.push [:FUNC, s.matched]
192
- elsif s.scan(/[A-Za-z_]\w+/)
193
- q.push [:NAME , s.matched]
194
192
  elsif s.scan(/TRUE/)
195
193
  q.push [:TRUE, s.matched]
196
194
  elsif s.scan(/FALSE/)
197
195
  q.push [:FALSE, s.matched]
196
+ elsif s.scan(/[A-Za-z_]\w+/)
197
+ q.push [:NAME , s.matched]
198
198
  elsif s.scan(/\s+/)
199
199
  ;
200
200
  elsif s.scan(/./)
@@ -554,7 +554,7 @@ class Formula < ExcelFormulaParser #:nodoc:
554
554
  if @ext_names.has_key?(name)
555
555
  @ext_names[name]
556
556
  else
557
- raise "Unknown defined name $name in formula\n"
557
+ raise "Unknown defined name #{name} in formula\n"
558
558
  end
559
559
  end
560
560
  private :get_name_index
@@ -211,8 +211,9 @@ class Worksheet < BIFFWriter
211
211
 
212
212
  # Prepend the COLINFO records if they exist
213
213
  unless @colinfo.empty?
214
- while (!@colinfo.empty?)
215
- arrayref = @colinfo.pop
214
+ colinfo = @colinfo.dup
215
+ while (!colinfo.empty?)
216
+ arrayref = colinfo.pop
216
217
  store_colinfo(*arrayref)
217
218
  end
218
219
  end
@@ -604,7 +605,7 @@ class Worksheet < BIFFWriter
604
605
  #
605
606
  grbit |= level
606
607
  grbit |= 0x0010 if collapsed != 0
607
- grbit |= 0x0020 if hidden != 0
608
+ grbit |= 0x0020 if !hidden.nil? && hidden != 0
608
609
  grbit |= 0x0040
609
610
  grbit |= 0x0080 unless format.nil?
610
611
  grbit |= 0x0100
@@ -3850,7 +3851,7 @@ class Worksheet < BIFFWriter
3850
3851
  # flag and check the result type.
3851
3852
  grbit = 0x00
3852
3853
 
3853
- if value =~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/
3854
+ if value.to_s =~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/
3854
3855
  # Value is a number.
3855
3856
  num = [value].pack("d")
3856
3857
  else
@@ -3906,7 +3907,7 @@ class Worksheet < BIFFWriter
3906
3907
  header = [record, length].pack("vv")
3907
3908
  data = [strlen, encoding].pack("vC")
3908
3909
 
3909
- return header . data . string
3910
+ return header + data + string
3910
3911
  end
3911
3912
  private :get_formula_string
3912
3913
 
@@ -5775,8 +5776,8 @@ class Worksheet < BIFFWriter
5775
5776
  col_level = 7 if col_level > 7
5776
5777
 
5777
5778
  # The displayed level is one greater than the max outline levels
5778
- row_level = row_level + 1 if row_level > 0
5779
- col_level = col_level + 1 if col_level > 0
5779
+ row_level += 1 if row_level > 0
5780
+ col_level += 1 if col_level > 0
5780
5781
 
5781
5782
  header = [record, length].pack("vv")
5782
5783
  data = [dxRwGut, dxColGut, row_level, col_level].pack("vvvv")
@@ -990,6 +990,62 @@ class TC_example_match < Test::Unit::TestCase
990
990
  compare_file("#{PERL_OUTDIR}/merge5.xls", @file)
991
991
  end
992
992
 
993
+ def test_merge6
994
+ # Create a new workbook and add a worksheet
995
+ workbook = WriteExcel.new(@file)
996
+ worksheet = workbook.add_worksheet
997
+
998
+ # Increase the cell size of the merged cells to highlight the formatting.
999
+ (2..9).each { |i| worksheet.set_row(i, 36) }
1000
+ worksheet.set_column('B:D', 25)
1001
+
1002
+ # Format for the merged cells.
1003
+ format = workbook.add_format(
1004
+ :border => 6,
1005
+ :bold => 1,
1006
+ :color => 'red',
1007
+ :size => 20,
1008
+ :valign => 'vcentre',
1009
+ :align => 'left',
1010
+ :indent => 1
1011
+ )
1012
+
1013
+ ###############################################################################
1014
+ #
1015
+ # Write an Ascii string.
1016
+ #
1017
+
1018
+ worksheet.merge_range('B3:D4', 'ASCII: A simple string', format)
1019
+
1020
+ ###############################################################################
1021
+ #
1022
+ # Write a UTF-16 Unicode string.
1023
+ #
1024
+
1025
+ # A phrase in Cyrillic encoded as UTF-16BE.
1026
+ utf16_str = [
1027
+ '005500540046002d00310036003a0020' <<
1028
+ '042d0442043e002004440440043004370430002004' <<
1029
+ '3d043000200440044304410441043a043e043c0021'
1030
+ ].pack("H*")
1031
+
1032
+ # Note the extra parameter at the end to indicate UTF-16 encoding.
1033
+ worksheet.merge_range('B6:D7', utf16_str, format, 1)
1034
+
1035
+ ###############################################################################
1036
+ #
1037
+ # Write a UTF-8 Unicode string.
1038
+ #
1039
+
1040
+ smiley = '☺' # chr 0x263a in perl
1041
+ worksheet.merge_range('B9:D10', "UTF-8: A Unicode smiley #{smiley}", format)
1042
+
1043
+ workbook.close
1044
+
1045
+ # do assertion
1046
+ compare_file("#{PERL_OUTDIR}/merge6.xls", @file)
1047
+ end
1048
+
993
1049
  def test_images
994
1050
  # Create a new workbook called simple.xls and add a worksheet
995
1051
  workbook = WriteExcel.new(@file)
@@ -2536,6 +2592,548 @@ workbook.close
2536
2592
  compare_file("#{PERL_OUTDIR}/comments2.xls", @file)
2537
2593
  end
2538
2594
 
2595
+ def test_formula_result
2596
+ workbook = WriteExcel.new(@file)
2597
+ worksheet = workbook.add_worksheet()
2598
+ format = workbook.add_format(:color => 'blue')
2599
+
2600
+ worksheet.write('A1', '=1+2')
2601
+ worksheet.write('A2', '=1+2', format, 4)
2602
+ worksheet.write('A3', '="ABC"', nil, 'DEF')
2603
+ worksheet.write('A4', '=IF(A1 > 1, TRUE, FALSE)', nil, 'TRUE')
2604
+ worksheet.write('A5', '=1/0', nil, '#DIV/0!')
2605
+
2606
+ workbook.close
2607
+
2608
+ # do assertion
2609
+ compare_file("#{PERL_OUTDIR}/formula_result.xls", @file)
2610
+ end
2611
+
2612
+ def test_indent
2613
+ workbook = WriteExcel.new(@file)
2614
+
2615
+ worksheet = workbook.add_worksheet()
2616
+ indent1 = workbook.add_format(:indent => 1)
2617
+ indent2 = workbook.add_format(:indent => 2)
2618
+
2619
+ worksheet.set_column('A:A', 40)
2620
+
2621
+ worksheet.write('A1', "This text is indented 1 level", indent1)
2622
+ worksheet.write('A2', "This text is indented 2 levels", indent2)
2623
+
2624
+ workbook.close
2625
+
2626
+ # do assertion
2627
+ compare_file("#{PERL_OUTDIR}/indent.xls", @file)
2628
+ end
2629
+
2630
+ def test_outline
2631
+ # Create a new workbook and add some worksheets
2632
+ workbook = WriteExcel.new(@file)
2633
+ worksheet1 = workbook.add_worksheet('Outlined Rows')
2634
+ worksheet2 = workbook.add_worksheet('Collapsed Rows')
2635
+ worksheet3 = workbook.add_worksheet('Outline Columns')
2636
+ worksheet4 = workbook.add_worksheet('Outline levels')
2637
+
2638
+ # Add a general format
2639
+ bold = workbook.add_format(:bold => 1)
2640
+
2641
+
2642
+
2643
+ ###############################################################################
2644
+ #
2645
+ # Example 1: Create a worksheet with outlined rows. It also includes SUBTOTAL()
2646
+ # functions so that it looks like the type of automatic outlines that are
2647
+ # generated when you use the Excel Data->SubTotals menu item.
2648
+ #
2649
+
2650
+
2651
+ # For outlines the important parameters are $hidden and $level. Rows with the
2652
+ # same $level are grouped together. The group will be collapsed if $hidden is
2653
+ # non-zero. $height and $XF are assigned default values if they are undef.
2654
+ #
2655
+ # The syntax is: set_row($row, $height, $XF, $hidden, $level, $collapsed)
2656
+ #
2657
+ worksheet1.set_row(1, nil, nil, 0, 2)
2658
+ worksheet1.set_row(2, nil, nil, 0, 2)
2659
+ worksheet1.set_row(3, nil, nil, 0, 2)
2660
+ worksheet1.set_row(4, nil, nil, 0, 2)
2661
+ worksheet1.set_row(5, nil, nil, 0, 1)
2662
+
2663
+ worksheet1.set_row(6, nil, nil, 0, 2)
2664
+ worksheet1.set_row(7, nil, nil, 0, 2)
2665
+ worksheet1.set_row(8, nil, nil, 0, 2)
2666
+ worksheet1.set_row(9, nil, nil, 0, 2)
2667
+ worksheet1.set_row(10, nil, nil, 0, 1)
2668
+
2669
+
2670
+ # Add a column format for clarity
2671
+ worksheet1.set_column('A:A', 20)
2672
+
2673
+ # Add the data, labels and formulas
2674
+ worksheet1.write('A1', 'Region', bold)
2675
+ worksheet1.write('A2', 'North')
2676
+ worksheet1.write('A3', 'North')
2677
+ worksheet1.write('A4', 'North')
2678
+ worksheet1.write('A5', 'North')
2679
+ worksheet1.write('A6', 'North Total', bold)
2680
+
2681
+ worksheet1.write('B1', 'Sales', bold)
2682
+ worksheet1.write('B2', 1000)
2683
+ worksheet1.write('B3', 1200)
2684
+ worksheet1.write('B4', 900)
2685
+ worksheet1.write('B5', 1200)
2686
+ worksheet1.write('B6', '=SUBTOTAL(9,B2:B5)', bold)
2687
+
2688
+ worksheet1.write('A7', 'South')
2689
+ worksheet1.write('A8', 'South')
2690
+ worksheet1.write('A9', 'South')
2691
+ worksheet1.write('A10', 'South')
2692
+ worksheet1.write('A11', 'South Total', bold)
2693
+
2694
+ worksheet1.write('B7', 400)
2695
+ worksheet1.write('B8', 600)
2696
+ worksheet1.write('B9', 500)
2697
+ worksheet1.write('B10', 600)
2698
+ worksheet1.write('B11', '=SUBTOTAL(9,B7:B10)', bold)
2699
+
2700
+ worksheet1.write('A12', 'Grand Total', bold)
2701
+ worksheet1.write('B12', '=SUBTOTAL(9,B2:B10)', bold)
2702
+
2703
+
2704
+ ###############################################################################
2705
+ #
2706
+ # Example 2: Create a worksheet with outlined rows. This is the same as the
2707
+ # previous example except that the rows are collapsed.
2708
+ # Note: We need to indicate the row that contains the collapsed symbol '+'
2709
+ # with the optional parameter, $collapsed.
2710
+
2711
+ # The group will be collapsed if $hidden is non-zero.
2712
+ # The syntax is: set_row($row, $height, $XF, $hidden, $level, $collapsed)
2713
+ #
2714
+ worksheet2.set_row(1, nil, nil, 1, 2)
2715
+ worksheet2.set_row(2, nil, nil, 1, 2)
2716
+ worksheet2.set_row(3, nil, nil, 1, 2)
2717
+ worksheet2.set_row(4, nil, nil, 1, 2)
2718
+ worksheet2.set_row(5, nil, nil, 1, 1)
2719
+
2720
+ worksheet2.set_row(6, nil, nil, 1, 2)
2721
+ worksheet2.set_row(7, nil, nil, 1, 2)
2722
+ worksheet2.set_row(8, nil, nil, 1, 2)
2723
+ worksheet2.set_row(9, nil, nil, 1, 2)
2724
+ worksheet2.set_row(10, nil, nil, 1, 1)
2725
+ worksheet2.set_row(11, nil, nil, 0, 0, 1)
2726
+
2727
+
2728
+ # Add a column format for clarity
2729
+ worksheet2.set_column('A:A', 20)
2730
+
2731
+ # Add the data, labels and formulas
2732
+ worksheet2.write('A1', 'Region', bold)
2733
+ worksheet2.write('A2', 'North')
2734
+ worksheet2.write('A3', 'North')
2735
+ worksheet2.write('A4', 'North')
2736
+ worksheet2.write('A5', 'North')
2737
+ worksheet2.write('A6', 'North Total', bold)
2738
+
2739
+ worksheet2.write('B1', 'Sales', bold)
2740
+ worksheet2.write('B2', 1000)
2741
+ worksheet2.write('B3', 1200)
2742
+ worksheet2.write('B4', 900)
2743
+ worksheet2.write('B5', 1200)
2744
+ worksheet2.write('B6', '=SUBTOTAL(9,B2:B5)', bold)
2745
+
2746
+ worksheet2.write('A7', 'South')
2747
+ worksheet2.write('A8', 'South')
2748
+ worksheet2.write('A9', 'South')
2749
+ worksheet2.write('A10', 'South')
2750
+ worksheet2.write('A11', 'South Total', bold)
2751
+
2752
+ worksheet2.write('B7', 400)
2753
+ worksheet2.write('B8', 600)
2754
+ worksheet2.write('B9', 500)
2755
+ worksheet2.write('B10', 600)
2756
+ worksheet2.write('B11', '=SUBTOTAL(9,B7:B10)', bold)
2757
+
2758
+ worksheet2.write('A12', 'Grand Total', bold)
2759
+ worksheet2.write('B12', '=SUBTOTAL(9,B2:B10)', bold)
2760
+
2761
+
2762
+
2763
+ ###############################################################################
2764
+ #
2765
+ # Example 3: Create a worksheet with outlined columns.
2766
+ #
2767
+ data = [
2768
+ ['Month', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', ' Total'],
2769
+ ['North', 50, 20, 15, 25, 65, 80, '=SUM(B2:G2)'],
2770
+ ['South', 10, 20, 30, 50, 50, 50, '=SUM(B3:G3)'],
2771
+ ['East', 45, 75, 50, 15, 75, 100, '=SUM(B4:G4)'],
2772
+ ['West', 15, 15, 55, 35, 20, 50, '=SUM(B5:G6)']
2773
+ ]
2774
+
2775
+ # Add bold format to the first row
2776
+ worksheet3.set_row(0, nil, bold)
2777
+
2778
+ # Syntax: set_column(col1, col2, width, XF, hidden, level, collapsed)
2779
+ worksheet3.set_column('A:A', 10, bold )
2780
+ worksheet3.set_column('B:G', 5, nil, 0, 1)
2781
+ worksheet3.set_column('H:H', 10)
2782
+
2783
+ # Write the data and a formula
2784
+ worksheet3.write_col('A1', data)
2785
+ worksheet3.write('H6', '=SUM(H2:H5)', bold)
2786
+
2787
+
2788
+
2789
+ ###############################################################################
2790
+ #
2791
+ # Example 4: Show all possible outline levels.
2792
+ #
2793
+ levels = [
2794
+ "Level 1", "Level 2", "Level 3", "Level 4",
2795
+ "Level 5", "Level 6", "Level 7", "Level 6",
2796
+ "Level 5", "Level 4", "Level 3", "Level 2", "Level 1"
2797
+ ]
2798
+
2799
+ worksheet4.write_col('A1', levels)
2800
+
2801
+ worksheet4.set_row(0, nil, nil, nil, 1)
2802
+ worksheet4.set_row(1, nil, nil, nil, 2)
2803
+ worksheet4.set_row(2, nil, nil, nil, 3)
2804
+ worksheet4.set_row(3, nil, nil, nil, 4)
2805
+ worksheet4.set_row(4, nil, nil, nil, 5)
2806
+ worksheet4.set_row(5, nil, nil, nil, 6)
2807
+ worksheet4.set_row(6, nil, nil, nil, 7)
2808
+ worksheet4.set_row(7, nil, nil, nil, 6)
2809
+ worksheet4.set_row(8, nil, nil, nil, 5)
2810
+ worksheet4.set_row(9, nil, nil, nil, 4)
2811
+ worksheet4.set_row(10, nil, nil, nil, 3)
2812
+ worksheet4.set_row(11, nil, nil, nil, 2)
2813
+ worksheet4.set_row(12, nil, nil, nil, 1)
2814
+
2815
+ workbook.close
2816
+
2817
+ # do assertion
2818
+ compare_file("#{PERL_OUTDIR}/outline.xls", @file)
2819
+ end
2820
+
2821
+ def test_outline_collapsed
2822
+ # Create a new workbook and add some worksheets
2823
+ workbook = WriteExcel.new(@file)
2824
+ worksheet1 = workbook.add_worksheet('Outlined Rows')
2825
+ worksheet2 = workbook.add_worksheet('Collapsed Rows 1')
2826
+ worksheet3 = workbook.add_worksheet('Collapsed Rows 2')
2827
+ worksheet4 = workbook.add_worksheet('Collapsed Rows 3')
2828
+ worksheet5 = workbook.add_worksheet('Outline Columns')
2829
+ worksheet6 = workbook.add_worksheet('Collapsed Columns')
2830
+
2831
+ # Add a general format
2832
+ bold = workbook.add_format(:bold => 1)
2833
+
2834
+ #
2835
+ # This function will generate the same data and sub-totals on each worksheet.
2836
+ #
2837
+ def create_sub_totals(worksheet, bold)
2838
+ # Add a column format for clarity
2839
+ worksheet.set_column('A:A', 20)
2840
+
2841
+ # Add the data, labels and formulas
2842
+ worksheet.write('A1', 'Region', bold)
2843
+ worksheet.write('A2', 'North')
2844
+ worksheet.write('A3', 'North')
2845
+ worksheet.write('A4', 'North')
2846
+ worksheet.write('A5', 'North')
2847
+ worksheet.write('A6', 'North Total', bold)
2848
+
2849
+ worksheet.write('B1', 'Sales', bold)
2850
+ worksheet.write('B2', 1000)
2851
+ worksheet.write('B3', 1200)
2852
+ worksheet.write('B4', 900)
2853
+ worksheet.write('B5', 1200)
2854
+ worksheet.write('B6', '=SUBTOTAL(9,B2:B5)', bold)
2855
+
2856
+ worksheet.write('A7', 'South')
2857
+ worksheet.write('A8', 'South')
2858
+ worksheet.write('A9', 'South')
2859
+ worksheet.write('A10', 'South')
2860
+ worksheet.write('A11', 'South Total', bold)
2861
+
2862
+ worksheet.write('B7', 400)
2863
+ worksheet.write('B8', 600)
2864
+ worksheet.write('B9', 500)
2865
+ worksheet.write('B10', 600)
2866
+ worksheet.write('B11', '=SUBTOTAL(9,B7:B10)', bold)
2867
+
2868
+ worksheet.write('A12', 'Grand Total', bold)
2869
+ worksheet.write('B12', '=SUBTOTAL(9,B2:B10)', bold)
2870
+
2871
+ end
2872
+
2873
+
2874
+ ###############################################################################
2875
+ #
2876
+ # Example 1: Create a worksheet with outlined rows. It also includes SUBTOTAL()
2877
+ # functions so that it looks like the type of automatic outlines that are
2878
+ # generated when you use the Excel Data.SubTotals menu item.
2879
+ #
2880
+
2881
+ # The syntax is: set_row(row, height, XF, hidden, level, collapsed)
2882
+ worksheet1.set_row(1, nil, nil, 0, 2)
2883
+ worksheet1.set_row(2, nil, nil, 0, 2)
2884
+ worksheet1.set_row(3, nil, nil, 0, 2)
2885
+ worksheet1.set_row(4, nil, nil, 0, 2)
2886
+ worksheet1.set_row(5, nil, nil, 0, 1)
2887
+
2888
+ worksheet1.set_row(6, nil, nil, 0, 2)
2889
+ worksheet1.set_row(7, nil, nil, 0, 2)
2890
+ worksheet1.set_row(8, nil, nil, 0, 2)
2891
+ worksheet1.set_row(9, nil, nil, 0, 2)
2892
+ worksheet1.set_row(10, nil, nil, 0, 1)
2893
+
2894
+ # Write the sub-total data that is common to the row examples.
2895
+ create_sub_totals(worksheet1, bold)
2896
+
2897
+
2898
+ ###############################################################################
2899
+ #
2900
+ # Example 2: Create a worksheet with collapsed outlined rows.
2901
+ # This is the same as the example 1 except that the all rows are collapsed.
2902
+ # Note: We need to indicate the row that contains the collapsed symbol '+' with
2903
+ # the optional parameter, collapsed.
2904
+
2905
+ worksheet2.set_row(1, nil, nil, 1, 2)
2906
+ worksheet2.set_row(2, nil, nil, 1, 2)
2907
+ worksheet2.set_row(3, nil, nil, 1, 2)
2908
+ worksheet2.set_row(4, nil, nil, 1, 2)
2909
+ worksheet2.set_row(5, nil, nil, 1, 1)
2910
+
2911
+ worksheet2.set_row(6, nil, nil, 1, 2)
2912
+ worksheet2.set_row(7, nil, nil, 1, 2)
2913
+ worksheet2.set_row(8, nil, nil, 1, 2)
2914
+ worksheet2.set_row(9, nil, nil, 1, 2)
2915
+ worksheet2.set_row(10, nil, nil, 1, 1)
2916
+
2917
+ worksheet2.set_row(11, nil, nil, 0, 0, 1)
2918
+
2919
+ # Write the sub-total data that is common to the row examples.
2920
+ create_sub_totals(worksheet2, bold)
2921
+
2922
+
2923
+ ###############################################################################
2924
+ #
2925
+ # Example 3: Create a worksheet with collapsed outlined rows.
2926
+ # Same as the example 1 except that the two sub-totals are collapsed.
2927
+
2928
+ worksheet3.set_row(1, nil, nil, 1, 2)
2929
+ worksheet3.set_row(2, nil, nil, 1, 2)
2930
+ worksheet3.set_row(3, nil, nil, 1, 2)
2931
+ worksheet3.set_row(4, nil, nil, 1, 2)
2932
+ worksheet3.set_row(5, nil, nil, 0, 1, 1)
2933
+
2934
+ worksheet3.set_row(6, nil, nil, 1, 2)
2935
+ worksheet3.set_row(7, nil, nil, 1, 2)
2936
+ worksheet3.set_row(8, nil, nil, 1, 2)
2937
+ worksheet3.set_row(9, nil, nil, 1, 2)
2938
+ worksheet3.set_row(10, nil, nil, 0, 1, 1)
2939
+
2940
+
2941
+ # Write the sub-total data that is common to the row examples.
2942
+ create_sub_totals(worksheet3, bold)
2943
+
2944
+
2945
+ ###############################################################################
2946
+ #
2947
+ # Example 4: Create a worksheet with outlined rows.
2948
+ # Same as the example 1 except that the two sub-totals are collapsed.
2949
+
2950
+ worksheet4.set_row(1, nil, nil, 1, 2)
2951
+ worksheet4.set_row(2, nil, nil, 1, 2)
2952
+ worksheet4.set_row(3, nil, nil, 1, 2)
2953
+ worksheet4.set_row(4, nil, nil, 1, 2)
2954
+ worksheet4.set_row(5, nil, nil, 1, 1, 1)
2955
+
2956
+ worksheet4.set_row(6, nil, nil, 1, 2)
2957
+ worksheet4.set_row(7, nil, nil, 1, 2)
2958
+ worksheet4.set_row(8, nil, nil, 1, 2)
2959
+ worksheet4.set_row(9, nil, nil, 1, 2)
2960
+ worksheet4.set_row(10, nil, nil, 1, 1, 1)
2961
+
2962
+ worksheet4.set_row(11, nil, nil, 0, 0, 1)
2963
+
2964
+ # Write the sub-total data that is common to the row examples.
2965
+ create_sub_totals(worksheet4, bold)
2966
+
2967
+
2968
+
2969
+ ###############################################################################
2970
+ #
2971
+ # Example 5: Create a worksheet with outlined columns.
2972
+ #
2973
+ data = [
2974
+ ['Month', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',' Total'],
2975
+ ['North', 50, 20, 15, 25, 65, 80, '=SUM(B2:G2)'],
2976
+ ['South', 10, 20, 30, 50, 50, 50, '=SUM(B3:G3)'],
2977
+ ['East', 45, 75, 50, 15, 75, 100, '=SUM(B4:G4)'],
2978
+ ['West', 15, 15, 55, 35, 20, 50, '=SUM(B5:G6)']
2979
+ ]
2980
+
2981
+ # Add bold format to the first row
2982
+ worksheet5.set_row(0, nil, bold)
2983
+
2984
+ # Syntax: set_column(col1, col2, width, XF, hidden, level, collapsed)
2985
+ worksheet5.set_column('A:A', 10, bold )
2986
+ worksheet5.set_column('B:G', 5, nil, 0, 1)
2987
+ worksheet5.set_column('H:H', 10 )
2988
+
2989
+ # Write the data and a formula
2990
+ worksheet5.write_col('A1', data)
2991
+ worksheet5.write('H6', '=SUM(H2:H5)', bold)
2992
+
2993
+
2994
+ ###############################################################################
2995
+ #
2996
+ # Example 6: Create a worksheet with collapsed outlined columns.
2997
+ # This is the same as the previous example except collapsed columns.
2998
+
2999
+ # Add bold format to the first row
3000
+ worksheet6.set_row(0, nil, bold)
3001
+
3002
+ # Syntax: set_column(col1, col2, width, XF, hidden, level, collapsed)
3003
+ worksheet6.set_column('A:A', 10, bold )
3004
+ worksheet6.set_column('B:G', 5, nil, 1, 1 )
3005
+ worksheet6.set_column('H:H', 10, nil, 0, 0, 1)
3006
+
3007
+ # Write the data and a formula
3008
+ worksheet6.write_col('A1', data)
3009
+ worksheet6.write('H6', '=SUM(H2:H5)', bold)
3010
+
3011
+ workbook.close
3012
+
3013
+ # do assertion
3014
+ compare_file("#{PERL_OUTDIR}/outline_collapsed.xls", @file)
3015
+ end
3016
+
3017
+ def test_panes
3018
+ workbook = WriteExcel.new(@file)
3019
+
3020
+ worksheet1 = workbook.add_worksheet('Panes 1')
3021
+ worksheet2 = workbook.add_worksheet('Panes 2')
3022
+ worksheet3 = workbook.add_worksheet('Panes 3')
3023
+ worksheet4 = workbook.add_worksheet('Panes 4')
3024
+
3025
+ # Freeze panes
3026
+ worksheet1.freeze_panes(1, 0) # 1 row
3027
+
3028
+ worksheet2.freeze_panes(0, 1) # 1 column
3029
+ worksheet3.freeze_panes(1, 1) # 1 row and column
3030
+
3031
+ # Split panes.
3032
+ # The divisions must be specified in terms of row and column dimensions.
3033
+ # The default row height is 12.75 and the default column width is 8.43
3034
+ #
3035
+ worksheet4.split_panes(12.75, 8.43, 1, 1) # 1 row and column
3036
+
3037
+
3038
+ #######################################################################
3039
+ #
3040
+ # Set up some formatting and text to highlight the panes
3041
+ #
3042
+
3043
+ header = workbook.add_format
3044
+ header.set_color('white')
3045
+ header.set_align('center')
3046
+ header.set_align('vcenter')
3047
+ header.set_pattern
3048
+ header.set_fg_color('green')
3049
+
3050
+ center = workbook.add_format
3051
+ center.set_align('center')
3052
+
3053
+ #######################################################################
3054
+ #
3055
+ # Sheet 1
3056
+ #
3057
+
3058
+ worksheet1.set_column('A:I', 16)
3059
+ worksheet1.set_row(0, 20)
3060
+ worksheet1.set_selection('C3')
3061
+
3062
+ (0..8).each { |i| worksheet1.write(0, i, 'Scroll down', header) }
3063
+ (1..100).each do |i|
3064
+ (0..8).each { |j| worksheet1.write(i, j, i + 1, center) }
3065
+ end
3066
+
3067
+ #######################################################################
3068
+ #
3069
+ # Sheet 2
3070
+ #
3071
+
3072
+ worksheet2.set_column('A:A', 16)
3073
+ worksheet2.set_selection('C3')
3074
+
3075
+ (0..49).each do |i|
3076
+ worksheet2.set_row(i, 15)
3077
+ worksheet2.write(i, 0, 'Scroll right', header)
3078
+ end
3079
+
3080
+ (0..49).each do |i|
3081
+ (1..25).each { |j| worksheet2.write(i, j, j, center) }
3082
+ end
3083
+
3084
+ #######################################################################
3085
+ #
3086
+ # Sheet 3
3087
+ #
3088
+
3089
+ worksheet3.set_column('A:Z', 16)
3090
+ worksheet3.set_selection('C3')
3091
+
3092
+ (1..25).each { |i| worksheet3.write(0, i, 'Scroll down', header) }
3093
+
3094
+ (1..49).each { |i| worksheet3.write(i, 0, 'Scroll right', header) }
3095
+
3096
+ (1..49).each do |i|
3097
+ (1..25).each { |j| worksheet3.write(i, j, j, center) }
3098
+ end
3099
+
3100
+ #######################################################################
3101
+ #
3102
+ # Sheet 4
3103
+ #
3104
+
3105
+ worksheet4.set_selection('C3')
3106
+
3107
+ (1..25).each { |i| worksheet4.write(0, i, 'Scroll', center) }
3108
+
3109
+ (1..49).each { |i| worksheet4.write(i, 0, 'Scroll', center) }
3110
+
3111
+ (1..49).each do |i|
3112
+ (1..25).each { |j| worksheet4.write(i, j, j, center) }
3113
+ end
3114
+
3115
+ workbook.close
3116
+
3117
+ # do assertion
3118
+ compare_file("#{PERL_OUTDIR}/panes.xls", @file)
3119
+ end
3120
+
3121
+ def test_right_to_left
3122
+ workbook = WriteExcel.new(@file)
3123
+ worksheet1 = workbook.add_worksheet
3124
+ worksheet2 = workbook.add_worksheet
3125
+
3126
+ worksheet2.right_to_left
3127
+
3128
+ worksheet1.write(0, 0, 'Hello') # A1, B1, C1, ...
3129
+ worksheet2.write(0, 0, 'Hello') # ..., C1, B1, A1
3130
+
3131
+ workbook.close
3132
+
3133
+ # do assertion
3134
+ compare_file("#{PERL_OUTDIR}/right_to_left.xls", @file)
3135
+ end
3136
+
2539
3137
  def compare_file(expected, target)
2540
3138
  # target is StringIO object.
2541
3139
  assert_equal(