[Bio] / FigKernelPackages / raelib.pm Repository:
ViewVC logotype

Diff of /FigKernelPackages/raelib.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.37, Mon Jun 19 04:05:21 2006 UTC revision 1.38, Mon Jun 19 21:19:22 2006 UTC
# Line 38  Line 38 
38  # we don't know whether the Spreadsheet::WriteExcel methods are available on all systems, and even on the CI systems they are currently in my shared directory  # we don't know whether the Spreadsheet::WriteExcel methods are available on all systems, and even on the CI systems they are currently in my shared directory
39  # so we use an eval and set the boolean if we are cool.  # so we use an eval and set the boolean if we are cool.
40  my $useexcel;  my $useexcel;
41    my $excelfile;
42    my $excelfilelink="";
43  BEGIN {  BEGIN {
44      use lib '/home/seed/Rob/perl/lib/perl5/site_perl/5.8.5/';      use lib '/home/seed/Rob/perl/lib/perl5/site_perl/5.8.5/';
45      eval "use Spreadsheet::WriteExcel";      eval "use Spreadsheet::WriteExcel";
46      unless ($@) {$useexcel=1}      unless ($@) {$useexcel=1}
47  }  }
48    
49    END {
50        my $self=shift;
51        if ($useexcel && $excelfile) {&close_excel_file($excelfile)}
52    }
53    
54    
55  use FIG;  use FIG;
56  my $fig=new FIG;  my $fig=new FIG;
# Line 854  Line 861 
861    
862  This is experimental as of May, 2006.  This is experimental as of May, 2006.
863    
 +++++ PLEASE NOTE WELL ++++  
 +  
 + Make the HTML table before you use this method.If you use the tab2excel feature you are fine,  
 + but if you use the methods separately you MUST make the HTML table first, becuase this will screw with your code :)  
 +  
 ++++++++++++++++++++++++++++  
   
   
   
