r1334@llin: dpavlin | 2007-10-08 00:51:58 +0200
[webpac2] / lib / WebPAC / Input / Excel.pm
index 21679f4..ee3c26a 100644 (file)
@@ -3,21 +3,21 @@ package WebPAC::Input::Excel;
 use warnings;
 use strict;
 
-use WebPAC::Input;
 use Spreadsheet::ParseExcel;
 use Spreadsheet::ParseExcel::Utility qw/int2col/;
+use base qw/WebPAC::Common/;
 
 =head1 NAME
 
-WebPAC::Input::Excel - support for Microsoft Excell and compatibile files
+WebPAC::Input::Excel - support for Microsoft Excel and compatibile files
 
 =head1 VERSION
 
-Version 0.02
+Version 0.04
 
 =cut
 
-our $VERSION = '0.02';
+our $VERSION = '0.04';
 
 
 =head1 SYNOPSIS
@@ -25,16 +25,13 @@ our $VERSION = '0.02';
 Open Microsoft Excell, or compatibile format (for e.g. from OpenOffice.org
 or Gnuemeric) in C<.xls> format.
 
- my $isis = new WebPAC::Input::Excel();
- $isis->open( path => '/path/to/workbook.xls' );
-
 =head1 FUNCTIONS
 
-=head2 open_db
+=head2 new
 
 Returns handle to database and size
 
-  my ($db,$size) = $open_db(
+  my $excel = new WebPAC::Input::Excel(
        path => '/path/to/workbook.xls'
        worksheet => 'name of sheet',
        from => 42,
@@ -48,27 +45,25 @@ C<from> and C<to> specify row numbers to start and finish import.
 
 =cut
 
-my $sheet;
-my ($from,$to);
-
-sub open_db {
-       my $self = shift;
-
-       my $arg = {@_};
+sub new {
+       my $class = shift;
+       my $self = {@_};
+       bless($self, $class);
 
        my $log = $self->_get_logger();
 
-       $log->logdie("can't open excel file $arg->{path}: $!") unless (-r $arg->{path} && -f $arg->{path});
+       $log->logdie("can't open excel file $self->{path}: $!") unless (-r $self->{path} && -f $self->{path});
 
-       my $workbook = Spreadsheet::ParseExcel::Workbook->Parse($arg->{path});
+       my $workbook = Spreadsheet::ParseExcel::Workbook->Parse($self->{path});
 
-       my ($size, $wanted_worksheet);
+       my $sheet;
+       my $wanted_worksheet;
 
-       if ($wanted_worksheet = $arg->{worksheet}) {
+       if ($wanted_worksheet = $self->{worksheet}) {
                my $name;
                do {
                        $sheet = shift @{ $workbook->{Worksheet} };
-                       $log->logdie("can't find sheet '$wanted_worksheet' in $arg->{path}\n") unless (defined($sheet));
+                       $log->logdie("can't find sheet '$wanted_worksheet' in $self->{path}\n") unless (defined($sheet));
                        $name = $sheet->{Name};
                        $name =~ s/\s\s+/ /g;
                } until ($name =~ m/^\s*\Q$wanted_worksheet\E\s*$/i);
@@ -79,17 +74,20 @@ sub open_db {
        
        }
 
-       $from = $arg->{from} || $sheet->{MinRow};
-       $to = $arg->{to} || $sheet->{MaxRow};
+       $self->{sheet} = $sheet;
+
+       $self->{from} ||= $sheet->{MinRow};
+       $self->{to} ||= $sheet->{MaxRow};
 
-       $size = $to - $from;
+       my $size = $self->{to} - $self->{from};
+       $self->{size} = $size;
 
-       $log->warn("opening Excel file '$arg->{path}', using ",
+       $log->warn("opening Excel file '$self->{path}', using ",
                $wanted_worksheet ? '' : 'first ',
                "worksheet: $sheet->{Name} [$size rows]"
        );
 
-       return (42, $size);
+       $self ? return $self : return undef;
 }
 
 =head2 fetch_rec
@@ -109,12 +107,13 @@ sub fetch_rec {
 
        my $log = $self->_get_logger();
 
+       my $sheet = $self->{sheet};
        $log->logdie("can't find sheet hash") unless (defined($sheet));
        $log->logdie("sheet hash isn't Spreadsheet::ParseExcel::Worksheet") unless ($sheet->isa('Spreadsheet::ParseExcel::Worksheet'));
 
        my $rec;
 
-       my $row = $from + $mfn - 1;
+       my $row = $self->{from} + $mfn - 1;
 
        $log->debug("fetch_rec( $mfn ) row: $row cols: ",$sheet->{MinCol}," - ",$sheet->{MaxCol});
 
@@ -130,6 +129,18 @@ sub fetch_rec {
        return $rec;
 }
 
+=head2 size
+
+Return number of records in database
+
+  my $size = $isis->size;
+
+=cut
+
+sub size {
+       my $self = shift;
+       return $self->{size};
+}
 =head1 AUTHOR
 
 Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>