1 package Excel::Template::Format;
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.
12 sub new { bless {}, shift }
14 sub _assign { $_[0]{$_[1]} = $_[2]; $_[0]{$_[2]} = $_[1] }
16 # my ($key, $format) = @_;
17 # $self->{$key} = $format;
18 # $self->{$format} = $key;
21 sub _retrieve_key { $_[0]{ $_[1] } }
24 # return $self->{$format};
27 *_retrieve_format = \&_retrieve_key;
28 #sub _retrieve_format {
31 # return $self->{$key};
35 my @_boolean_formats = qw(
36 bold italic locked hidden font_outline font_shadow font_strikeout
37 text_wrap text_justlast shrink
40 my @_integer_formats = qw(
41 size num_format underline rotation indent pattern border
45 my @_string_formats = qw(
46 font color align valign bg_color fg_color border_color
47 bottom_color top_color left_color right_color
53 $params{lc $_} = delete $params{$_} for keys %params;
56 (map { $params{$_} ? 1 : '' } @_boolean_formats),
57 (map { $params{$_} ? $params{$_} + 0 : '' } @_integer_formats),
58 (map { $params{$_} || '' } @_string_formats),
61 return join( "\n", @parts );
68 my @key_parts = split /\n/, $key;
70 my @boolean_parts = splice @key_parts, 0, scalar( @_boolean_formats );
71 my @integer_parts = splice @key_parts, 0, scalar( @_integer_formats );
72 my @string_parts = splice @key_parts, 0, scalar( @_string_formats );
75 $params{ $_boolean_formats[$_] } = ~~1
76 for grep { $boolean_parts[$_] } 0 .. $#_boolean_formats;
78 $params{ $_integer_formats[$_] } = $integer_parts[$_]
79 for grep { defined $integer_parts[$_] && length $integer_parts[$_] } 0 .. $#_integer_formats;
81 $params{ $_string_formats[$_] } = $string_parts[$_]
82 for grep { $string_parts[$_] } 0 .. $#_string_formats;
90 my ($context, $old_fmt, %properties) = @_;
92 # This is a key used for non-format book-keeping.
93 delete $properties{ ELEMENTS };
95 defined(my $key = _retrieve_key($self, $old_fmt))
96 || die "Internal Error: Cannot find key for format '$old_fmt'!\n";
98 my %params = _key_to_params($key);
100 while ( my ($prop, $value) = each %properties )
103 foreach (@_boolean_formats)
106 $params{$_} = ($value && $value !~ /false/i);
110 foreach (@_integer_formats, @_string_formats)
113 $params{$_} = $value;
118 warn "Property '$prop' is unrecognized\n" if $^W;
121 my $new_key = _params_to_key(%params);
123 my $format = _retrieve_format($self, $new_key);
124 return $format if $format;
126 $format = $context->{XLS}->add_format(%params);
127 _assign($self, $new_key, $format);
137 my $blank_key = _params_to_key();
139 my $format = _retrieve_format($self, $blank_key);
140 return $format if $format;
142 $format = $context->{XLS}->add_format;
143 _assign($self, $blank_key, $format);
152 Excel::Template::Format - Excel::Template::Format
156 Helper class for FORMAT
186 Provides a blank format for use
190 Clones an existing format, so that a new format can be built from it
194 Rob Kinyon (rob.kinyon@gmail.com)