Apache-FastForward

 view release on metacpan or  search on metacpan

lib/Apache/FastForward/Spreadsheet.pm  view on Meta::CPAN

 
use vars qw( $VERSION );
$VERSION = '1.01'; 

# Perl tie methods: DELETE, CLEAR, EXISTS, FIRSTKEY, NEXTKEY, 
# SCALAR, UNTIE, DESTROY  are not implemented as the package 
# mimics a spreadsheet array of array behaviour

sub TIEHASH {

    my $class = shift;
    my $self = {};
    $self->{VERSION} = $VERSION;
    $self->{template} = []; 
    bless ( $self, $class );
    return $self 
}

sub LoadTemplate {
  
#Contract: 
#   [1] Path to a CSV file and CSV parameters as input
#   [2] Method can either suceed (true) or everything dies...

    my $self  = shift;
    my $path = shift;
    my $csv_atrributes = shift;

    defined( $csv_atrributes ) or 
     croak 'Missing CSV parameters for spreadsheet template';
    my $csv = Text::CSV_XS->new( $csv_atrributes );
        
    open( TEMPLATE, '< '.$path ) or 
     croak "Spreadsheet template $path -> $!";
    while ( defined( my $line = <TEMPLATE> ) ){
        $csv->parse( $line ) or
         croak "Spreadsheet template $path -> The file cannot be parsed";
        push( @{ $self->{template} }, [ $csv->fields() ] );
    }
    close( TEMPLATE )    
}

sub FETCH {

#Contract: 
#   [1] ('A12') spreadsheet address or row & column number eg ('12,1') as input
#   [2] Cell value or undef if adresse does not exist

   my $self = shift;
   my $cell_address = shift;
   
   my $coordinate = convert_spreadsheet_address( $cell_address );
   
   return $self->{template}->[$coordinate->{x}][$coordinate->{y}]   
}

sub STORE {

#Contract: 
#   [1] ('A12') spreadsheet address or row & column number eg ('12,1') as input
#   [2] Method can only suceed as array can always be expanded...

   my $self = shift;
   my $cell_address = shift;
   my $cell_value = shift;
   
   my $coordinate = convert_spreadsheet_address( $cell_address );
   
   $self->{template}->[$coordinate->{x}][$coordinate->{y}] = $cell_value
}


sub DumpAsCSV {

#Contract: 
#	[1] CSV file parameters as input
#   [2] Method can return result string or everything dies...

    my $self  = shift;
    my $csv_atrributes = shift;

    defined( $csv_atrributes ) or 
     croak 'Missing CSV parameters for spreadsheet template';
    my $csv = Text::CSV_XS->new( $csv_atrributes );
    
    my $csv_string = '';
    foreach my $row ( @{$self->{template}} ){
        if ( $csv->combine( @$row ) ){
            $csv_string .= $csv->string()."\n";
        }
        else{
            croak 'The spreadsheet cannot be dumped for value '.$csv->error_input();    
        }
    }
    return $csv_string
}

sub ShiftRow {

#Contract: 
#   [1] No input
#   [2] Similar to shift @ARRAY
#   [3] Returns shifted row

    my $self = shift;
    my $row_number = shift;
    
    return @{ shift( @{$self->{template}} ) }    
}

sub PopRow {
  
#Contract: 
#   [1] No input
#   [2] Similar to pop @ARRAY
#   [3] Returns poped row

    my $self = shift;
    my $row_number = shift;
    
    return @{ pop( @{$self->{template}} ) }  



( run in 1.443 second using v1.01-cache-2.11-cpan-97f6503c9c8 )