writeexcel 0.4.0 → 0.4.1
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/.gitignore +3 -0
- data/README.rdoc +4 -0
- data/VERSION +1 -1
- data/examples/utf8.rb +15 -0
- data/lib/writeexcel.rb +24 -1
- data/lib/writeexcel/biffwriter.rb +2 -44
- data/lib/writeexcel/caller_info.rb +12 -0
- data/lib/writeexcel/chart.rb +11 -85
- data/lib/writeexcel/colors.rb +59 -0
- data/lib/writeexcel/compatibility.rb +1 -1
- data/lib/writeexcel/debug_info.rb +33 -0
- data/lib/writeexcel/excelformula.y +139 -139
- data/lib/writeexcel/format.rb +23 -124
- data/lib/writeexcel/formula.rb +6 -3
- data/lib/writeexcel/helper.rb +19 -0
- data/lib/writeexcel/olewriter.rb +1 -1
- data/lib/writeexcel/storage_lite.rb +4 -4
- data/lib/writeexcel/workbook.rb +156 -279
- data/lib/writeexcel/worksheet.rb +335 -541
- data/lib/writeexcel/write_file.rb +40 -0
- data/test/perl_output/README +31 -31
- data/test/perl_output/utf8.xls +0 -0
- data/test/test_example_match.rb +14 -0
- data/test/test_format.rb +17 -17
- data/test/test_workbook.rb +23 -0
- data/test/test_worksheet.rb +0 -1
- data/writeexcel.gemspec +9 -2
- data/writeexcel.rdoc +266 -64
- metadata +10 -3
@@ -0,0 +1,40 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
class WriteFile
|
4
|
+
###############################################################################
|
5
|
+
#
|
6
|
+
# _prepend($data)
|
7
|
+
#
|
8
|
+
# General storage function
|
9
|
+
#
|
10
|
+
def prepend(*args)
|
11
|
+
data = args.collect{ |arg| arg.dup.force_encoding('ASCII-8BIT') }.join
|
12
|
+
data = add_continue(data) if data.bytesize > @limit
|
13
|
+
|
14
|
+
@datasize += data.bytesize
|
15
|
+
@data = data + @data
|
16
|
+
|
17
|
+
data
|
18
|
+
end
|
19
|
+
|
20
|
+
###############################################################################
|
21
|
+
#
|
22
|
+
# _append($data)
|
23
|
+
#
|
24
|
+
# General storage function
|
25
|
+
#
|
26
|
+
def append(*args)
|
27
|
+
data = args.collect{ |arg| arg.dup.force_encoding('ASCII-8BIT') }.join
|
28
|
+
# Add CONTINUE records if necessary
|
29
|
+
data = add_continue(data) if data.bytesize > @limit
|
30
|
+
if @using_tmpfile
|
31
|
+
@filehandle.write data
|
32
|
+
@datasize += data.bytesize
|
33
|
+
else
|
34
|
+
@datasize += data.bytesize
|
35
|
+
@data = @data + data
|
36
|
+
end
|
37
|
+
|
38
|
+
data
|
39
|
+
end
|
40
|
+
end
|
data/test/perl_output/README
CHANGED
@@ -1,31 +1,31 @@
|
|
1
|
-
###############################################################################
|
2
|
-
# This file contains sample output from the Spreadsheet::WriteExcel Perl
|
3
|
-
# module. Used for testing file sizes/contents
|
4
|
-
###############################################################################
|
5
|
-
|
6
|
-
Files:
|
7
|
-
|
8
|
-
ws_ -> from Worksheet.pm
|
9
|
-
f_ -> from Format.pm
|
10
|
-
ole_ -> from OLEWriter.pm
|
11
|
-
|
12
|
-
f_font_biff
|
13
|
-
Output of the _get_font call
|
14
|
-
|
15
|
-
f_font_key
|
16
|
-
Output of the _get_font_key call
|
17
|
-
|
18
|
-
f_xf_biff
|
19
|
-
Output of the _get_xf call
|
20
|
-
|
21
|
-
ws_colinfo
|
22
|
-
Output of the _store_colinfo call (default values)
|
23
|
-
|
24
|
-
ws_store_dimensions
|
25
|
-
Output of the _store_dimensions call
|
26
|
-
|
27
|
-
ws_store_window2
|
28
|
-
Output of the _store_window call
|
29
|
-
|
30
|
-
ws_store_selection
|
31
|
-
Output of the _store_selection call
|
1
|
+
###############################################################################
|
2
|
+
# This file contains sample output from the Spreadsheet::WriteExcel Perl
|
3
|
+
# module. Used for testing file sizes/contents
|
4
|
+
###############################################################################
|
5
|
+
|
6
|
+
Files:
|
7
|
+
|
8
|
+
ws_ -> from Worksheet.pm
|
9
|
+
f_ -> from Format.pm
|
10
|
+
ole_ -> from OLEWriter.pm
|
11
|
+
|
12
|
+
f_font_biff
|
13
|
+
Output of the _get_font call
|
14
|
+
|
15
|
+
f_font_key
|
16
|
+
Output of the _get_font_key call
|
17
|
+
|
18
|
+
f_xf_biff
|
19
|
+
Output of the _get_xf call
|
20
|
+
|
21
|
+
ws_colinfo
|
22
|
+
Output of the _store_colinfo call (default values)
|
23
|
+
|
24
|
+
ws_store_dimensions
|
25
|
+
Output of the _store_dimensions call
|
26
|
+
|
27
|
+
ws_store_window2
|
28
|
+
Output of the _store_window call
|
29
|
+
|
30
|
+
ws_store_selection
|
31
|
+
Output of the _store_selection call
|
Binary file
|
data/test/test_example_match.rb
CHANGED
@@ -3127,4 +3127,18 @@ def test_right_to_left
|
|
3127
3127
|
# do assertion
|
3128
3128
|
compare_file("#{PERL_OUTDIR}/right_to_left.xls", @file)
|
3129
3129
|
end
|
3130
|
+
|
3131
|
+
def test_utf8
|
3132
|
+
workbook = WriteExcel.new(@file)
|
3133
|
+
worksheet = workbook.add_worksheet('シート1')
|
3134
|
+
format = workbook.add_format(:font => 'MS 明朝')
|
3135
|
+
worksheet.set_footer('フッター')
|
3136
|
+
worksheet.set_header('ヘッダー')
|
3137
|
+
worksheet.write('A1', 'UTF8文字列', format)
|
3138
|
+
worksheet.write('A2', '=CONCATENATE(A1,"の連結")', format)
|
3139
|
+
workbook.close
|
3140
|
+
|
3141
|
+
# do assertion
|
3142
|
+
compare_file("#{PERL_OUTDIR}/utf8.xls", @file)
|
3143
|
+
end
|
3130
3144
|
end
|
data/test/test_format.rb
CHANGED
@@ -1112,23 +1112,23 @@ def get_valid_format_properties
|
|
1112
1112
|
end
|
1113
1113
|
|
1114
1114
|
def get_valid_color_string_number
|
1115
|
-
|
1116
|
-
|
1117
|
-
|
1118
|
-
|
1119
|
-
|
1120
|
-
|
1121
|
-
|
1122
|
-
|
1123
|
-
|
1124
|
-
|
1125
|
-
|
1126
|
-
|
1127
|
-
|
1128
|
-
|
1129
|
-
|
1130
|
-
|
1131
|
-
|
1115
|
+
{
|
1116
|
+
:black => 8,
|
1117
|
+
:blue => 12,
|
1118
|
+
:brown => 16,
|
1119
|
+
:cyan => 15,
|
1120
|
+
:gray => 23,
|
1121
|
+
:green => 17,
|
1122
|
+
:lime => 11,
|
1123
|
+
:magenta => 14,
|
1124
|
+
:navy => 18,
|
1125
|
+
:orange => 53,
|
1126
|
+
:pink => 33,
|
1127
|
+
:purple => 20,
|
1128
|
+
:red => 10,
|
1129
|
+
:silver => 22,
|
1130
|
+
:white => 9,
|
1131
|
+
:yellow => 13
|
1132
1132
|
}
|
1133
1133
|
end
|
1134
1134
|
# :rotation => -90,
|
data/test/test_workbook.rb
CHANGED
@@ -113,4 +113,27 @@ def invalid_sheetname
|
|
113
113
|
]
|
114
114
|
end
|
115
115
|
|
116
|
+
def test_add_format_must_accept_one_or_more_hash_params
|
117
|
+
font = {
|
118
|
+
:font => 'MS 明朝',
|
119
|
+
:size => 12,
|
120
|
+
:color => 'blue',
|
121
|
+
:bold => 1
|
122
|
+
}
|
123
|
+
shading = {
|
124
|
+
:bg_color => 'green',
|
125
|
+
:pattern => 1
|
126
|
+
}
|
127
|
+
properties = font.merge(shading)
|
128
|
+
|
129
|
+
format1 = @workbook.add_format(properties)
|
130
|
+
format2 = @workbook.add_format(font, shading)
|
131
|
+
assert(format_equal?(format1, format2))
|
132
|
+
end
|
133
|
+
|
134
|
+
def format_equal?(f1, f2)
|
135
|
+
require 'yaml'
|
136
|
+
re = /xf_index: \d+\n/
|
137
|
+
YAML.dump(f1).sub(re, '') == YAML.dump(f2).sub(re, '')
|
138
|
+
end
|
116
139
|
end
|
data/test/test_worksheet.rb
CHANGED
data/writeexcel.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{writeexcel}
|
8
|
-
s.version = "0.4.
|
8
|
+
s.version = "0.4.1"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Hideo NAKAMURA"]
|
12
|
-
s.date = %q{2010-04-
|
12
|
+
s.date = %q{2010-04-27}
|
13
13
|
s.description = %q{Multiple worksheets can be added to a workbook and formatting can be applied to cells. Text, numbers, formulas, hyperlinks and images can be written to the cells.}
|
14
14
|
s.email = %q{cxn03651@msj.biglobe.ne.jp}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -80,9 +80,11 @@ Gem::Specification.new do |s|
|
|
80
80
|
"examples/stats.rb",
|
81
81
|
"examples/stocks.rb",
|
82
82
|
"examples/tab_colors.rb",
|
83
|
+
"examples/utf8.rb",
|
83
84
|
"examples/write_arrays.rb",
|
84
85
|
"lib/writeexcel.rb",
|
85
86
|
"lib/writeexcel/biffwriter.rb",
|
87
|
+
"lib/writeexcel/caller_info.rb",
|
86
88
|
"lib/writeexcel/chart.rb",
|
87
89
|
"lib/writeexcel/charts/area.rb",
|
88
90
|
"lib/writeexcel/charts/bar.rb",
|
@@ -92,7 +94,9 @@ Gem::Specification.new do |s|
|
|
92
94
|
"lib/writeexcel/charts/pie.rb",
|
93
95
|
"lib/writeexcel/charts/scatter.rb",
|
94
96
|
"lib/writeexcel/charts/stock.rb",
|
97
|
+
"lib/writeexcel/colors.rb",
|
95
98
|
"lib/writeexcel/compatibility.rb",
|
99
|
+
"lib/writeexcel/debug_info.rb",
|
96
100
|
"lib/writeexcel/excelformula.y",
|
97
101
|
"lib/writeexcel/excelformulaparser.rb",
|
98
102
|
"lib/writeexcel/format.rb",
|
@@ -103,6 +107,7 @@ Gem::Specification.new do |s|
|
|
103
107
|
"lib/writeexcel/storage_lite.rb",
|
104
108
|
"lib/writeexcel/workbook.rb",
|
105
109
|
"lib/writeexcel/worksheet.rb",
|
110
|
+
"lib/writeexcel/write_file.rb",
|
106
111
|
"test/excelfile/Chart1.xls",
|
107
112
|
"test/excelfile/Chart2.xls",
|
108
113
|
"test/excelfile/Chart3.xls",
|
@@ -165,6 +170,7 @@ Gem::Specification.new do |s|
|
|
165
170
|
"test/perl_output/stocks.xls",
|
166
171
|
"test/perl_output/tab_colors.xls",
|
167
172
|
"test/perl_output/unicode_cyrillic.xls",
|
173
|
+
"test/perl_output/utf8.xls",
|
168
174
|
"test/perl_output/workbook1.xls",
|
169
175
|
"test/perl_output/workbook2.xls",
|
170
176
|
"test/perl_output/ws_colinfo",
|
@@ -315,6 +321,7 @@ Gem::Specification.new do |s|
|
|
315
321
|
"examples/stats.rb",
|
316
322
|
"examples/stocks.rb",
|
317
323
|
"examples/tab_colors.rb",
|
324
|
+
"examples/utf8.rb",
|
318
325
|
"examples/write_arrays.rb"
|
319
326
|
]
|
320
327
|
|
data/writeexcel.rdoc
CHANGED
@@ -1,19 +1,74 @@
|
|
1
|
-
=
|
1
|
+
= 目次
|
2
|
+
概要
|
3
|
+
説明
|
4
|
+
クイックスタート
|
5
|
+
WORDBOOK メソッド
|
6
|
+
WORKSHEET メソッド
|
7
|
+
PAGE セットアップメソッド
|
8
|
+
セルフォーマッティング
|
9
|
+
色の扱い
|
10
|
+
日付、時刻の扱い
|
11
|
+
アウトライン、グルーピング
|
12
|
+
データの検証
|
13
|
+
数式と関数
|
2
14
|
|
3
|
-
|
15
|
+
== 概要
|
16
|
+
最初の Sheet に、文字列、書式付き文字列、数値、数式を書き込んだ ruby.xls を作成するには、
|
17
|
+
次のようにします。
|
4
18
|
|
5
|
-
|
19
|
+
require 'WriteExcel'
|
6
20
|
|
7
|
-
|
21
|
+
# エクセルワークブックオブジェクトを作成
|
22
|
+
workbook = WriteExcel.new('ruby.xls')
|
8
23
|
|
9
|
-
|
10
|
-
=
|
24
|
+
# シートを追加
|
25
|
+
worksheet = workbook.add_worksheet
|
11
26
|
|
12
|
-
|
27
|
+
# 書式を作成
|
28
|
+
format = workbook.add_format # Add a format
|
29
|
+
format.set_bold()
|
30
|
+
format.set_color('red')
|
31
|
+
format.set_align('center')
|
32
|
+
|
33
|
+
# 文字列を書式指定有りと無しの2形式で格納。行・桁でセルを指定。
|
34
|
+
col = row = 0
|
35
|
+
worksheet.write(row, col, 'Hi Excel!', format)
|
36
|
+
worksheet.write(1, col, 'Hi Excel!')
|
37
|
+
|
38
|
+
# 数値と数式を格納。A1形式でセルを指定。
|
39
|
+
worksheet.write('A3', 1.2345)
|
40
|
+
worksheet.write('A4', '=SIN(PI()/4)')
|
41
|
+
|
42
|
+
# 作成を完了し、エクセルファイルを書き出し。
|
43
|
+
workbook.close
|
44
|
+
|
45
|
+
== 説明
|
46
|
+
|
47
|
+
WriteExcelライブラリは、エクセルのバイナリファイルを作成することができます。プラットフォームを問いません。
|
48
|
+
複数のワークシートを作成することができますし、セルの書式を設定することもできます。
|
49
|
+
テキスト、数値、数式、ハイパーリンク、画像を格納することが出来ます。
|
50
|
+
|
51
|
+
このライブラリで作成されるエクセルファイルは、エクセル97、2000、2002、2003、2007と互換性があります。
|
52
|
+
|
53
|
+
このライブラリはWindows、UNIX、マッキントッシュプラットフォームのほとんどで動作します。
|
54
|
+
作成されたファイルは、LinuxやUNIXのスプレッドシートアプリケーションであるGnumericやOpenOffice.orgとも互換性があります。
|
55
|
+
|
56
|
+
このライブラリは、既存のエクセルファイルの編集には使うことができません。
|
57
|
+
|
58
|
+
このライブラリは、PerlのSpreadsheet::WriteexcelモジュールをRubyに移植したものです。
|
59
|
+
http://search.cpan.org/~jmcnamara/Spreadsheet-WriteExcel-2.37/
|
60
|
+
|
61
|
+
== クイックスタート
|
62
|
+
|
63
|
+
WriteExcelは、エクセルの機能へのできるだけ多くのインターフェイスを提供しようとしています。
|
64
|
+
結果として、インターフェイスに関する多くのドキュメントがあります。最初はどれが重要でどれが
|
65
|
+
そうでないかわかりずらいでしょう。イケアの家具を組み立てるのが好みの人は、まずこの使用説明を読んでください。
|
66
|
+
4つの簡単なステップです。
|
13
67
|
|
14
68
|
1.new()メソッドで新規にエクセルのワークブックを作成する。
|
15
69
|
2.add_worksheet()メソッドでこのワークブックにワークシートを加える。
|
16
70
|
3.write()メソッドでこのワークシートにデータを書き込む。
|
71
|
+
4.ファイルに保存する。
|
17
72
|
|
18
73
|
こんな感じです。
|
19
74
|
|
@@ -22,11 +77,11 @@ Spreadsheet::WriteExcelは、エクセルの機能へのできるだけ多くの
|
|
22
77
|
workbook = Spreadsheet::WriteExcel.new('ruby.xls') # Step 1
|
23
78
|
worksheet = workbook.add_worksheet # Step 2
|
24
79
|
worksheet.write('A1', 'Hi Excel!') # Step 3
|
25
|
-
workbook.close #
|
26
|
-
|
27
|
-
これで、一つのワークシートを持ち、A1セルに'Hi Excel!'というテキストが書き込まれたエクセルファイルが作成されます。これでできあがりです。実際は0番目のステップがありますが、これはgoes without saying. 多くの使用例がgemには含まれていますので、それを参考にすることも出来ます。 See EXAMPLES
|
80
|
+
workbook.close # Step 4
|
28
81
|
|
29
|
-
|
82
|
+
これで、一つのワークシートを持ち、A1セルに'Hi Excel!'というテキストが書き込まれたエクセル
|
83
|
+
ファイル(ruby.xls)が作成されます。これでできあがりです。多くの使用例がgemには含まれていますので、
|
84
|
+
それを参考にすることも出来ます。
|
30
85
|
|
31
86
|
= Workbook メソッド
|
32
87
|
|
@@ -35,10 +90,11 @@ Spreadsheet::WriteExcel ライブラリは新規のワークブックへのオ
|
|
35
90
|
new
|
36
91
|
add_worksheet
|
37
92
|
add_format
|
38
|
-
|
93
|
+
add_chart
|
94
|
+
add_chart_ext
|
39
95
|
close
|
40
96
|
compatibility_mode
|
41
|
-
set_properties
|
97
|
+
set_properties
|
42
98
|
set_tempdir
|
43
99
|
set_custom_color
|
44
100
|
sheets
|
@@ -213,72 +269,73 @@ set_codepage()はめったに必要にはなりません。
|
|
213
269
|
|
214
270
|
= Worksheet メソッド
|
215
271
|
|
216
|
-
|
272
|
+
ワークブックオブジェクトnのadd_worksheet()メソッドを呼ぶことで、新しいワークシートが作成されます。
|
217
273
|
|
218
274
|
worksheet1 = workbook.add_worksheet
|
219
275
|
worksheet2 = workbook.add_worksheet
|
220
276
|
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
277
|
+
このワークシートに対して、次のメソッドを使うことが出来ます。
|
278
|
+
|
279
|
+
write
|
280
|
+
write_number
|
281
|
+
write_string
|
282
|
+
write_utf16be_string
|
283
|
+
write_utf16le_string
|
284
|
+
keep_leading_zeros
|
285
|
+
write_blank
|
286
|
+
write_row
|
287
|
+
write_col
|
288
|
+
write_date_time
|
289
|
+
write_url
|
290
|
+
write_url_range
|
291
|
+
write_formula
|
292
|
+
store_formula
|
293
|
+
repeat_formula
|
294
|
+
write_comment
|
295
|
+
show_comments
|
296
|
+
add_write_handler
|
297
|
+
insert_image
|
298
|
+
insert_chart
|
299
|
+
data_validation
|
300
|
+
get_name
|
301
|
+
activate
|
302
|
+
select
|
303
|
+
hide
|
304
|
+
set_first_sheet
|
305
|
+
protect
|
306
|
+
set_selection
|
307
|
+
set_row
|
308
|
+
set_column
|
309
|
+
outline_settings
|
310
|
+
freeze_panes
|
311
|
+
split_panes
|
312
|
+
merge_range
|
313
|
+
set_zoom
|
314
|
+
right_to_left
|
315
|
+
hide_zero
|
316
|
+
set_tab_color
|
317
|
+
autofilter
|
262
318
|
|
263
319
|
== セル・ノーテーション
|
264
320
|
|
265
|
-
|
321
|
+
WriteExcelはセルの位置を指定するのに2つの表記法、Row-Column法とA1法をサポートしています。
|
266
322
|
|
267
|
-
Row-Column法は行、桁ともに0から始まるインデックスですが、A1
|
323
|
+
Row-Column法は行、桁ともに0から始まるインデックスですが、A1法はエクセルで標準的に
|
324
|
+
用いられる桁を示す英字と行を示す1から始まる数字からなる英数字の並びです。例えば、
|
268
325
|
|
269
|
-
(0, 0)
|
270
|
-
('A1')
|
326
|
+
(0, 0) # Row-Column 法
|
327
|
+
('A1') # A1 法
|
271
328
|
|
272
|
-
(1999,29)
|
273
|
-
('AD2000') #
|
329
|
+
(1999,29) # Row-Column 法
|
330
|
+
('AD2000') # 同じセルを A1 法で
|
274
331
|
|
275
332
|
Row-column法はセルをプログラムで扱うのに便利です。
|
276
333
|
|
277
|
-
0.
|
334
|
+
(0..10).each do |i|
|
278
335
|
worksheet.write(i, 0, 'Hello') # Cells A1 to A10
|
279
336
|
end
|
280
337
|
|
281
|
-
A1
|
338
|
+
A1法はワークシートを手動で設定したりエクセルの数式で用いるときに便利です。
|
282
339
|
|
283
340
|
worksheet.write('H1', 200)
|
284
341
|
worksheet.write('H2', '=H1+1') # 201
|
@@ -289,7 +346,7 @@ A1法はワークシートを手動で設定したり数式を用いるときに
|
|
289
346
|
|
290
347
|
簡便さのために、以下のワークシートのメソッドではrow-column法で記述していますが、全てA1法も用いることができます。
|
291
348
|
|
292
|
-
注:エクセルではR1C1法も使うことができますが、
|
349
|
+
注:エクセルではR1C1法も使うことができますが、WriteExcelではサポートしていません。
|
293
350
|
|
294
351
|
== write(row, column, [token, format])
|
295
352
|
|
@@ -581,3 +638,148 @@ DOSやWindowsであっても、ファイル名の区切りには'/'を使用す
|
|
581
638
|
worksheet.write_formula('A4', '=IF(A3>1,"Yes", "No")' )
|
582
639
|
worksheet.write_formula('A5', '=AVERAGE(1, 2, 3, 4)' )
|
583
640
|
worksheet.write_formula('A6', '=DATEVALUE("1-Jan-2001")')
|
641
|
+
|
642
|
+
|
643
|
+
|
644
|
+
== ページセットアップメソッド
|
645
|
+
|
646
|
+
印刷されたときの書式設定ですね。ヘッダ・フッタやマージンなど。
|
647
|
+
次のメソッドが用意されています。
|
648
|
+
|
649
|
+
set_landscape
|
650
|
+
set_portrait
|
651
|
+
set_page_view
|
652
|
+
set_paper
|
653
|
+
center_horizontally
|
654
|
+
center_vertically
|
655
|
+
set_margins
|
656
|
+
set_header
|
657
|
+
set_footer
|
658
|
+
repeat_rows
|
659
|
+
repeat_columns
|
660
|
+
hide_gridlines
|
661
|
+
print_row_col_headers
|
662
|
+
print_area
|
663
|
+
print_across
|
664
|
+
fit_to_pages
|
665
|
+
set_start_page
|
666
|
+
set_print_scale
|
667
|
+
set_h_pagebreaks
|
668
|
+
set_v_pagebreaks
|
669
|
+
|
670
|
+
すべてのワークシートに同じ印刷書式を設定する場合は、ワークシートの sheet メソッドを使って
|
671
|
+
行うのが簡単です。
|
672
|
+
|
673
|
+
workbook.sheets.each do |worksheet|
|
674
|
+
worksheet.set_landscape
|
675
|
+
end
|
676
|
+
|
677
|
+
== セルフォーマッティング
|
678
|
+
|
679
|
+
ここではセルの書式設定について説明します。フォント、色、(塗りつぶし)パターン、枠線、配置、
|
680
|
+
数値書式などなどです。
|
681
|
+
|
682
|
+
=== フォーマットオブジェクトの作成と利用
|
683
|
+
|
684
|
+
セル書式はフォーマットオブジェクトを通して定義される。フォーマットオブジェクトは、workbook
|
685
|
+
の add_format メソッドで作成する。
|
686
|
+
|
687
|
+
format1 = workbook.add_format # プロパティは後で設定
|
688
|
+
format2 = workbook.add_format(property hash..) # 作成時にプロパティを設定
|
689
|
+
|
690
|
+
フォーマットオブジェクトはセルや行、桁に適用できる書式プロパティをすべて保持する。これらの
|
691
|
+
プロパティを設定するプロセスについては次節で述べられている。
|
692
|
+
|
693
|
+
フォーマットオブジェクトが作成されプロパティがセットされれば、worksheet の write メソッド
|
694
|
+
にパラメータとして渡して使う。
|
695
|
+
|
696
|
+
worksheet.write(0, 0, 'One', format)
|
697
|
+
worksheet.wirte_string(1, 0, 'Two', format)
|
698
|
+
worksheet.write_number(2, 0, 3, format)
|
699
|
+
worksheet.write_blank(3, 0, format)
|
700
|
+
|
701
|
+
フォーマットオブジェクトは、worksheet の set_row や set_column メソッドに渡され、行や
|
702
|
+
桁のデフォルトプロパティを設定するのにも用いられる。
|
703
|
+
|
704
|
+
worksheet.set_row(0, 15, format)
|
705
|
+
worksheet.set_column(0, 0, 15, format)
|
706
|
+
|
707
|
+
=== Format メソッドとプロパティ
|
708
|
+
|
709
|
+
Category Description Property Method Name
|
710
|
+
-------- ----------- -------- -----------
|
711
|
+
Font Font type font set_font()
|
712
|
+
Font size size set_size()
|
713
|
+
Font color color set_color()
|
714
|
+
Bold bold set_bold()
|
715
|
+
Italic italic set_italic()
|
716
|
+
Underline underline set_underline()
|
717
|
+
Strikeout font_strikeout set_font_strikeout()
|
718
|
+
Super/Subscript font_script set_font_script()
|
719
|
+
Outline font_outline set_font_outline()
|
720
|
+
Shadow font_shadow set_font_shadow()
|
721
|
+
|
722
|
+
Number Numeric format num_format set_num_format()
|
723
|
+
|
724
|
+
Protection Lock cells locked set_locked()
|
725
|
+
Hide formulas hidden set_hidden()
|
726
|
+
|
727
|
+
Alignment Horizontal align align set_align()
|
728
|
+
Vertical align valign set_align()
|
729
|
+
Rotation rotation set_rotation()
|
730
|
+
Text wrap text_wrap set_text_wrap()
|
731
|
+
Justify last text_justlast set_text_justlast()
|
732
|
+
Center across center_across set_center_across()
|
733
|
+
Indentation indent set_indent()
|
734
|
+
Shrink to fit shrink set_shrink()
|
735
|
+
|
736
|
+
Pattern Cell pattern pattern set_pattern()
|
737
|
+
Background color bg_color set_bg_color()
|
738
|
+
Foreground color fg_color set_fg_color()
|
739
|
+
|
740
|
+
Border Cell border border set_border()
|
741
|
+
Bottom border bottom set_bottom()
|
742
|
+
Top border top set_top()
|
743
|
+
Left border left set_left()
|
744
|
+
Right border right set_right()
|
745
|
+
Border color border_color set_border_color()
|
746
|
+
Bottom color bottom_color set_bottom_color()
|
747
|
+
Top color top_color set_top_color()
|
748
|
+
Left color left_color set_left_color()
|
749
|
+
Right color right_color set_right_color()
|
750
|
+
|
751
|
+
書式プロパティを設定するには2つの方法がある。すなわち、オブジェクトメソッドインターフェースを用いる
|
752
|
+
方法と、プロパティを直接設定する方法である。たとえば、メソッドインターフェースを用いる典型的な例は
|
753
|
+
次のようなものである。
|
754
|
+
|
755
|
+
format = workbook.add_format
|
756
|
+
format.set_bold
|
757
|
+
format.set_color('red')
|
758
|
+
|
759
|
+
フォーマットオブジェクトを作成する際に、直接プロパティを hash で渡して指定する方法と比較する。
|
760
|
+
|
761
|
+
format = workbook.add_format(:bold => 1, :color => 'red')
|
762
|
+
|
763
|
+
あるいは、フォーマットオブジェクトを作成した後に set_format_properties メソッドで指定する
|
764
|
+
やり方は次の通りとなる。
|
765
|
+
|
766
|
+
format = workbook.add_format
|
767
|
+
format.set_format_properties(:bold => 1, :color => 'red')
|
768
|
+
|
769
|
+
プロパティを1つあるいはいくつかのハッシュに格納してメソッドに渡すこともできる。
|
770
|
+
|
771
|
+
font = {
|
772
|
+
:font => 'MS 明朝',
|
773
|
+
:size => 12,
|
774
|
+
:color => 'blue',
|
775
|
+
:bold => 1
|
776
|
+
}
|
777
|
+
|
778
|
+
shading = {
|
779
|
+
:bg_color => 'green',
|
780
|
+
:pattern => 1
|
781
|
+
}
|
782
|
+
|
783
|
+
format1 = workbook.add_format(font) # フォントだけ設定
|
784
|
+
format2 = workbook.add_format(font, shading) # 両方を設定
|
785
|
+
|