| 1 | package Excel::Template::Format; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | # This is the format repository. Spreadsheet::WriteExcel does not cache the | ||||||||||||||||||||
| 6 | # known formats. So, it is very possible to continually add the same format | ||||||||||||||||||||
| 7 | # over and over until you run out of RAM or addressability in the XLS file. In | ||||||||||||||||||||
| 8 | # real life, less than 10-20 formats are used, and they're re-used in various | ||||||||||||||||||||
| 9 | # places in the file. This provides a way of keeping track of already-allocated | ||||||||||||||||||||
| 10 | # formats and making new formats based on old ones. | ||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | 0 | ▶ | sub new { bless {}, shift } | ||||||||||||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | 0 | ▶ | sub _assign { $_[0]{$_[1]} = $_[2]; $_[0]{$_[2]} = $_[1] } | ||||||||||||||||||
|
Subroutine
_assign: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 15 | # my $self = shift; | ||||||||||||||||||||
| 16 | # my ($key, $format) = @_; | ||||||||||||||||||||
| 17 | # $self->{$key} = $format; | ||||||||||||||||||||
| 18 | # $self->{$format} = $key; | ||||||||||||||||||||
| 19 | #} | ||||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | 0 | ▶ | sub _retrieve_key { $_[0]{ $_[1] } } | ||||||||||||||||||
|
Subroutine
_retrieve_key: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 22 | # my $self = shift; | ||||||||||||||||||||
| 23 | # my ($format) = @_; | ||||||||||||||||||||
| 24 | # return $self->{$format}; | ||||||||||||||||||||
| 25 | #} | ||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | 1 | *_retrieve_format = \&_retrieve_key; | |||||||||||||||||||
| 28 | #sub _retrieve_format { | ||||||||||||||||||||
| 29 | # my $self = shift; | ||||||||||||||||||||
| 30 | # my ($key) = @_; | ||||||||||||||||||||
| 31 | # return $self->{$key}; | ||||||||||||||||||||
| 32 | #} | ||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | { | ||||||||||||||||||||
| 35 | 1 | my @_boolean_formats = qw( | |||||||||||||||||||
| 36 | bold italic locked hidden font_outline font_shadow font_strikeout | ||||||||||||||||||||
| 37 | text_wrap text_justlast shrink is_merged | ||||||||||||||||||||
| 38 | ); | ||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | 1 | my @_integer_formats = qw( | |||||||||||||||||||
| 41 | size underline rotation indent pattern border | ||||||||||||||||||||
| 42 | bottom top left right | ||||||||||||||||||||
| 43 | ); | ||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | 1 | my @_string_formats = qw( | |||||||||||||||||||
| 46 | num_format font color align valign bg_color fg_color border_color | ||||||||||||||||||||
| 47 | bottom_color top_color left_color right_color | ||||||||||||||||||||
| 48 | ); | ||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | 1 | my @_fake_slots = qw( | |||||||||||||||||||
| 51 | is_merged | ||||||||||||||||||||
| 52 | ); | ||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | sub _params_to_key | ||||||||||||||||||||
| 55 | { | ||||||||||||||||||||
| 56 | 0 | ▶ | my %params = @_; | ||||||||||||||||||
|
Subroutine
_params_to_key: not called CC=5 SCAR=34.0
| |||||||||||||||||||||
| 57 | 0 | $params{lc $_} = delete $params{$_} for keys %params; | |||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | my @parts = ( | ||||||||||||||||||||
| 60 | 0 | ▶ | (map { $params{$_} ? 1 : '' } @_boolean_formats), | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 61 | 0 | ▶ | (map { $params{$_} ? $params{$_} + 0 : '' } @_integer_formats), | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 62 | 0 | ▶ | (map { $params{$_} || '' } @_string_formats), | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 63 | ); | ||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | 0 | return join( "\n", @parts ); | |||||||||||||||||||
| 66 | } | ||||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | sub _key_to_params | ||||||||||||||||||||
| 69 | { | ||||||||||||||||||||
| 70 | 0 | ▶ | my ($key) = @_; | ||||||||||||||||||
|
Subroutine
_key_to_params: not called CC=5 SCAR=34.0
| |||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | 0 | my @key_parts = split /\n/, $key; | |||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | 0 | my @boolean_parts = splice @key_parts, 0, scalar( @_boolean_formats ); | |||||||||||||||||||
| 75 | 0 | my @integer_parts = splice @key_parts, 0, scalar( @_integer_formats ); | |||||||||||||||||||
| 76 | 0 | my @string_parts = splice @key_parts, 0, scalar( @_string_formats ); | |||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | 0 | my %params; | |||||||||||||||||||
| 79 | $params{ $_boolean_formats[$_] } = ~~1 | ||||||||||||||||||||
| 80 | 0 | for grep { $boolean_parts[$_] } 0 .. $#_boolean_formats; | |||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | $params{ $_integer_formats[$_] } = $integer_parts[$_] | ||||||||||||||||||||
| 83 | 0 | ▶ | for grep { defined $integer_parts[$_] && length $integer_parts[$_] } 0 .. $#_integer_formats; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | $params{ $_string_formats[$_] } = $string_parts[$_] | ||||||||||||||||||||
| 86 | 0 | for grep { $string_parts[$_] } 0 .. $#_string_formats; | |||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | 0 | return %params; | |||||||||||||||||||
| 89 | } | ||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | sub copy | ||||||||||||||||||||
| 92 | { | ||||||||||||||||||||
| 93 | 0 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
copy: not called CC=11 SCAR=48.8
Pod
documented
| |||||||||||||||||||||
| 94 | 0 | my ($context, $old_fmt, %properties) = @_; | |||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | # This is a key used for non-format book-keeping. | ||||||||||||||||||||
| 97 | 0 | delete $properties{ ELEMENTS }; | |||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | 0 | ▶ | defined(my $key = _retrieve_key($self, $old_fmt)) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 100 | || die "Internal Error: Cannot find key for format '$old_fmt'!\n"; | ||||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | 0 | my %params = _key_to_params($key); | |||||||||||||||||||
| 103 | PROPERTY: | ||||||||||||||||||||
| 104 | 0 | ▶ | while ( my ($prop, $value) = each %properties ) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 105 | { | ||||||||||||||||||||
| 106 | 0 | $prop = lc $prop; | |||||||||||||||||||
| 107 | 0 | foreach (@_boolean_formats) | |||||||||||||||||||
| 108 | { | ||||||||||||||||||||
| 109 | 0 | ▶ | if ($prop eq $_) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 110 | 0 | ▶ | $params{$_} = ($value && $value !~ /false/i); | ||||||||||||||||||
|
Condition0%0/3
$value && !($value =~ /false/i)
MC/DC0%0/2
$value && !($value =~ /false/i)
$value
!($value =~ /false/i)
Truth table0%0/3
A && not(B)
A= $valueB= !($value =~ /false/i)
| |||||||||||||||||||||
| 111 | 0 | next PROPERTY; | |||||||||||||||||||
| 112 | } | ||||||||||||||||||||
| 113 | } | ||||||||||||||||||||
| 114 | 0 | foreach (@_integer_formats, @_string_formats) | |||||||||||||||||||
| 115 | { | ||||||||||||||||||||
| 116 | 0 | ▶ | if ($prop eq $_) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 117 | 0 | $params{$_} = $value; | |||||||||||||||||||
| 118 | 0 | next PROPERTY; | |||||||||||||||||||
| 119 | } | ||||||||||||||||||||
| 120 | } | ||||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | 0 | ▶ | warn "Property '$prop' is unrecognized\n" if $^W; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 123 | } | ||||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | 0 | my $new_key = _params_to_key(%params); | |||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | 0 | my $format = _retrieve_format($self, $new_key); | |||||||||||||||||||
| 128 | 0 | ▶ | return $format if $format; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 129 | |||||||||||||||||||||
| 130 | 0 | delete $params{$_} for @_fake_slots; | |||||||||||||||||||
| 131 | |||||||||||||||||||||
| 132 | 0 | $format = $context->{XLS}->add_format(%params); | |||||||||||||||||||
| 133 | 0 | _assign($self, $new_key, $format); | |||||||||||||||||||
| 134 | 0 | return $format; | |||||||||||||||||||
| 135 | } | ||||||||||||||||||||
| 136 | } | ||||||||||||||||||||
| 137 | |||||||||||||||||||||
| 138 | sub blank_format | ||||||||||||||||||||
| 139 | 1 | { | |||||||||||||||||||
| 140 | 0 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
blank_format: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 141 | 0 | my ($context) = @_; | |||||||||||||||||||
| 142 | |||||||||||||||||||||
| 143 | 0 | my $blank_key = _params_to_key(); | |||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | 0 | my $format = _retrieve_format($self, $blank_key); | |||||||||||||||||||
| 146 | 0 | ▶ | return $format if $format; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 147 | |||||||||||||||||||||
| 148 | 0 | $format = $context->{XLS}->add_format; | |||||||||||||||||||
| 149 | 0 | _assign($self, $blank_key, $format); | |||||||||||||||||||
| 150 | 0 | return $format; | |||||||||||||||||||
| 151 | } | ||||||||||||||||||||
| 152 | |||||||||||||||||||||
| 153 | 1 | 1; | |||||||||||||||||||
| 154 | __END__ | ||||||||||||||||||||