writeexcel 0.3.1 → 0.3.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +21 -0
- data/{README → README.rdoc} +14 -3
- data/Rakefile +52 -0
- data/VERSION +1 -0
- data/charts/chartex.rb +315 -0
- data/charts/demo1.rb +45 -0
- data/charts/demo101.bin +0 -0
- data/charts/demo2.rb +64 -0
- data/charts/demo201.bin +0 -0
- data/charts/demo3.rb +116 -0
- data/charts/demo301.bin +0 -0
- data/charts/demo4.rb +118 -0
- data/charts/demo401.bin +0 -0
- data/charts/demo5.rb +47 -0
- data/charts/demo501.bin +0 -0
- data/examples/demo.rb +4 -1
- data/examples/images.rb +14 -4
- data/examples/stats.rb +0 -2
- data/lib/writeexcel.rb +0 -1
- data/lib/writeexcel/excelformula.y +139 -0
- data/lib/writeexcel/formula.rb +3 -1
- data/lib/writeexcel/workbook.rb +9 -9
- data/test/helper.rb +9 -0
- data/test/perl_output/defined_name.xls +0 -0
- data/test/test_00_IEEE_double.rb +1 -3
- data/test/test_01_add_worksheet.rb +1 -4
- data/test/test_02_merge_formats.rb +1 -4
- data/test/test_04_dimensions.rb +1 -4
- data/test/test_05_rows.rb +1 -4
- data/test/test_06_extsst.rb +1 -4
- data/test/test_11_date_time.rb +1 -4
- data/test/test_12_date_only.rb +1 -4
- data/test/test_13_date_seconds.rb +1 -4
- data/test/test_21_escher.rb +1 -4
- data/test/test_22_mso_drawing_group.rb +1 -4
- data/test/test_23_note.rb +1 -4
- data/test/test_24_txo.rb +1 -4
- data/test/test_25_position_object.rb +1 -4
- data/test/test_26_autofilter.rb +1 -4
- data/test/test_27_autofilter.rb +1 -4
- data/test/test_28_autofilter.rb +1 -4
- data/test/test_29_process_jpg.rb +1 -4
- data/test/test_30_validation_dval.rb +1 -4
- data/test/test_31_validation_dv_strings.rb +1 -4
- data/test/test_32_validation_dv_formula.rb +1 -4
- data/test/test_40_property_types.rb +1 -5
- data/test/test_41_properties.rb +1 -5
- data/test/test_42_set_properties.rb +1 -5
- data/test/test_50_name_stored.rb +1 -4
- data/test/test_51_name_print_area.rb +1 -4
- data/test/test_52_name_print_titles.rb +1 -4
- data/test/test_53_autofilter.rb +1 -4
- data/test/test_60_chart_generic.rb +1 -4
- data/test/test_61_chart_subclasses.rb +1 -4
- data/test/test_62_chart_formats.rb +1 -4
- data/test/test_63_chart_area_formats.rb +1 -4
- data/test/{tc_biff.rb → test_biff.rb} +1 -4
- data/test/{tc_example_match.rb → test_example_match.rb} +84 -69
- data/test/{tc_format.rb → test_format.rb} +1 -4
- data/test/{tc_formula.rb → test_formula.rb} +1 -4
- data/test/{tc_ole.rb → test_ole.rb} +1 -4
- data/test/{tc_storage_lite.rb → test_storage_lite.rb} +1 -4
- data/test/{tc_workbook.rb → test_workbook.rb} +1 -4
- data/test/{tc_worksheet.rb → test_worksheet.rb} +1 -4
- data/writeexcel.gemspec +287 -16
- metadata +123 -35
- data/test/tc_all.rb +0 -32
- data/test/tc_chart.rb +0 -22
- data/test/test_chartex.rb +0 -35
- data/test/ts_all.rb +0 -46
data/.document
ADDED
data/.gitignore
ADDED
data/{README → README.rdoc}
RENAMED
@@ -1,8 +1,8 @@
|
|
1
|
-
=
|
1
|
+
= writeexcel
|
2
2
|
|
3
|
-
|
3
|
+
Write to a cross-platform Excel binary file.
|
4
4
|
|
5
|
-
== Recent Changes
|
5
|
+
== Recent Major Changes
|
6
6
|
|
7
7
|
0.3.0
|
8
8
|
* Chart support(Area, Bar, Column, Line, Pie, Scatter, Stock).
|
@@ -92,7 +92,18 @@ correctly.
|
|
92
92
|
Original was written in Perl by John McNamara (jmcnamara@cpan.org).
|
93
93
|
|
94
94
|
Convert to ruby by Hideo Nakamura (cxn03651@msj.biglobe.ne.jp)
|
95
|
+
Copyright (c) 2009-2010 Hideo NAKAMURA. See LICENSE for details.
|
95
96
|
|
96
97
|
== License
|
97
98
|
|
98
99
|
same as Ruby.
|
100
|
+
|
101
|
+
== Note on Patches/Pull Requests
|
102
|
+
|
103
|
+
* Fork the project.
|
104
|
+
* Make your feature addition or bug fix.
|
105
|
+
* Add tests for it. This is important so I don't break it in a
|
106
|
+
future version unintentionally.
|
107
|
+
* Commit, do not mess with rakefile, version, or history.
|
108
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
109
|
+
* Send me a pull request. Bonus points for topic branches.
|
data/Rakefile
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "writeexcel"
|
8
|
+
gem.summary = %Q{Write to a cross-platform Excel binary file.}
|
9
|
+
gem.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.}
|
10
|
+
gem.email = "cxn03651@msj.biglobe.ne.jp"
|
11
|
+
gem.homepage = "http://wiki.github.com/cxn03651/writeexcel/"
|
12
|
+
gem.authors = ["Hideo NAKAMURA"]
|
13
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
14
|
+
end
|
15
|
+
Jeweler::GemcutterTasks.new
|
16
|
+
rescue LoadError
|
17
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
18
|
+
end
|
19
|
+
|
20
|
+
require 'rake/testtask'
|
21
|
+
Rake::TestTask.new(:test) do |test|
|
22
|
+
test.libs << 'lib' << 'test'
|
23
|
+
test.pattern = 'test/**/test_*.rb'
|
24
|
+
test.verbose = true
|
25
|
+
end
|
26
|
+
|
27
|
+
begin
|
28
|
+
require 'rcov/rcovtask'
|
29
|
+
Rcov::RcovTask.new do |test|
|
30
|
+
test.libs << 'test'
|
31
|
+
test.pattern = 'test/**/test_*.rb'
|
32
|
+
test.verbose = true
|
33
|
+
end
|
34
|
+
rescue LoadError
|
35
|
+
task :rcov do
|
36
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
task :test => :check_dependencies
|
41
|
+
|
42
|
+
task :default => :test
|
43
|
+
|
44
|
+
require 'rake/rdoctask'
|
45
|
+
Rake::RDocTask.new do |rdoc|
|
46
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
47
|
+
|
48
|
+
rdoc.rdoc_dir = 'rdoc'
|
49
|
+
rdoc.title = "writeexcel #{version}"
|
50
|
+
rdoc.rdoc_files.include('README*')
|
51
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
52
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.3.2
|
data/charts/chartex.rb
ADDED
@@ -0,0 +1,315 @@
|
|
1
|
+
#!/usr/bin/ruby -w
|
2
|
+
|
3
|
+
#######################################################################
|
4
|
+
#
|
5
|
+
# chartex - A utility to extract charts from an Excel file for
|
6
|
+
# insertion into a Spreadsheet::WriteExcel file.
|
7
|
+
#
|
8
|
+
# reverse('ゥ'), September 2007, John McNamara, jmcnamara@cpan.org
|
9
|
+
#
|
10
|
+
# original written in Perl by John McNamara
|
11
|
+
# converted to Ruby by Hideo Nakamura, cxn03651@msj.biglobe.ne.jp
|
12
|
+
#
|
13
|
+
# Documentation after __END__
|
14
|
+
#
|
15
|
+
|
16
|
+
require 'writeexcel'
|
17
|
+
|
18
|
+
class Chartex
|
19
|
+
attr_reader :file
|
20
|
+
|
21
|
+
def initialize(file = nil)
|
22
|
+
@file = file
|
23
|
+
@sheetnames = Array.new
|
24
|
+
@exrefs = Array.new
|
25
|
+
@buf = StringIO.new
|
26
|
+
end
|
27
|
+
|
28
|
+
def set_file(file)
|
29
|
+
@file = file
|
30
|
+
end
|
31
|
+
|
32
|
+
def get_workbook(file = nil)
|
33
|
+
file ||= @file
|
34
|
+
ole = OLEStorageLite.new(file)
|
35
|
+
book97 = 'Workbook'.unpack('C*').pack('v*')
|
36
|
+
workbook = ole.getPpsSearch([book97], 1, 1)[0]
|
37
|
+
@buf.write(workbook.data)
|
38
|
+
@buf.rewind
|
39
|
+
workbook
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
# main
|
44
|
+
|
45
|
+
if $0 == __FILE__
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
=begin
|
50
|
+
my $man = 0;
|
51
|
+
my $help = 0;
|
52
|
+
my $in_chart = 0;
|
53
|
+
my $chart_name = 'chart';
|
54
|
+
my $chart_index = 1;
|
55
|
+
my $sheet_index = -1;
|
56
|
+
my @sheetnames;
|
57
|
+
my @exrefs;
|
58
|
+
my $depth_count = 0;
|
59
|
+
my $max_font = 0;
|
60
|
+
|
61
|
+
#
|
62
|
+
# Do the Getopt and Pod::Usage routines.
|
63
|
+
#
|
64
|
+
GetOptions(
|
65
|
+
'help|?' => \$help,
|
66
|
+
'man' => \$man,
|
67
|
+
'chart=s' => \$chart_name,
|
68
|
+
) or pod2usage(2);
|
69
|
+
|
70
|
+
pod2usage(1) if $help;
|
71
|
+
pod2usage(-verbose => 2) if $man;
|
72
|
+
|
73
|
+
|
74
|
+
# From the Pod::Usage pod:
|
75
|
+
# If no arguments were given, then allow STDIN to be used only
|
76
|
+
# if it's not connected to a terminal (otherwise print usage)
|
77
|
+
pod2usage() if @ARGV == 0 && -t STDIN;
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
|
82
|
+
# Check that the file can be opened because OLE::Storage_Lite won't tell us.
|
83
|
+
# Possible race condition here. Could fix with latest OLE::Storage_Lite. TODO.
|
84
|
+
#
|
85
|
+
my $file = $ARGV[0];
|
86
|
+
|
87
|
+
open TMP, $file or die "Couldn't open $file. $!\n";
|
88
|
+
close TMP;
|
89
|
+
|
90
|
+
my $ole = OLE::Storage_Lite->new($file);
|
91
|
+
my $book97 = pack 'v*', unpack 'C*', 'Workbook';
|
92
|
+
my $workbook = ($ole->getPpsSearch([$book97], 1, 1))[0];
|
93
|
+
|
94
|
+
die "Couldn't find Excel97 data in file $file.\n" unless $workbook;
|
95
|
+
|
96
|
+
|
97
|
+
# Write the data to a file so that we can access it with read().
|
98
|
+
my $tmpfile = IO::File->new_tmpfile();
|
99
|
+
binmode $tmpfile;
|
100
|
+
|
101
|
+
my $biff = $workbook->{Data};
|
102
|
+
print {$tmpfile} $biff;
|
103
|
+
seek $tmpfile, 0, 0;
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
my $header;
|
108
|
+
my $data;
|
109
|
+
|
110
|
+
# Read the file record by record and look for a chart BOF record.
|
111
|
+
#
|
112
|
+
while (read $tmpfile, $header, 4) {
|
113
|
+
|
114
|
+
my ($record, $length) = unpack "vv", $header;
|
115
|
+
next unless $record;
|
116
|
+
|
117
|
+
read $tmpfile, $data, $length;
|
118
|
+
|
119
|
+
# BOUNDSHEET
|
120
|
+
if ($record == 0x0085) {
|
121
|
+
push @sheetnames, substr $data, 8;
|
122
|
+
}
|
123
|
+
|
124
|
+
# EXTERNSHEET
|
125
|
+
if ($record == 0x0017) {
|
126
|
+
my $count = unpack 'v', $data;
|
127
|
+
|
128
|
+
for my $i (1 .. $count) {
|
129
|
+
my @tmp = unpack 'vvv', substr($data, 2 +6*($i-1));
|
130
|
+
push @exrefs, [@tmp];
|
131
|
+
}
|
132
|
+
|
133
|
+
}
|
134
|
+
|
135
|
+
# BOF
|
136
|
+
if ($record == 0x0809) {
|
137
|
+
my $type = unpack 'xx v', $data;
|
138
|
+
|
139
|
+
if ($type == 0x0020) {
|
140
|
+
my $filename = sprintf "%s%02d.bin", $chart_name, $chart_index;
|
141
|
+
open CHART, ">$filename" or die "Couldn't open $filename: $!";
|
142
|
+
binmode CHART;
|
143
|
+
|
144
|
+
my $sheet_name = $sheetnames[$sheet_index];
|
145
|
+
$sheet_name .= ' embedded' if $depth_count;
|
146
|
+
|
147
|
+
printf "\nExtracting \%s\ to %s", $sheet_name, $filename;
|
148
|
+
$in_chart = 1;
|
149
|
+
$chart_index++;
|
150
|
+
}
|
151
|
+
$depth_count++;
|
152
|
+
}
|
153
|
+
|
154
|
+
|
155
|
+
# FBI, Chart fonts
|
156
|
+
if ($record == 0x1060) {
|
157
|
+
|
158
|
+
my $index = substr $data, 8, 2, '';
|
159
|
+
$index = unpack 'v', $index;
|
160
|
+
|
161
|
+
# Ignore the inbuilt fonts.
|
162
|
+
if ($index >= 5) {
|
163
|
+
$max_font = $index if $index > $max_font;
|
164
|
+
|
165
|
+
# Shift index past S::WE fonts
|
166
|
+
$index += 2;
|
167
|
+
}
|
168
|
+
|
169
|
+
$data .= pack 'v', $index;
|
170
|
+
}
|
171
|
+
|
172
|
+
# FONTX, Chart fonts
|
173
|
+
if ($record == 0x1026) {
|
174
|
+
|
175
|
+
my $index = unpack 'v', $data;
|
176
|
+
|
177
|
+
# Ignore the inbuilt fonts.
|
178
|
+
if ($index >= 5) {
|
179
|
+
$max_font = $index if $index > $max_font;
|
180
|
+
|
181
|
+
# Shift index past S::WE fonts
|
182
|
+
$index += 2;
|
183
|
+
}
|
184
|
+
|
185
|
+
$data = pack 'v', $index;
|
186
|
+
}
|
187
|
+
|
188
|
+
|
189
|
+
|
190
|
+
if ($in_chart) {
|
191
|
+
print CHART $header, $data;
|
192
|
+
}
|
193
|
+
|
194
|
+
|
195
|
+
# EOF
|
196
|
+
if ($record == 0x000A) {
|
197
|
+
$in_chart = 0;
|
198
|
+
$depth_count--;
|
199
|
+
$sheet_index++ if $depth_count == 0;
|
200
|
+
;
|
201
|
+
}
|
202
|
+
}
|
203
|
+
|
204
|
+
|
205
|
+
if ($chart_index > 1) {
|
206
|
+
print "\n\n";
|
207
|
+
print "Add the following near the start of your program\n";
|
208
|
+
print "and change the variable names if required.\n\n";
|
209
|
+
}
|
210
|
+
else {
|
211
|
+
print "\nNo charts found in workbook\n";
|
212
|
+
}
|
213
|
+
|
214
|
+
for my $aref (@exrefs) {
|
215
|
+
my $sheet1 = $sheetnames[$aref->[1]];
|
216
|
+
my $sheet2 = $sheetnames[$aref->[2]];
|
217
|
+
|
218
|
+
my $range;
|
219
|
+
|
220
|
+
if ($sheet1 ne $sheet2) {
|
221
|
+
$range = $sheet1 . ":" . $sheet2;
|
222
|
+
}
|
223
|
+
else {
|
224
|
+
$range = $sheet1;
|
225
|
+
}
|
226
|
+
|
227
|
+
$range = "'$range'" if $range =~ /[^\w:]/;
|
228
|
+
|
229
|
+
print " \$worksheet->store_formula('=$range!A1');\n";
|
230
|
+
}
|
231
|
+
|
232
|
+
print "\n";
|
233
|
+
|
234
|
+
for my $i (5 .. $max_font) {
|
235
|
+
|
236
|
+
printf " my \$chart_font_%d = \$workbook->add_format(font_only => 1);\n",
|
237
|
+
$i -4;
|
238
|
+
|
239
|
+
}
|
240
|
+
|
241
|
+
|
242
|
+
|
243
|
+
|
244
|
+
|
245
|
+
__END__
|
246
|
+
|
247
|
+
|
248
|
+
=head1 NAME
|
249
|
+
|
250
|
+
chartex - A utility to extract charts from an Excel file for insertion into a Spreadsheet::WriteExcel file.
|
251
|
+
|
252
|
+
=head1 DESCRIPTION
|
253
|
+
|
254
|
+
This program is used for extracting one or more charts from an Excel file in binary format. The charts can then be included in a C<Spreadsheet::WriteExcel> file.
|
255
|
+
|
256
|
+
See the C<add_chart_ext()> section of the Spreadsheet::WriteExcel documentation for more details.
|
257
|
+
|
258
|
+
|
259
|
+
=head1 SYNOPSIS
|
260
|
+
|
261
|
+
chartex [--chartname --help --man] file.xls
|
262
|
+
|
263
|
+
Options:
|
264
|
+
--chartname -c The root name for the extracted charts,
|
265
|
+
defaults to "chart".
|
266
|
+
|
267
|
+
|
268
|
+
=head1 OPTIONS
|
269
|
+
|
270
|
+
=over 4
|
271
|
+
|
272
|
+
=item B<--chartname or -c>
|
273
|
+
|
274
|
+
This sets the root name for the extracted charts, defaults to "chart". For example:
|
275
|
+
|
276
|
+
$ chartex file.xls
|
277
|
+
|
278
|
+
Extracting "Chart1" to chart01.bin
|
279
|
+
|
280
|
+
|
281
|
+
$ chartex -c mychart file.xls
|
282
|
+
|
283
|
+
Extracting "Chart1" to mychart01.bin
|
284
|
+
|
285
|
+
=item B<--help or -h>
|
286
|
+
|
287
|
+
Print a brief help message and exits.
|
288
|
+
|
289
|
+
|
290
|
+
=item B<--man or -m>
|
291
|
+
|
292
|
+
Prints the manual page and exits.
|
293
|
+
|
294
|
+
=back
|
295
|
+
|
296
|
+
|
297
|
+
=head1 AUTHOR
|
298
|
+
|
299
|
+
John McNamara jmcnamara@cpan.org
|
300
|
+
|
301
|
+
|
302
|
+
=head1 VERSION
|
303
|
+
|
304
|
+
Version 0.02.
|
305
|
+
|
306
|
+
|
307
|
+
=head1 COPYRIGHT
|
308
|
+
|
309
|
+
ゥ MMV, John McNamara.
|
310
|
+
|
311
|
+
All Rights Reserved. This program is free software. It may be used, redistributed and/or modified under the same terms as Perl itself.
|
312
|
+
|
313
|
+
|
314
|
+
=cut
|
315
|
+
=end
|
data/charts/demo1.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
#!/usr/bin/ruby -w
|
2
|
+
|
3
|
+
###############################################################################
|
4
|
+
#
|
5
|
+
# Simple example of how to add an externally created chart to a Spreadsheet::
|
6
|
+
# WriteExcel file.
|
7
|
+
#
|
8
|
+
#
|
9
|
+
# This example adds a line chart extracted from the file Chart1.xls as follows:
|
10
|
+
#
|
11
|
+
# perl chartex.pl -c=demo1 Chart1.xls
|
12
|
+
#
|
13
|
+
#
|
14
|
+
# reverse('ゥ'), September 2004, John McNamara, jmcnamara@cpan.org
|
15
|
+
#
|
16
|
+
# original written in Perl by John McNamara
|
17
|
+
# converted to Ruby by Hideo Nakamura, cxn03651@msj.biglobe.ne.jp
|
18
|
+
#
|
19
|
+
|
20
|
+
require 'writeexcel'
|
21
|
+
|
22
|
+
workbook = WriteExcel.new("demo1.xls")
|
23
|
+
worksheet = workbook.add_worksheet
|
24
|
+
|
25
|
+
# Add the chart extracted using the chartex utility
|
26
|
+
chart = workbook.add_chart_ext('demo101.bin', 'Chart1')
|
27
|
+
|
28
|
+
# Link the chart to the worksheet data using a dummy formula.
|
29
|
+
worksheet.store_formula('=Sheet1!A1')
|
30
|
+
|
31
|
+
# Add some extra formats to cover formats used in the charts.
|
32
|
+
chart_font_1 = workbook.add_format(:font_only => 1)
|
33
|
+
chart_font_2 = workbook.add_format(:font_only => 1)
|
34
|
+
chart_font_3 = workbook.add_format(:font_only => 1)
|
35
|
+
|
36
|
+
# Add all other formats (if any).
|
37
|
+
|
38
|
+
# Add data to range that the chart refers to.
|
39
|
+
nums = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
|
40
|
+
squares = [0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
|
41
|
+
|
42
|
+
worksheet.write_col('A1', nums )
|
43
|
+
worksheet.write_col('B1', squares)
|
44
|
+
|
45
|
+
workbook.close
|