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   18250 use Mojo::Base 'Mojolicious::Plugin';
  2         7307  
  2         12  
4 2     2   1900 use Spreadsheet::WriteExcel::Simple;
  2         136978  
  2         748  
5              
6             our $VERSION = '2.04';
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 86907 my ($r, $c, $output, $options) = @_;
13              
14             # don't let MojoX::Renderer to encode output to string
15 5         16 delete $options->{encoding};
16              
17             # tell the renderer we're not html
18 5         10 $options->{format} = 'xls';
19              
20 5         50 my $ss = Spreadsheet::WriteExcel::Simple->new;
21 5         47088 my $heading = $c->stash->{heading};
22 5         76 my $result = $c->stash->{result};
23 5         39 my $settings = $c->stash->{settings};
24              
25 5 100       46 if (ref $heading) {
26 1         5 $ss->write_bold_row($heading);
27             }
28              
29 5 100       417 if (ref $settings) {
30 2 100       15 $c->render_exception("invalid column width")
31             unless defined $settings->{column_width};
32 2         61227 for my $col (keys %{$settings->{column_width}}) {
  2         13  
33 3         148 $ss->sheet->set_column($col, $settings->{column_width}->{$col});
34             }
35             }
36              
37 5         60 foreach my $data (@$result) {
38 9         1589 $ss->write_row($data);
39             }
40              
41 5         683 $$output = $ss->data;
42              
43 5         31020 return 1;
44             }
45              
46             sub register {
47 1     1 1 52 my ($self, $app) = @_;
48              
49 1         25 $app->types->type(xls => 'application/vnd.ms-excel');
50 1         113 $app->renderer->add_handler(xls => \&xls_renderer);
51             $app->helper(
52             render_xls => sub {
53 1     1   19180 shift->render(handler => 'xls', @_);
54             }
55 1         52 );
56             }
57              
58             1;
59             __END__