File Coverage

blib/lib/MojoX/Renderer/WriteExcel.pm
Criterion Covered Total %
statement 9 28 32.1
branch 0 6 0.0
condition n/a
subroutine 3 5 60.0
pod 1 1 100.0
total 13 40 32.5


line stmt bran cond sub pod time code
1             package MojoX::Renderer::WriteExcel;
2              
3 1     1   45653 use warnings;
  1         3  
  1         41  
4 1     1   6 use strict;
  1         2  
  1         40  
5              
6 1     1   4170 use Spreadsheet::WriteExcel::Simple;
  1         153577  
  1         300  
7              
8             our $VERSION = '1.0';
9              
10             # Fry: Why would a robot need to drink?
11             # Bender: I don't need to drink. I can quit anytime I want!
12             sub new {
13 0     0 1   shift; # ignore
14              
15             return sub {
16 0     0     my ($r, $c, $output, $options) = @_;
17              
18             # don't let MojoX::Renderer to encode output to string
19 0           delete $options->{encoding};
20              
21 0           my $ss = Spreadsheet::WriteExcel::Simple->new;
22 0           my $heading = $c->stash->{heading};
23 0           my $result = $c->stash->{result};
24 0           my $settings = $c->stash->{settings};
25              
26 0 0         if (ref $heading) {
27 0           $ss->write_bold_row($heading);
28             }
29              
30 0 0         if (ref $settings) {
31 0 0         $c->render_exception("invalid column width")
32             unless defined $settings->{column_width};
33 0           for my $col (keys %{$settings->{column_width}}) {
  0            
34 0           $ss->sheet->set_column($col, $settings->{column_width}->{$col});
35             }
36             }
37              
38 0           foreach my $data (@$result) {
39 0           $ss->write_row($data);
40             }
41              
42 0           $$output = $ss->data;
43              
44 0           return 1;
45 0           };
46             }
47              
48             =head1 NAME
49              
50             MojoX::Renderer::WriteExcel - emit Excel spreadsheets from Mojo
51              
52             =head1 SYNOPSIS
53              
54             use MojoX::Renderer::WriteExcel;
55              
56             sub startup {
57             my $self = shift;
58              
59             $self->types->type(xls => 'application/vnd.ms-excel');
60              
61             my $self->renderer->add_handler(
62             xls => MojoX::Renderer::WriteExcel->new
63             );
64             }
65              
66             =head1 DESCRIPTION
67              
68             This renderer converts the C element in the stash to an Excel
69             spreadsheet. If the stash also has a C element, the renderer
70             will also write headings in bold type for the columns in the
71             spreadsheet.
72              
73             C is an arrayref, while C is an array of arrayrefs.
74              
75             Optionally, a C parameter can be provided to set additional
76             attributes in the Excel spreadsheet. Currently 'column_width' is the
77             only working attribute. C is a hashref. Column widths
78             could be set by passing the settings to render such as:
79              
80             settings => {column_width => {'A:A' => 10, 'B:B' => 25, 'C:D' => 40}}
81              
82             =head1 METHODS
83              
84             =head2 new
85              
86             This method returns a handler for the Mojo renderer.
87              
88             =cut
89              
90             =head1 AUTHOR
91              
92             Zak B. Elep
93              
94             =head1 BUGS
95              
96             Please report any bugs or feature requests to
97             C, or through the web
98             interface at
99             L.
100             I will be notified, and then you'll automatically be notified of
101             progress on your bug as I make changes.
102              
103              
104              
105              
106             =head1 SUPPORT
107              
108             You can find documentation for this module with the perldoc command.
109              
110             perldoc MojoX::Renderer::WriteExcel
111              
112              
113             You can also look for information at:
114              
115             =over 4
116              
117             =item * RT: CPAN's request tracker
118              
119             L
120              
121             =item * AnnoCPAN: Annotated CPAN documentation
122              
123             L
124              
125             =item * CPAN Ratings
126              
127             L
128              
129             =item * Search CPAN
130              
131             L
132              
133             =back
134              
135              
136             =head1 ACKNOWLEDGEMENTS
137              
138             Thanks to Graham Barr and his L module, and
139             Sebastian Riedel's core L for showing
140             how to write renderers for Mojo!
141              
142             Inspiration for this renderer came from this mailing list thread:
143             L
144              
145             =head1 LICENSE AND COPYRIGHT
146              
147             Copyright 2010 Zak B. Elep.
148              
149             This program is free software; you can redistribute it and/or modify it
150             under the terms of either: the GNU General Public License as published
151             by the Free Software Foundation; or the Artistic License.
152              
153             See http://dev.perl.org/licenses/ for more information.
154              
155              
156             =cut
157              
158             1; # End of MojoX::Renderer::WriteExcel