| 1 | package Spreadsheet::Wright::OOXML; | ||
| 2 | |||
| 3 | use 5.010; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | no warnings qw( uninitialized numeric ); | ||
| 7 | |||
| 8 | BEGIN { | ||
| 9 | $Spreadsheet::Wright::OOXML::VERSION = '0.107'; | ||
| 10 | $Spreadsheet::Wright::OOXML::AUTHORITY = 'cpan:TOBYINK'; | ||
| 11 | } | ||
| 12 | |||
| 13 | use parent qw(Spreadsheet::Wright); | ||
| 14 | use Carp; | ||
| 15 | use Excel::Writer::XLSX; | ||
| 16 | |||
| 17 | sub new | ||
| 18 | { | ||
| 19 | my ($class, %args) = @_; | ||
| 20 | |||
| 21 | my $self = bless { 'options' => \%args }, $class; | ||
| 22 | |||
| 23 | $self->{'_FILENAME'} = $args{'file'} // $args{'filename'} | ||
| 24 | or croak "Need filename."; | ||
| 25 | $self->{'_SHEETNAME'} = $args{'sheet'} || ''; | ||
| 26 | $self->{'_STYLES'} = $args{'styles'} || {}; | ||
| 27 | |||
| 28 | return $self; | ||
| 29 | } | ||
| 30 | |||
| 31 | sub _prepare { | ||
| 32 | my $self = shift; | ||
| 33 | my $worksheet = $self->{'_WORKSHEET'}; | ||
| 34 | my $workbook = $self->{'_WORKBOOK'}; | ||
| 35 | |||
| 36 | if(!$worksheet) { | ||
| 37 | $self->{'_FH'}->binmode(); | ||
| 38 | $workbook = Excel::Writer::XLSX->new($self->{'_FH'}); | ||
| 39 | $self->{'_WORKBOOK'} = $workbook; | ||
| 40 | $worksheet = $workbook->add_worksheet($self->{'_SHEETNAME'}); | ||
| 41 | $self->{'_WORKSHEET'} = $worksheet; | ||
| 42 | $self->{'_WORKBOOK_ROW'} = 0; | ||
| 43 | } | ||
| 44 | |||
| 45 | return $self; | ||
| 46 | } | ||
| 47 | |||
| 48 | sub freeze (@) { | ||
| 49 | my $self=shift; | ||
| 50 | $self->_open() || return undef; | ||
| 51 | $self->{'_WORKSHEET'}->freeze_panes(@_); | ||
| 52 | return $self; | ||
| 53 | } | ||
| 54 | |||
| 55 | sub close { | ||
| 56 | my $self=shift; | ||
| 57 | return if $self->{'_CLOSED'}; | ||
| 58 | $self->{'_WORKBOOK'}->close | ||
| 59 | if $self->{'_WORKBOOK'}; | ||
| 60 | $self->{'_FH'}->close | ||
| 61 | if $self->{'_FH'}; | ||
| 62 | $self->{'_CLOSED'} = 1; | ||
| 63 | return $self; | ||
| 64 | } | ||
| 65 | |||
| 66 | sub _format_cache($$) { | ||
| 67 | my ($self, $format) = @_; | ||
| 68 | my $cache_key=''; | ||
| 69 | foreach my $key (sort keys %$format) { | ||
| 70 | $cache_key .= $key . $format->{$key}; | ||
| 71 | } | ||
| 72 | if(exists($self->{'_FORMAT_CACHE'}->{$cache_key})) { | ||
| 73 | return $self->{'_FORMAT_CACHE'}->{$cache_key}; | ||
| 74 | } | ||
| 75 | return $self->{'_FORMAT_CACHE'}->{$cache_key} = $self->{'_WORKBOOK'}->add_format(%$format); | ||
| 76 | } | ||
| 77 | |||
| 78 | sub addsheet ($$) { | ||
| 79 | my ($self,$name)=@_; | ||
| 80 | $self->_open() || return undef; | ||
| 81 | my $worksheet = $self->{'_WORKBOOK'}->add_worksheet($name); | ||
| 82 | $self->{'_SHEETNAME'} = $name; | ||
| 83 | $self->{'_WORKSHEET'} = $worksheet; | ||
| 84 | $self->{'_WORKBOOK_ROW'} = 0; | ||
| 85 | return $self; | ||
| 86 | } | ||
| 87 | |||
| 88 | sub _add_prepared_row { | ||
| 89 | my $self = shift; | ||
| 90 | |||
| 91 | my $worksheet = $self->{'_WORKSHEET'}; | ||
| 92 | my $workbook = $self->{'_WORKBOOK'}; | ||
| 93 | my $row = $self->{'_WORKBOOK_ROW'}; | ||
| 94 | my $col = 0; | ||
| 95 | |||
| 96 | for(my $i=0; $i<scalar(@_); $i++) { | ||
| 97 | my %props = %{ $_[$i] }; | ||
| 98 | my $value = $props{'content'}; | ||
| 99 | |||
| 100 | delete $props{'content'}; | ||
| 101 | my $props = \%props; | ||
| 102 | |||
| 103 | my %format; | ||
| 104 | if(%props) { | ||
| 105 | if(my $stylelist = $props->{'style'}) { | ||
| 106 | $stylelist=[$stylelist] unless ref $stylelist; | ||
| 107 | foreach my $style (ref $stylelist ? @$stylelist : ($stylelist)) { | ||
| 108 | my $stprops = $self->{'_STYLES'}->{$style}; | ||
| 109 | if(!$stprops) { | ||
| 110 | warn "Style '$style' is not defined\n"; | ||
| 111 | } | ||
| 112 | else { | ||
| 113 | my %a; | ||
| 114 | @a{keys %$stprops} = values %$stprops; | ||
| 115 | @a{keys %$props} = values %$props; | ||
| 116 | $props=\%a; | ||
| 117 | } | ||
| 118 | } | ||
| 119 | } | ||
| 120 | |||
| 121 | if(defined $props->{'font_weight'} && $props->{'font_weight'} eq 'bold') { | ||
| 122 | $format{'bold'} = 1; | ||
| 123 | } | ||
| 124 | if(defined $props->{'font_style'} && $props->{'font_style'} eq 'italic') { | ||
| 125 | $format{'italic'} = 1; | ||
| 126 | } | ||
| 127 | if(defined $props->{'font_decoration'} && $props->{'font_decoration'} =~ m'underline') { | ||
| 128 | $format{'underline'} = 1; | ||
| 129 | } | ||
| 130 | if(defined $props->{'font_decoration'} && $props->{'font_decoration'} =~ m'strikeout') { | ||
| 131 | $format{'font_strikeout'} = 1; | ||
| 132 | } | ||
| 133 | if(defined $props->{'font_color'}) { | ||
| 134 | $format{'color'} = $props->{'font_color'}; | ||
| 135 | } | ||
| 136 | if (defined $props->{'bg_color'}) { | ||
| 137 | $format{'bg_color'} = $props->{'bg_color'}; | ||
| 138 | } | ||
| 139 | if (defined $props->{'font_face'}) { | ||
| 140 | $format{'font'}=$props->{'font_face'}; | ||
| 141 | } | ||
| 142 | if (defined $props->{'font_size'}) { | ||
| 143 | $format{'size'}=$props->{'font_size'}; | ||
| 144 | } | ||
| 145 | if (defined $props->{'align'}) { | ||
| 146 | $format{'align'}=$props->{'align'}; | ||
| 147 | } | ||
| 148 | if (defined $props->{'valign'}) { | ||
| 149 | $format{'valign'}=$props->{'valign'}; | ||
| 150 | } | ||
| 151 | if (defined $props->{'format'}) { | ||
| 152 | $format{'num_format'} = $props->{'format'}; | ||
| 153 | } | ||
| 154 | if (defined $props->{'width'}) { | ||
| 155 | $worksheet->set_column($col,$col,$props->{'width'}); | ||
| 156 | } | ||
| 157 | if (defined $props->{'comment'} && length($props->{'comment'})) { | ||
| 158 | $worksheet->write_comment($row,$col,$props->{'comment'}); | ||
| 159 | } | ||
| 160 | } | ||
| 161 | |||
| 162 | my @params = ($row, $col++, $value); | ||
| 163 | push @params, $self->_format_cache(\%format) if keys %format; | ||
| 164 | |||
| 165 | my $type = lc ($props->{'type'} || 'auto'); | ||
| 166 | if ($type eq 'auto') { $worksheet->write(@params); } | ||
| 167 | elsif ($type eq 'string') { $worksheet->write_string(@params); } | ||
| 168 | elsif ($type eq 'text') { $worksheet->write_string(@params); } | ||
| 169 | elsif ($type eq 'number') { $worksheet->write_number(@params); } | ||
| 170 | elsif ($type eq 'blank') { $worksheet->write_blank(@params); } | ||
| 171 | elsif ($type eq 'formula') { $worksheet->write_formula(@params); } | ||
| 172 | elsif ($type eq 'url') { $worksheet->write_url(@params); } | ||
| 173 | else { | ||
| 174 | warn "Unknown cell type $type"; | ||
| 175 | $worksheet->write(@params); | ||
| 176 | } | ||
| 177 | } | ||
| 178 | |||
| 179 | $self->{'_WORKBOOK_ROW'}++; | ||
| 180 | |||
| 181 | return $self; | ||
| 182 | } | ||
| 183 | |||
| 184 | 1; |