Leser: 3
|< 1 2 >| | 16 Einträge, 2 Seiten |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
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
#! /usr/bin/perl
use warnings;
use strict;
use Getopt::Long;
use Win32::OLE;
use Win32::OLE::Const 'Microsoft Excel';
use Win32::OLE::Variant;
$|++;
my $filename;
my $visible = 0;
my $workSheetNumber = 1;
my $headline = 0;
my $maxEmptyRows = 20;
my $perlCode = "";
GetOptions('file=s' => \$filename, # filename
'visible' => \$visible, # see what you're doing
'sheetnumber=i' => \$workSheetNumber, # use which worksheet
'headline=i' => \$headline, # not yet used
'maxemptyrows' => \$maxEmptyRows, # how many rows may be empty?
'ane=s' => \$perlCode, # code to execute at @F
);
my $xls = XlsPerl->new( filename => $filename,
visible => $visible,
worksheetnumber => $workSheetNumber,
headline => $headline,
)
->open;
while( my @F = $xls->getNextRowData ) {
# setting up some variables
my $R = $xls->currentRow;
# ...
eval "$perlCode"; die $@ if $@;
# update row in excel sheet
# TODO
} # for $row
# ============================================================
package XlsPerl;
use Carp qw(croak);
# ------------------------------------------------------------
sub new {
my( $class, %params ) = @_;
my $self = bless( {}, $class );
foreach my $param (keys %params) {
$self->_getSet( $param, $params{$param} );
} # foreach
return $self;
} # new
# ------------------------------------------------------------
sub open {
my( $self ) = @_;
my $filename = $self->filename;
defined $filename or croak( "Error: no filename given");
-f $filename or croak( "Error: filename '$filename' doesn't exist");
my $excel; # try to re-use running instance of Excel
eval { $excel = Win32::OLE->GetActiveObject( 'Excel.Application' ) };
die "Error: no Excel installed\n" if $@;
unless( defined $excel ) { # if not running, start excel
$excel = Win32::OLE->new( 'Excel.Application', sub { $_[0]->Quit } )
or die "Error: can't start Excel\n";
} # unless
$self->excel( $excel );
# set window visible if it is wanted
$self->setVisible;
my $book = $excel->Workbooks->Open( $self->filename );
$self->workbook( $book );
my $sheet = $book->Worksheets( $self->worksheetnumber );
$self->worksheet( $sheet );
$self->currentRow( 1 ); # TODO: headline
$self->emptyRowCount( 0 );
return $self;
} # open
# ------------------------------------------------------------
sub setVisible {
my $self = shift;
$self->excel->{Visible} = $self->visible if $self->excel;
} # setVisible
# ------------------------------------------------------------
sub getNextRowData {
my( $self ) = @_;
my $row = $self->currentRow;
my $sheet = $self->worksheet;
return if $row > $sheet->Rows->Count;
# my $maxCols = $sheet->Columns->Count;
my $rowData = $sheet->Range("A$row:IV$row")->{Value}; # dirty
# "trimright" range (remove undefs at the end)
my( $itemsFound, @row) = (0);
foreach my $value ( reverse @{ $rowData->[0] } ) {
if( defined $value ) { unshift( @row, $value ); $itemsFound++ }
else { unshift( @row, '' ) if scalar @row }
} # foreach
# check
unless( $itemsFound ) {
$self->increaseEmptyRowCount;
return if $self->emptyRowCount > $maxEmptyRows;
return $self->getNextRowData();
} # unless
else {
$self->emptyRowCount( 0 );
} # else
$self->increaseCurrentRow;
return @row;
} # getNextRowData
# ------------------------------------------------------------
sub increaseCurrentRow {
my $self = shift;
$self->currentRow( 1 + $self->currentRow );
} # increaseCurrentRow
# ------------------------------------------------------------
sub increaseEmptyRowCount {
my $self = shift;
$self->emptyRowCount( 1 + $self->emptyRowCount );
} # increaseEmptyRowCount
# ------------------------------------------------------------
# ------------------------------------------------------------
# Object Interface Methods
# ------------------------------------------------------------
sub filename {
my $self = shift;
$self->_getSet( 'filename', @_ );
} # filename
# ------------------------------------------------------------
sub excel {
my $self = shift;
$self->_getSet( 'excel', @_ );
} # excel
# ------------------------------------------------------------
sub workbook {
my $self = shift;
$self->_getSet( 'workbook', @_ );
} # worksheet
# ------------------------------------------------------------
sub worksheet {
my $self = shift;
$self->_getSet( 'worksheet', @_ );
} # worksheet
# ------------------------------------------------------------
sub worksheetnumber {
my $self = shift;
$self->_getSet( 'worksheetnumber', @_ );
} # worksheetnumber
# ------------------------------------------------------------
sub visible {
my $self = shift;
$self->_getSet( 'visible', @_ );
} # visible
# ------------------------------------------------------------
sub headline {
my $self = shift;
$self->_getSet( 'headline', @_ );
} # visible
# ------------------------------------------------------------
sub currentRow {
my $self = shift;
$self->_getSet( 'currentRow', @_ );
} # currentRow
# ------------------------------------------------------------
sub emptyRowCount {
my $self = shift;
$self->_getSet( 'emptyRowCount', @_ );
} # emptyRowCount
# ------------------------------------------------------------
# ------------------------------------------------------------
# private subs
# ------------------------------------------------------------
sub _getSet {
my( $self, $attribute, @values) = @_;
$self->{ $attribute} = $values[0] if scalar @values;
return $self->{ $attribute };
} # _getSet
# ------------------------------------------------------------
sub DESTROY {
my $self = shift;
$self->excel->Quit if $self->excel;
} # DESTROY
# ------------------------------------------------------------
=pod
=head1 Prerequisites
=over 1
=item Win32::OLE
=item Excel needs to be installed
=back
=head1 Description
This script xlsPerl.pl loops over an excel file row by row (like
perl -ne), but skips empty rows. It splits the cells into an array
with name @F (like perl -ane), and executes a given bit of perl code
for each row.
Since I didn't find an easy way to find out how many rows are in this
excel sheet, I told the script to stop after -maxemptyrows empty lines
in a sequence.
Only the reading part is coded. A way to modify the Excel sheet when you
modify @F needs to be written.
=head1 Params:
-file String: Name of excel file (full path)
-visible Boolean: make excel visible (funny, but not yet useful)
-sheetnumber Integer: which sheet shell I read (1..n, default: 1)
-headline Integer: not yet implemented (line number of headline)
-maxemptyrows Integer: stop after how many empty rows (default: 20)
-ane String: Perl-Code to execute
Abbreviations of params are allowed, as long as they are unique, e.g
xlsPerl.pl -s 2 -m 10 -ane "print qq~$R: $F[0] $F[5]\n" -f c:\test.xls
=head1 Special Variables:
$R Row number
@F Array containing the values of one complete row
=head1 Bugs/...
This code is very, very, very experimental, and some parts are dirty!
=head1 Author
Martin Fabiani L<http://www.fabiani.net/>
=cut
|< 1 2 >| | 16 Einträge, 2 Seiten |