[Catalyst-commits] r8615 - in
Catalyst-Action-Serialize-SimpleExcel/1.000/trunk: .
lib/Catalyst/Action/Serialize t t/lib/TestApp/Controller
caelum at dev.catalyst.perl.org
caelum at dev.catalyst.perl.org
Mon Nov 17 23:27:18 GMT 2008
Author: caelum
Date: 2008-11-17 23:27:17 +0000 (Mon, 17 Nov 2008)
New Revision: 8615
Modified:
Catalyst-Action-Serialize-SimpleExcel/1.000/trunk/Changes
Catalyst-Action-Serialize-SimpleExcel/1.000/trunk/META.yml
Catalyst-Action-Serialize-SimpleExcel/1.000/trunk/lib/Catalyst/Action/Serialize/SimpleExcel.pm
Catalyst-Action-Serialize-SimpleExcel/1.000/trunk/t/excel.t
Catalyst-Action-Serialize-SimpleExcel/1.000/trunk/t/lib/TestApp/Controller/REST.pm
Log:
C::A::S::SimpleExcel: automatic column widths
Modified: Catalyst-Action-Serialize-SimpleExcel/1.000/trunk/Changes
===================================================================
--- Catalyst-Action-Serialize-SimpleExcel/1.000/trunk/Changes 2008-11-17 18:29:24 UTC (rev 8614)
+++ Catalyst-Action-Serialize-SimpleExcel/1.000/trunk/Changes 2008-11-17 23:27:17 UTC (rev 8615)
@@ -1,5 +1,8 @@
Revision history for Catalyst-Action-Serialize-SimpleExcel
+0.012 2008-11-17 15:24:22 PDT
+ Added automatic column widths support.
+
0.011 2008-11-12 13:17:16 PDT
First CPAN release, fixed Content-Type and used HashRefInflator in example
as jshirley suggested.
Modified: Catalyst-Action-Serialize-SimpleExcel/1.000/trunk/META.yml
===================================================================
--- Catalyst-Action-Serialize-SimpleExcel/1.000/trunk/META.yml 2008-11-17 18:29:24 UTC (rev 8614)
+++ Catalyst-Action-Serialize-SimpleExcel/1.000/trunk/META.yml 2008-11-17 23:27:17 UTC (rev 8615)
@@ -25,4 +25,4 @@
parent: 0
resources:
license: http://dev.perl.org/licenses/
-version: 0.011
+version: 0.012
Modified: Catalyst-Action-Serialize-SimpleExcel/1.000/trunk/lib/Catalyst/Action/Serialize/SimpleExcel.pm
===================================================================
--- Catalyst-Action-Serialize-SimpleExcel/1.000/trunk/lib/Catalyst/Action/Serialize/SimpleExcel.pm 2008-11-17 18:29:24 UTC (rev 8614)
+++ Catalyst-Action-Serialize-SimpleExcel/1.000/trunk/lib/Catalyst/Action/Serialize/SimpleExcel.pm 2008-11-17 23:27:17 UTC (rev 8615)
@@ -14,11 +14,11 @@
=head1 VERSION
-Version 0.011
+Version 0.012
=cut
-our $VERSION = '0.011';
+our $VERSION = '0.012';
=head1 SYNOPSIS
@@ -52,7 +52,6 @@
my $entity = {
header => ['Author', 'Title'], # will be bold
- column_widths => [30, 50], # in characters
rows => \@t,
# the part before .xls, which is automatically appended
filename => 'myapp-books-'.strftime('%m-%d-%Y', localtime)
@@ -94,7 +93,8 @@
=head2 column_widths
-Optional, the widths in characters of the columns.
+Optional, the widths in characters of the columns. Otherwise the widths are
+calculated automatically from the data and header.
=head2 filename
@@ -122,22 +122,16 @@
my ($row, $col) = (0,0);
-# Set column widths
- if (exists $data->{column_widths}) {
- for my $width (@{ $data->{column_widths} }) {
- $worksheet->set_column($col, $col++, $width);
- }
-# Have to set the width of column 0 again, otherwise Excel loses it!
-# I don't know why...
- $worksheet->set_column(0, 0, $data->{column_widths}[0]);
- $col = 0;
- }
+ my @auto_widths;
# Write Header
if (exists $data->{header}) {
my $header_format = $workbook->add_format;
$header_format->set_bold;
for my $header (@{ $data->{header} }) {
+ $auto_widths[$col] = length $header
+ if $auto_widths[$col] < length $header;
+
$worksheet->write($row, $col++, $header, $header_format);
}
$row++;
@@ -147,12 +141,27 @@
# Write data
for my $the_row (@{ $data->{rows} }) {
for my $the_col (@$the_row) {
+ $auto_widths[$col] = length $the_col
+ if $auto_widths[$col] < length $the_col;
+
$worksheet->write($row, $col++, $the_col);
}
$row++;
$col = 0;
}
+# Set column widths
+ $data->{column_widths} = \@auto_widths
+ unless exists $data->{column_widths};
+
+ for my $width (@{ $data->{column_widths} }) {
+ $worksheet->set_column($col, $col++, $width);
+ }
+# Have to set the width of column 0 again, otherwise Excel loses it!
+# I don't know why...
+ $worksheet->set_column(0, 0, $data->{column_widths}[0]);
+ $col = 0;
+
# Write the file
my $filename = $data->{filename} || 'data';
@@ -189,8 +198,6 @@
=item * Multiple sheet support.
-=item * Autofit support (would require a macro.)
-
=back
=head1 SUPPORT
Modified: Catalyst-Action-Serialize-SimpleExcel/1.000/trunk/t/excel.t
===================================================================
--- Catalyst-Action-Serialize-SimpleExcel/1.000/trunk/t/excel.t 2008-11-17 18:29:24 UTC (rev 8614)
+++ Catalyst-Action-Serialize-SimpleExcel/1.000/trunk/t/excel.t 2008-11-17 23:27:17 UTC (rev 8615)
@@ -7,9 +7,11 @@
use Catalyst::Test 'TestApp';
use Spreadsheet::ParseExcel ();
-use Test::More tests => 10;
+use Test::More tests => 13;
use Test::Deep;
+# Test array of array
+
ok((my $file = get '/rest/a_o_a?content-type=application%2Fvnd.ms-excel'),
'received file');
ok((my $excel = Spreadsheet::ParseExcel::Workbook->Parse(\$file)),
@@ -22,6 +24,24 @@
'array_of_array -> sheet'
);
+# Test automatic column widths
+
+ok(($file = get '/rest/auto_widths?content-type=application%2Fvnd.ms-excel'),
+ 'received file');
+ok(($excel = Spreadsheet::ParseExcel::Workbook->Parse(\$file)),
+ 'parsed file');
+$sheet = $excel->{Worksheet}[0];
+
+cmp_deeply(
+# internal representation of column widths is as floats
+# and for some reason there's a width set on the column beyond the last
+ [ map int, @{ $sheet->{ColWidth} }[0..1] ],
+ [ 3, 6 ],
+ 'column_widths'
+);
+
+# Test everything else
+
ok((my $resp = request '/rest/fancy?content-type=application%2Fvnd.ms-excel'),
'received response');
@@ -36,8 +56,6 @@
$sheet = $excel->{Worksheet}[0];
cmp_deeply(
-# internal representation of column widths is as floats
-# and for some reason there's a width set on the column beyond the last
[ map int, @{ $sheet->{ColWidth} }[0..1] ],
[ 10, 20 ],
'column_widths'
Modified: Catalyst-Action-Serialize-SimpleExcel/1.000/trunk/t/lib/TestApp/Controller/REST.pm
===================================================================
--- Catalyst-Action-Serialize-SimpleExcel/1.000/trunk/t/lib/TestApp/Controller/REST.pm 2008-11-17 18:29:24 UTC (rev 8614)
+++ Catalyst-Action-Serialize-SimpleExcel/1.000/trunk/t/lib/TestApp/Controller/REST.pm 2008-11-17 23:27:17 UTC (rev 8615)
@@ -39,4 +39,22 @@
);
}
+sub auto_widths : Local ActionClass('REST') {}
+
+sub auto_widths_GET {
+ my ($self, $c) = @_;
+
+ $self->status_ok(
+ $c,
+ entity => {
+ header => [qw/Foo Bar/],
+ rows => [
+ [1,2],
+ [3,999999]
+ ],
+ filename => 'mtfnpy'
+ }
+ );
+}
+
1;
More information about the Catalyst-commits
mailing list