864  There are a couple of perl modules that allow you to write to excel files, and so I am trying out the idea of taking our standard $tab table respresentation that is used in HTML.pm and making an excel file that people could download. It seems like that would be a great tool for them to have.  There are a couple of perl modules that allow you to write to excel files, and so I am trying out the idea of taking our standard $tab table respresentation that is used in HTML.pm and making an excel file that people could download. It seems like that would be a great tool for them to have.
865    
866  At the moment the excel modules are in my shared space on the CI machines, and so won't work in every seed installation. Therefore the $self->{'useexcel'} boolean is set at compile time if we successfully load the module.  At the moment the excel modules are in my shared space on the CI machines, and so won't work in every seed installation. Therefore the $self->{'useexcel'} boolean is set at compile time if we successfully load the module.
# Line 878  Line 876 
876    
877    
878  Usage:  Usage:
879    
880        The recommended way of using this set of modules is to add the options excelfile=>"filename" to the options passed to &HTML::make_table. That should take care of EVERYTHING for you, so you should do that. You can call thinks separetly if you like, but I don't recommend it.
881    
882        Note that you can make multiple calls to the same excel file,a nd each one will get added as a new sheet.
883    
884      Note the usage is ALMOST the same as make_table, but not quite. First, options is a reference to a hash rather than the hash itself      Note the usage is ALMOST the same as make_table, but not quite. First, options is a reference to a hash rather than the hash itself
885      and second, the additional option "filename" that is the filename to be written;      and second, the additional option "filename" that is the filename to be written;
886    
# Line 897  Line 900 
900          this loads all the data into the excel file, but if you know what you are doing you can call this many times,          this loads all the data into the excel file, but if you know what you are doing you can call this many times,
901          each with a different spreadsheet          each with a different spreadsheet
902      4. close_excel_file      4. close_excel_file
903          this closes the file and writes it. It is what returns the link.          this closes the file and writes it. This is called in the END block, so you do not have to explicitly call it here.
904    
905      tab2excel is a wrapper for all three so that the method in HTML::make_table is really easy.      tab2excel is a wrapper for all three so that the method in HTML::make_table is really easy.
906      See subsys.cgi for a more complex involvement of this!      See subsys.cgi for a more complex involvement of this!
# Line 908  Line 911 
911  sub tab2excel {  sub tab2excel {
912      my($self, $col_hdrs, $tab, $title, $options, $filename)=@_;      my($self, $col_hdrs, $tab, $title, $options, $filename)=@_;
913      return "<p>Couldn't load Spreadsheet::WriteExcel</p>\n" unless ($self->{'useexcel'});      return "<p>Couldn't load Spreadsheet::WriteExcel</p>\n" unless ($self->{'useexcel'});
914      $self->make_excel_workbook($filename, $options);      $self->{'excel_file_link'} = $self->make_excel_workbook($filename, $options);
915        $excelfilelink=$self->{'excel_file_link'};
916      $self->make_excel_worksheet($col_hdrs, $tab, $title);      $self->make_excel_worksheet($col_hdrs, $tab, $title);
917      return $self->close_excel_file();      return "" if ($options->{'no_excel_link'});
918        return $self->{'excel_file_link'};
919  }  }
920    
921    
922    =head1 excel_file_link
923    
924    Just returns the link to the file, if one has been created. If not, returns a non-breaking space (&nbsp;)
925    
926    =cut
927    
928    sub excel_file_link {
929        my $self=shift;
930        # I am not sure why, but this is not working. Perhaps because I am calling it from &HTML::make_table (i.e. not OO perl?)
931        #print STDERR "SELF: $self LINK: ",$self->{'excel_file_link'}," or \n$excelfilelink\n";
932        #return $self->{'excel_file_link'};
933        return $excelfilelink;
934    }
935    
936    
937    
938  =head1 make_excel_workbook  =head1 make_excel_workbook
939    
940  This is the method that actually makes individual workbook. You should call this once, with the name of the file that you want it to be known by. The options are to set borders and whatnot.  This is the method that actually makes individual workbook. You should call this once, with the name of the file that you want it to be known by. The options are to set borders and whatnot.
941    
942    This will return the link to the workbook
943    
944  =cut  =cut
945    
946  sub make_excel_workbook {  sub make_excel_workbook {
# Line 926  Line 950 
950      $filename =~ s/^.*\///; # remove any path information. We are going to only write to FIG_Config::temp      $filename =~ s/^.*\///; # remove any path information. We are going to only write to FIG_Config::temp
951      unless ($filename =~ /\.xls$/) {$filename .=".xls"}      unless ($filename =~ /\.xls$/) {$filename .=".xls"}
952    
953        # now generate the link to return
954        my $link="<p><a href=\"".$fig->temp_url."/".$filename.'">'.$filename."</a> [Download table in Excel format].</p>\n";
955      # do we already have this file -- if so, just return that info      # do we already have this file -- if so, just return that info
956      return  1 if ($self->{'excel_short_filename'} eq $filename); # don't do anything, just return the fact that we have the book made!      return  $link if ($self->{'excel_short_filename'} eq $filename); # don't do anything, just return the fact that we have the book made!
957    
958    
959      $self->{'excel_short_filename'}=$filename;      $self->{'excel_short_filename'}=$filename;
# Line 940  Line 966 
966    
967      # instantiate the workbook      # instantiate the workbook
968      $self->{'excel_workbook'}=Spreadsheet::WriteExcel->new($self->{'excel_filename'});      $self->{'excel_workbook'}=Spreadsheet::WriteExcel->new($self->{'excel_filename'});
969        $excelfile=$self->{'excel_workbook'}; # this is for the close on END
970      $self->{'excel_workbook'}->set_tempdir($FIG_Config::temp); # you don't have to do this, but it may speed things up and reduce memory load.      $self->{'excel_workbook'}->set_tempdir($FIG_Config::temp); # you don't have to do this, but it may speed things up and reduce memory load.
971    
972      # define the default formats      # define the default formats
973      my $border = defined $options->{border} ? $options->{border} : 0;      my $border = defined $options->{border} ? $options->{border} : 0;
974      $self->{'excel_format'}->{default}=$self->{'excel_workbook'}->add_format(border=>$border, size=>10);      $self->{'excel_format'}->{default}=$self->{'excel_workbook'}->add_format(border=>$border, size=>10);
975        return $link;
976  }  }
977    
978    
# Line 1025  Line 1053 
1053  =cut  =cut
1054    
1055  sub close_excel_file{  sub close_excel_file{
1056      my ($self)=@_;      my ($workbook)=@_;
1057        return unless (defined $workbook);
1058      # close the workbook. this writes the files      # close the workbook. this writes the files
1059      $self->{'excel_workbook'}->close();      return $workbook->close();
   
     # now generate the link to return  
     my $size=(stat($self->{'excel_filename'}))[7];  
     $size=int($size/1000);  
     my $link="<p><a href=\"".$fig->temp_url."/".$self->{'excel_short_filename'}.'">'.  
             $self->{'excel_short_filename'}."</a> [Download table in Excel format. $size kb]</p>\n";  
     return $link;  
   
1060  }  }
1061    
1062    
# Line 1085  Line 1105 
1105      if ($tag =~ /background-color\=\"(.*?)\"/i || $tag =~ /bgcolor\=\"(.*?)\"/i)      if ($tag =~ /background-color\=\"(.*?)\"/i || $tag =~ /bgcolor\=\"(.*?)\"/i)
1106      {      {
1107          my $color=$1;          my $color=$1;
1108            if ($color)
1109            {
1110          if (!defined $self->{'excel_color'}->{$color})          if (!defined $self->{'excel_color'}->{$color})
1111          {          {
1112              # find out the last custom color used and increment it              # find out the last custom color used and increment it
# Line 1095  Line 1117 
1117          }          }
1118          $bgcolor=$self->{'excel_color'}->{$color};          $bgcolor=$self->{'excel_color'}->{$color};
1119      }      }
1120        }
1121      elsif ($tag =~ /color\=\"(.*?)\"/i || $tag =~ /color\=\'(.*?)\'/i)      elsif ($tag =~ /color\=\"(.*?)\"/i || $tag =~ /color\=\'(.*?)\'/i)
1122      {      {
1123          my $color=$1;          my $color=$1;

Legend:
Removed from v.1.37  
changed lines
  Added in v.1.38

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3