~ubuntu-branches/ubuntu/gutsy/horae/gutsy

« back to all changes in this revision

Viewing changes to 0CPAN/Spreadsheet-WriteExcel-2.15/bin/chartex

  • Committer: Bazaar Package Importer
  • Author(s): Carlo Segre
  • Date: 2006-12-28 12:36:48 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20061228123648-9xnjr76wfthd92cq
Tags: 064-1
New upstream release, dropped dependency on libtk-filedialog-perl.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/perl -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 2004, John McNamara, jmcnamara@cpan.org
9
 
#
10
 
# Documentation after __END__
11
 
#
12
 
 
13
 
 
14
 
use strict;
15
 
use OLE::Storage_Lite;
16
 
use Getopt::Long;
17
 
use Pod::Usage;
18
 
 
19
 
 
20
 
my $man         = 0;
21
 
my $help        = 0;
22
 
my $in_chart    = 0;
23
 
my $chart_name  = 'chart';
24
 
my $chart_index = 1;
25
 
my $sheet_index = -1;
26
 
my @sheetnames;
27
 
my @exrefs;
28
 
 
29
 
 
30
 
#
31
 
# Do the Getopt and Pod::Usage routines.
32
 
#
33
 
GetOptions(
34
 
            'help|?'    => \$help,
35
 
            'man'       => \$man,
36
 
            'chart=s'   => \$chart_name,
37
 
          ) or pod2usage(2);
38
 
 
39
 
pod2usage(1) if $help;
40
 
pod2usage(-verbose => 2) if $man;
41
 
 
42
 
 
43
 
# From the Pod::Usage pod:
44
 
# If no arguments were given, then allow STDIN to be used only
45
 
# if it's not connected to a terminal (otherwise print usage)
46
 
pod2usage() if @ARGV == 0 && -t STDIN;
47
 
 
48
 
 
49
 
 
50
 
 
51
 
# Check that the file can be opened because OLE::Storage_Lite won't tell us.
52
 
# Possible race condition here. Could fix with latest OLE::Storage_Lite. TODO.
53
 
#
54
 
my $file = $ARGV[0];
55
 
 
56
 
open  TMP, $file or die "Couldn't open $file. $!\n";
57
 
close TMP;
58
 
 
59
 
my $ole      = OLE::Storage_Lite->new($file);
60
 
my $book97   = pack 'v*', unpack 'C*', 'Workbook';
61
 
my $workbook = ($ole->getPpsSearch([$book97], 1, 1))[0];
62
 
 
63
 
die "Couldn't find Excel97 data in file $file.\n" unless $workbook;
64
 
 
65
 
 
66
 
# Write the data to a file so that we can access it with read().
67
 
my $tmpfile = IO::File->new_tmpfile();
68
 
binmode $tmpfile;
69
 
 
70
 
my $biff = $workbook->{Data};
71
 
print {$tmpfile} $biff;
72
 
seek $tmpfile, 0, 0;
73
 
 
74
 
 
75
 
 
76
 
my $header;
77
 
my $data;
78
 
 
79
 
# Read the file record by record and look for a chart BOF record.
80
 
#
81
 
