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   17305 use Mojo::Base 'Mojolicious::Plugin';
  2         9286  
  2         16  
4 2     2   2115 use Spreadsheet::WriteExcel::Simple;
  2         152798  
  2         714  
5              
6             our $VERSION = '2.05';
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 61391 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         12 $options->{format} = 'xls';
19              
20 5         45 my $ss = Spreadsheet::WriteExcel::Simple->new;
21 5         42619 my $heading = $c->stash->{heading};
22 5         68 my $result = $c->stash->{result};
23 5         35 my $settings = $c->stash->{settings};
24              
25 5 100       42 if (ref $heading) {
26 1         4 $ss->write_bold_row($heading);
27             }
28              
29 5 100       439 if (ref $settings) {
30 2 100       23 $c->reply->exception("invalid column width")
31             unless defined $settings->{column_width};
32 2         53916 for my $col (keys %{$settings->{column_width}}) {
  2         8  
33 3         147 $ss->sheet->set_column($col, $settings->{column_width}->{$col});
34             }
35             }
36              
37 5         64 foreach my $data (@$result) {
38 9         1376 $ss->write_row($data);
39             }
40              
41 5         580 $$output = $ss->data;
42              
43 5         30130 return 1;
44             }
45              
46             sub register {
47 1     1 1 49 my ($self, $app) = @_;
48              
49 1         25 $app->types->type(xls => 'application/vnd.ms-excel');
50 1         111 $app->renderer->add_handler(xls => \&xls_renderer);
51             $app->helper(
52             render_xls => sub {
53 1     1   19062 shift->render(handler => 'xls', @_);
54             }
55 1         47 );
56             }
57              
58             1;
59             __END__