File Coverage

blib/lib/Mojolicious/Plugin/WriteExcel.pm
Criterion Covered Total %
statement 29 29 100.0
branch 6 6 100.0
condition n/a
subroutine 5 5 100.0
pod 2 2 100.0
total 42 42 100.0


line stmt bran cond sub pod time code
1             package Mojolicious::Plugin::WriteExcel;
2              
3 2     2   64556 use Mojo::Base 'Mojolicious::Plugin';
  2         190234  
  2         17  
4 2     2   2307 use Spreadsheet::WriteExcel::Simple;
  2         143480  
  2         802  
5              
6             our $VERSION = '3.00';
7              
8             # You just have to give guys a chance. Sometimes you meet a guy and
9             # think he's a pig, but then later on you realize he actually has a
10             # really good body.
11             sub xls_renderer {
12 5     5 1 45668 my ($r, $c, $output, $options) = @_;
13              
14             # don't let MojoX::Renderer to encode output to string
15 5         12 delete $options->{encoding};
16              
17             # tell the renderer we're not html
18 5         8 $options->{format} = 'xls';
19              
20 5         30 my $ss = Spreadsheet::WriteExcel::Simple->new;
21 5         14090 my $heading = $c->stash->{heading};
22 5         51 my $result = $c->stash->{result};
23 5         32 my $settings = $c->stash->{settings};
24              
25 5 100       36 if (ref $heading) {
26 1         4 $ss->write_bold_row($heading);
27             }
28              
29 5 100       429 if (ref $settings) {
30             die "invalid column width"
31 2 100       15 unless defined $settings->{column_width};
32 1         2 for my $col (keys %{$settings->{column_width}}) {
  1         5  
33 3         190 $ss->sheet->set_column($col, $settings->{column_width}->{$col});
34             }
35             }
36              
37 4         83 foreach my $data (@$result) {
38 9         1786 $ss->write_row($data);
39             }
40              
41 4         807 $$output = $ss->data;
42              
43 4         32123 return 1;
44             }
45              
46             sub register {
47 1     1 1 52 my ($self, $app) = @_;
48              
49 1         8 $app->types->type(xls => 'application/vnd.ms-excel');
50 1         70 $app->renderer->add_handler(xls => \&xls_renderer);
51             $app->helper(
52             render_xls => sub {
53 1     1   62720 shift->render(handler => 'xls', @_);
54             }
55 1         26 );
56             }
57              
58             1;
59             __END__