while (read $tmpfile, $header, 4) {
82
 
 
83
 
    my ($record, $length) = unpack "vv", $header;
84
 
    next unless $record;
85
 
 
86
 
    read $tmpfile, $data, $length;
87
 
 
88
 
    # BOUNDSHEET
89
 
    if ($record == 0x0085) {
90
 
        push @sheetnames, substr $data, 8;
91
 
    }
92
 
 
93
 
    # EXTERNSHEET
94
 
    if ($record == 0x0017) {
95
 
        my $count = unpack 'v', $data;
96
 
 
97
 
        for my $i (1 .. $count) {
98
 
            my @tmp = unpack 'vvv', substr($data, 2 +6*($i-1));
99
 
            push @exrefs, [@tmp];
100
 
        }
101
 
 
102
 
    }
103
 
 
104
 
    # BOF
105
 
    if ($record == 0x0809) {
106
 
        my $type = unpack 'xx v', $data;
107
 
 
108
 
        if ($type == 0x0020) {
109
 
            my $filename = sprintf "%s%02d.bin", $chart_name, $chart_index;
110
 
            open    CHART, ">$filename" or die "Couldn't open $filename: $!";
111
 
            binmode CHART;
112
 
            printf "\nExtracting \"%s\" to %s", $sheetnames[$sheet_index],
113
 
                                                $filename;
114
 
            $in_chart = 1;
115
 
            $chart_index++;
116
 
        }
117
 
        $sheet_index++;
118
 
    }
119
 
 
120
 
    if ($in_chart) {
121
 
        print CHART $header, $data;
122
 
    }
123
 
 
124
 
    # EOF
125
 
    if ($record == 0x000A) {
126
 
            $in_chart = 0;
127
 
    }
128
 
}
129
 
 
130
 
 
131
 
 
132
 
print "\n\n", ('=' x 60), "\n";
133
 
print "Add the following near the start of your program.\n";
134
 
print "Change variable name \$worksheet if required.\n\n";
135
 
 
136
 
for my $aref (@exrefs) {
137
 
    my $sheet1 = $sheetnames[$aref->[1]];
138
 
    my $sheet2 = $sheetnames[$aref->[2]];
139
 
 
140
 
    my $range;
141
 
 
142
 
    if ($sheet1 ne $sheet2) {
143
 
        $range = $sheet1 . ":" .  $sheet2;
144
 
    }
145
 
    else {
146
 
        $range = $sheet1;
147
 
    }
148
 
 
149
 
    $range = "'$range'" if $range =~ /[^\w:]/;
150
 
 
151
 
    print "    \$worksheet->store_formula(\"=$range!A1\");\n";
152
 
}
153
 
 
154
 
 
155
 
 
156
 
 
157
 
__END__
158
 
 
159
 
 
160
 
=head1 NAME
161
 
 
162
 
chartex - A utility to extract charts from an Excel file for insertion into a Spreadsheet::WriteExcel file.
163
 
 
164
 
=head1 DESCRIPTION
165
 
 
166
 
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.
167
 
 
168
 
See the C<add_chart_ext()> section of the  Spreadsheet::WriteExcel documentation for more details.
169
 
 
170
 
 
171
 
=head1 SYNOPSIS
172
 
 
173
 
chartex [--chartname --help --man] file.xls
174
 
 
175
 
    Options:
176
 
        --chartname -c  The root name for the extracted charts,
177
 
                        defaults to "chart".
178
 
 
179
 
 
180
 
=head1 OPTIONS
181
 
 
182
 
=over 4
183
 
 
184
 
=item B<--chartname or -c>
185
 
 
186
 
This sets the root name for the extracted charts, defaults to "chart". For example:
187
 
 
188
 
    $ chartex file.xls
189
 
 
190
 
    Extracting "Chart1" to chart01.bin
191
 
 
192
 
 
193
 
    $ chartex -c mychart file.xls
194
 
 
195
 
    Extracting "Chart1" to mychart01.bin
196
 
 
197
 
=item B<--help or -h>
198
 
 
199
 
Print a brief help message and exits.
200
 
 
201
 
 
202
 
=item B<--man or -m>
203
 
 
204
 
Prints the manual page and exits.
205
 
 
206
 
=back
207
 
 
208
 
 
209
 
=head1 AUTHOR
210
 
 
211
 
John McNamara jmcnamara@cpan.org
212
 
 
213
 
 
214
 
=head1 VERSION
215
 
 
216
 
Version 0.01.
217
 
 
218
 
 
219
 
=head1 COPYRIGHT
220
 
 
221
 
� MMV, John McNamara.
222
 
 
223
 
All Rights Reserved. This program is free software. It may be used, redistributed and/or modified under the same terms as Perl itself.
224
 
 
225
 
 
226
 
=cut