| 1 | # Copyright (C) 2003-2007, G. Allen Morris III, all rights reserved | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | package Data::Tabular::Output::XLS; | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 4 | our @CARP_NOT = qw (Data::Tabular Data::Tabular::Output); | |||||||||||||||||||||||||||
| 8 | 4 | ▶ | use Carp qw (croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 4 | ▶ | use base qw(Data::Tabular::Output); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | sub new | ||||||||||||||||||||||||||||
| 13 | 3 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=5 SCAR=16.6
Pod
documented
| |||||||||||||||||||||||||||||
| 14 | 3 | my $class = shift; | |||||||||||||||||||||||||||
| 15 | 3 | my $args = { @_ }; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 3 | my $self = bless {}, $class; | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 3 | my $arg_list = { | |||||||||||||||||||||||||||
| 20 | table => {required => 1}, | ||||||||||||||||||||||||||||
| 21 | output => {}, | ||||||||||||||||||||||||||||
| 22 | workbook => {required => 1}, | ||||||||||||||||||||||||||||
| 23 | worksheet => {required => 1}, | ||||||||||||||||||||||||||||
| 24 | row_offset => {}, | ||||||||||||||||||||||||||||
| 25 | column_offset => {}, | ||||||||||||||||||||||||||||
| 26 | }; | ||||||||||||||||||||||||||||
| 27 | 3 | for my $arg (keys %$arg_list) { | |||||||||||||||||||||||||||
| 28 | 18 | $self->{$arg} = $args->{$arg}; | |||||||||||||||||||||||||||
| 29 | 18 | ▶ | croak "Need $arg" if $arg_list->{$arg}->{required} && !defined($self->{$arg}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$arg_list->{$arg}{"required"} and not defined $self->{$arg}
MC/DC0%0/2
$arg_list->{$arg}{"required"} and not defined $self->{$arg}
$arg_list->{$arg}{"required"}
not defined $self->{$arg}
Truth table66%2/3
A and not(B)
A= $arg_list->{$arg}{"required"}B= not defined $self->{$arg}
| |||||||||||||||||||||||||||||
| 30 | 18 | delete $args->{$arg}; | |||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 3 | ▶ | if (my @x_list = keys %$args) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | 0 | die 'Unknows arguments: ' . join(' ', sort @x_list); | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 3 | $self->_render; | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 3 | $self; | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | sub workbook | ||||||||||||||||||||||||||||
| 43 | 3 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
workbook: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 44 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 45 | 3 | $self->{workbook}; | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | sub worksheet | ||||||||||||||||||||||||||||
| 49 | 3 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
worksheet: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 50 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 51 | 3 | $self->{worksheet}; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | sub row_offset | ||||||||||||||||||||||||||||
| 55 | 3 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
row_offset: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 56 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 57 | 3 | $self->{row_offset}; | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | sub col_offset | ||||||||||||||||||||||||||||
| 61 | 3 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
col_offset: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 62 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 63 | 3 | $self->{col_offset}; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | sub _get_col_id | ||||||||||||||||||||||||||||
| 67 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_get_col_id: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 68 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 0 | $self->output->col_id(shift); | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | sub _render | ||||||||||||||||||||||||||||
| 74 | 3 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_render: called CC=19 SCAR=32.9
| |||||||||||||||||||||||||||||
| 75 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 3 | ▶ | my $workbook = $self->workbook || croak 'need a workbook'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->workbook || croak('need a workbook')
MC/DC0%0/1
$self->workbook || croak('need a workbook')
$self->workbook
Truth table50%1/2
A
A= $self->workbook
| |||||||||||||||||||||||||||||
| 78 | 3 | ▶ | my $worksheet = $self->worksheet || croak 'need a worksheet'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->worksheet || croak('need a worksheet')
MC/DC0%0/1
$self->worksheet || croak('need a worksheet')
$self->worksheet
Truth table50%1/2
A
A= $self->worksheet
| |||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 3 | my $row_offset = $self->row_offset; | |||||||||||||||||||||||||||
| 81 | 3 | my $col_offset = $self->col_offset; | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 3 | my $pin_title = $self->output->test_xls_attribute('pin_title'); | |||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 3 | my $format_default = $workbook->addformat( | |||||||||||||||||||||||||||
| 86 | align => 'left', | ||||||||||||||||||||||||||||
| 87 | size => 8, | ||||||||||||||||||||||||||||
| 88 | ); | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 3 | my $formats = { | |||||||||||||||||||||||||||
| 91 | 'right' => $workbook->addformat( | ||||||||||||||||||||||||||||
| 92 | align => 'right', | ||||||||||||||||||||||||||||
| 93 | text_wrap => 1, | ||||||||||||||||||||||||||||
| 94 | bold => 1, | ||||||||||||||||||||||||||||
| 95 | ), | ||||||||||||||||||||||||||||
| 96 | 'left' => $workbook->addformat( | ||||||||||||||||||||||||||||
| 97 | align => 'left', | ||||||||||||||||||||||||||||
| 98 | text_wrap => 1, | ||||||||||||||||||||||||||||
| 99 | bold => 1, | ||||||||||||||||||||||||||||
| 100 | ), | ||||||||||||||||||||||||||||
| 101 | }; | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 3 | my $output = $self->output; | |||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 3 | for my $column ($self->columns()) { | |||||||||||||||||||||||||||
| 106 | 32 | my $col = $column->col_id; | |||||||||||||||||||||||||||
| 107 | 32 | ▶ | my $align = $column->align || undef; | ||||||||||||||||||||||||||
|
Condition50%1/2
$column->align || undef
MC/DC0%0/1
$column->align || undef
$column->align
Truth table50%1/2
A
A= $column->align
| |||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 32 | my $type = $self->output->type($column->name); | |||||||||||||||||||||||||||
| 110 | 32 | my $width = undef; | |||||||||||||||||||||||||||
| 111 | 32 | ▶ | if ($type eq 'number') { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 112 | 4 | ▶ | $align ||= 'right'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$align ||= "right"
MC/DC0%0/1
$align ||= "right"
$align
Truth table50%1/2
A
A= $align
| |||||||||||||||||||||||||||||
| 113 | } elsif ($type eq 'time') { | ||||||||||||||||||||||||||||
| 114 | 1 | $width = 22; | |||||||||||||||||||||||||||
| 115 | 1 | ▶ | $align ||= 'right'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$align ||= "right"
MC/DC0%0/1
$align ||= "right"
$align
Truth table50%1/2
A
A= $align
| |||||||||||||||||||||||||||||
| 116 | } elsif ($type eq 'date') { | ||||||||||||||||||||||||||||
| 117 | 0 | $width = 12; | |||||||||||||||||||||||||||
| 118 | 0 | ▶ | $align ||= 'right'; | ||||||||||||||||||||||||||
|
Condition0%0/2
$align ||= "right"
MC/DC0%0/1
$align ||= "right"
$align
Truth table0%0/2
A
A= $align
| |||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 32 | $worksheet->set_column($col, $col, undef, $formats->{$align}); | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 3 | my $types = { | |||||||||||||||||||||||||||
| 125 | default => { | ||||||||||||||||||||||||||||
| 126 | align => 'left', | ||||||||||||||||||||||||||||
| 127 | size => 8, | ||||||||||||||||||||||||||||
| 128 | }, | ||||||||||||||||||||||||||||
| 129 | month => { | ||||||||||||||||||||||||||||
| 130 | num_format => 'mm/yyyy', | ||||||||||||||||||||||||||||
| 131 | align => 'right', | ||||||||||||||||||||||||||||
| 132 | size => 8, | ||||||||||||||||||||||||||||
| 133 | }, | ||||||||||||||||||||||||||||
| 134 | time => { | ||||||||||||||||||||||||||||
| 135 | num_format => 'mm/dd/yyyy hh:mm:ss am/pm', | ||||||||||||||||||||||||||||
| 136 | align => 'right', | ||||||||||||||||||||||||||||
| 137 | size => 8, | ||||||||||||||||||||||||||||
| 138 | }, | ||||||||||||||||||||||||||||
| 139 | date => { | ||||||||||||||||||||||||||||
| 140 | num_format => 'mm/dd/yyyy', | ||||||||||||||||||||||||||||
| 141 | align => 'right', | ||||||||||||||||||||||||||||
| 142 | size => 8, | ||||||||||||||||||||||||||||
| 143 | }, | ||||||||||||||||||||||||||||
| 144 | dollar => { | ||||||||||||||||||||||||||||
| 145 | num_format => '$#,##0.00_);[Red]($#,##0.00)', | ||||||||||||||||||||||||||||
| 146 | align => 'right', | ||||||||||||||||||||||||||||
| 147 | size => 8, | ||||||||||||||||||||||||||||
| 148 | }, | ||||||||||||||||||||||||||||
| 149 | percent => { | ||||||||||||||||||||||||||||
| 150 | num_format => '0.0%', | ||||||||||||||||||||||||||||
| 151 | align => 'right', | ||||||||||||||||||||||||||||
| 152 | size => 8, | ||||||||||||||||||||||||||||
| 153 | }, | ||||||||||||||||||||||||||||
| 154 | number => { | ||||||||||||||||||||||||||||
| 155 | num_format => '#,##0', | ||||||||||||||||||||||||||||
| 156 | align => 'right', | ||||||||||||||||||||||||||||
| 157 | size => 8, | ||||||||||||||||||||||||||||
| 158 | }, | ||||||||||||||||||||||||||||
| 159 | text => { | ||||||||||||||||||||||||||||
| 160 | align => 'left', | ||||||||||||||||||||||||||||
| 161 | size => 8, | ||||||||||||||||||||||||||||
| 162 | }, | ||||||||||||||||||||||||||||
| 163 | }; | ||||||||||||||||||||||||||||
| 164 | 3 | for my $type (keys %{$types}) { | |||||||||||||||||||||||||||
| 165 | 24 | $formats->{$type} = $workbook->addformat(%{$types->{$type}}); | |||||||||||||||||||||||||||
| 166 | 24 | $formats->{$type . '_hdr'} = $workbook->addformat(%{$types->{$type}}, bold => 1, text_wrap => 0); | |||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | 3 | $formats->{'title_right'} = $workbook->addformat(align => 'right', size => 8, bold => 1, text_wrap => 1); | |||||||||||||||||||||||||||
| 169 | 3 | $formats->{'title_left'} = $workbook->addformat(align => 'left', size => 8, bold => 1, text_wrap => 1); | |||||||||||||||||||||||||||
| 170 | 3 | $formats->{'title_center'} = $workbook->addformat(align => 'center', size => 8, bold => 1, text_wrap => 1); | |||||||||||||||||||||||||||
| 171 | 3 | $formats->{'averages_right'} = $workbook->addformat(align => 'right', size => 8, bold => 1, text_wrap => 0); | |||||||||||||||||||||||||||
| 172 | 3 | $formats->{'averages_left'} = $workbook->addformat(align => 'left', size => 8, bold => 1, text_wrap => 0); | |||||||||||||||||||||||||||
| 173 | 3 | $formats->{'averages_center'} = $workbook->addformat(align => 'center', size => 8, bold => 1, text_wrap => 0); | |||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 3 | my $title_pinned = 0; | |||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 3 | for my $row ($self->rows()) { | |||||||||||||||||||||||||||
| 178 | 64 | ▶ | if ($row->is_title) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 179 | 0 | ▶ | if ($pin_title) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 180 | 0 | ▶ | next if $title_pinned; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 181 | 0 | $title_pinned = 1; | |||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | 64 | for my $cell ($row->cells()) { | |||||||||||||||||||||||||||
| 185 | 440 | my ($y, $x) = ($cell->row_id, $cell->col_id); | |||||||||||||||||||||||||||
| 186 | 440 | my $data = $cell->data; | |||||||||||||||||||||||||||
| 187 | 440 | my $formula = ''; | |||||||||||||||||||||||||||
| 188 | 440 | my $value = 'asdf'; | |||||||||||||||||||||||||||
| 189 | 440 | eval { | |||||||||||||||||||||||||||
| 190 | 440 | ▶ | if (ref $data) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 191 | 440 | $worksheet->write($y, $x, $data->string); | |||||||||||||||||||||||||||
| 192 | } else { | ||||||||||||||||||||||||||||
| 193 | 0 | $worksheet->write($y, $x, $data); | |||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||
| 195 | }; | ||||||||||||||||||||||||||||
| 196 | 440 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 197 | 0 | die "$formula " . $@; | |||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | 4 | 1; | |||||||||||||||||||||||||||
| 204 | __END__ | ||||||||||||||||||||||||||||