axelrose has asked for the wisdom of the Perl Monks concerning the following question:
To check you need to open the resulting tmpfile. I'm testing with MacPerl5.6.1 btw.#!perl -w use strict; my $tmpl = ExcelWriter->new(); my @row = ( 1, 2, 3 ); $tmpl->writerow( \@row ); BEGIN { package ExcelWriter; use Spreadsheet::WriteExcel; my $tmpfile = sprintf( "/tmp/%d.%d.xls", time % 1000, int rand(1000) ); sub new { my $class = shift; my $workbook = Spreadsheet::WriteExcel->new($tmpfile) or die $!; my $worksheet = $workbook->addworksheet('Data'); bless \$worksheet, $class; } sub writerow { my ( $self, $row ) = @_; my $worksheet = $$self; for ( 0 .. @$row - 1 ) { $worksheet->write( 0, $_, $row->[$_] ); } } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: writing with WriteExcel in OO style
by jmcnamara (Monsignor) on Mar 25, 2002 at 23:46 UTC | |
When you encapsulate Spreadsheet::WriteExcel objects or sub-objects like this you have to explicitly call the workbook close() method to ensure that the sub-objects are destroyed in the correct order. This is due to what I believe is a bug in DESTROY. Also, the functionality that you are implementing via your writerow() method is already available through the Spreadsheet::WriteExcel write_row() or write() methods if you have version 0.34 or later. ;-)
--
| [reply] [d/l] |
by axelrose (Scribe) on Mar 26, 2002 at 18:37 UTC | |
I know about write_row() and write_col() but they don't fit my need. The data from a database comes in rows. I want to write them immediately and need a different format for each column. So I planned to extend my writerow() method to accept not only an arrayref to the data but also a second arrayref for the format of ach column. Do I reinvent the wheel? | [reply] |
by jmcnamara (Monsignor) on Mar 27, 2002 at 09:35 UTC | |
Do I reinvent the wheel? No. :-) The set_column() method should allow you to set the format for a column and then have that applied to all data written to the column. However, it doesn't currently work like that and it should really be fixed. An easy way of adding new methods to the Worksheet class, or to any of the other classes, is to do something like this:
In the example directory of the Spreadsheet::WriteExcel distro the write_many.pl and comments.pl programs use this methodology. However, this is only suitable for simple or one-off solutions. If you need to do anything more complicated or maintainable it would probably be better to subclass the Worksheet class.
--
| [reply] [d/l] |
|
Re: writing with WriteExcel in OO style
by axelrose (Scribe) on Apr 08, 2002 at 22:15 UTC | |
I checked the "writemany.pl" example and found it easier to achieve my goal by writing just a "formatmany" method. Eventually what I really need is formatting in one go with predefined formats. Below is the code I have so far (work in progress!) The problem I have with this - the format doesn't seem to have any effect in the resulting file no matter what style settings I select. Perhaps some monk sees the problem I can't find ... Thanks for your time, Axel. (please change #!perl -l015 for your needs, I'm on MacPerl in the moment, writemany() taken from John's example) | [reply] [d/l] |
by axelrose (Scribe) on Apr 11, 2002 at 21:47 UTC | |
I call the set_column() method now immediately after the write() which sets the format as received in the parameter list. The interface is not very robust yet but the usage seems easy. What do you think?
| [reply] [d/l] |
by jmcnamara (Monsignor) on Apr 12, 2002 at 08:13 UTC | |
That looks good. The next release of Spreadsheet::WriteExcel will correct the deficiency in set_column() that you have to work around here.
--
| [reply] |
by jmcnamara (Monsignor) on Apr 09, 2002 at 07:45 UTC | |
The problem seems to be that you are using set_col() to set the format of cells that you have writing seperately. Unfortunately, in the current implementation of the set_col() method doesn't work like this. This is documented in the set_col() section of the documentation with the promise that it "will be fixed in a future release". It will be fixed but in the meantime the best way to work around this is to store the column formats in a hash where the key is the column number. Then in your write_many() method use the appropriate column format if an explicit cell format isn't specified.
--
| [reply] |
|
Re: writing with WriteExcel in OO style
by axelrose (Scribe) on May 13, 2002 at 21:17 UTC | |
| [reply] [d/l] |