| 1 | #ABSTRACT: read xls / write xls | ||||||||||||||||||||
| 2 | package XLS::Simple; | ||||||||||||||||||||
| 3 | |||||||||||||||||||||
| 4 | 1 | require Exporter; | |||||||||||||||||||
| 5 | 1 | @ISA = qw(Exporter); | |||||||||||||||||||
| 6 | 1 | @EXPORT = qw(write_xls read_xls read_xlsx); | |||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 1 | our $VERSION=0.021; | |||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | 1 | ▶ | use Encode; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 1 | ▶ | use Excel::Writer::XLSX; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | 1 | ▶ | use Spreadsheet::Read; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | 1 | ▶ | use Spreadsheet::XLSX; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 16 | 1 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | 1 | our %XLS_FORMAT_DATA = ( | |||||||||||||||||||
| 19 | align => 'right', | ||||||||||||||||||||
| 20 | size => 13.5, | ||||||||||||||||||||
| 21 | border => 1, | ||||||||||||||||||||
| 22 | ); | ||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | 1 | our %XLS_FORMAT_HEADER = ( | |||||||||||||||||||
| 25 | %XLS_FORMAT_DATA, | ||||||||||||||||||||
| 26 | color => 'blue', | ||||||||||||||||||||
| 27 | bold => 1, | ||||||||||||||||||||
| 28 | ); | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | 1 | ▶ | sub write_xls { | ||||||||||||||||||
|
Subroutine
write_xls: called CC=4 SCAR=14.9
Pod
undocumented
| |||||||||||||||||||||
| 31 | 1 | my ( $data, $fname, %opt ) = @_; | |||||||||||||||||||
| 32 | 1 | format_xls_data( $data, %opt ); | |||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | 1 | my $workbook = Excel::Writer::XLSX->new($fname); | |||||||||||||||||||
| 35 | 1 | my $worksheet = $workbook->add_worksheet(); | |||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | my $fmt_data = | ||||||||||||||||||||
| 38 | $workbook->add_format( | ||||||||||||||||||||
| 39 | 0 | ▶ | $opt{format_data} ? %{ $opt{format_data} } : %XLS_FORMAT_DATA ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 40 | 1 | ▶ | if ( $opt{header} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 41 | my $fmt_head = | ||||||||||||||||||||
| 42 | $workbook->add_format( $opt{format_header} | ||||||||||||||||||||
| 43 | 0 | ▶ | ? %{ $opt{format_header} } | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 44 | : %XLS_FORMAT_HEADER ); | ||||||||||||||||||||
| 45 | 1 | $worksheet->write_row( 0, 0, $opt{header}, $fmt_head ); | |||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | 1 | $worksheet->write_col( 1, 0, $data, $fmt_data ); | |||||||||||||||||||
| 48 | } | ||||||||||||||||||||
| 49 | else { | ||||||||||||||||||||
| 50 | 0 | $worksheet->write_col( 0, 0, $data, $fmt_data ); | |||||||||||||||||||
| 51 | } | ||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | 1 | $workbook->close(); | |||||||||||||||||||
| 54 | 1 | return $fname; | |||||||||||||||||||
| 55 | } | ||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | 1 | ▶ | sub format_xls_data { | ||||||||||||||||||
|
Subroutine
format_xls_data: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||
| 58 | 1 | my ( $data, %opt ) = @_; | |||||||||||||||||||
| 59 | 1 | ▶ | return $data unless ( exists $opt{charset} ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | 1 | for my $d ( $opt{header}, @$data ) { | |||||||||||||||||||
| 62 | 2 | for my $x (@$d) { | |||||||||||||||||||
| 63 | 4 | $x =~ s/^\s+|\s+$//; | |||||||||||||||||||
| 64 | 4 | $x = decode( $opt{charset}, $x ); | |||||||||||||||||||
| 65 | } | ||||||||||||||||||||
| 66 | } ## end for my $d (@$sheet_data) | ||||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | 1 | return $data; | |||||||||||||||||||
| 69 | } | ||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | 3 | ▶ | sub read_xlsx { | ||||||||||||||||||
|
Subroutine
read_xlsx: called CC=7 SCAR=19.8
Pod
undocumented
| |||||||||||||||||||||
| 72 | 3 | my ($xlsx, %opt) = @_; | |||||||||||||||||||
| 73 | 3 | my $excel = Spreadsheet::XLSX->new($xlsx); | |||||||||||||||||||
| 74 | 3 | my @res; | |||||||||||||||||||
| 75 | 3 | for my $sheet (@{$excel->{Worksheet}}){ | |||||||||||||||||||
| 76 | 3 | ▶ | my $max_row = $sheet->{MaxRow} || $sheet->{MinRow}; | ||||||||||||||||||
|
Condition33%1/3
$sheet->{'MaxRow'} || $sheet->{'MinRow'}
MC/DC0%0/2
$sheet->{'MaxRow'} || $sheet->{'MinRow'}
$sheet->{'MaxRow'}
$sheet->{'MinRow'}
Truth table33%1/3
A || B
A= $sheet->{'MaxRow'}B= $sheet->{'MinRow'}
| |||||||||||||||||||||
| 77 | 3 | for my $row ($sheet->{MinRow} .. $max_row){ | |||||||||||||||||||
| 78 | 5 | ▶ | my $max_col = $sheet->{MaxCol} || $sheet->{MinCol}; | ||||||||||||||||||
|
Condition33%1/3
$sheet->{'MaxCol'} || $sheet->{'MinCol'}
MC/DC0%0/2
$sheet->{'MaxCol'} || $sheet->{'MinCol'}
$sheet->{'MaxCol'}
$sheet->{'MinCol'}
Truth table33%1/3
A || B
A= $sheet->{'MaxCol'}B= $sheet->{'MinCol'}
| |||||||||||||||||||||
| 79 | 10 | my @data = map { $sheet->{Cells}[$row][$_]{Val} } | |||||||||||||||||||
| 80 | 5 | ($sheet->{MinCol} .. $max_col); | |||||||||||||||||||
| 81 | 5 | push @res, \@data; | |||||||||||||||||||
| 82 | 5 | ▶ | return \@data if($opt{only_header}); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 83 | } | ||||||||||||||||||||
| 84 | } | ||||||||||||||||||||
| 85 | 2 | ▶ | shift @res if ( $opt{skip_header} ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 86 | 2 | return \@res; | |||||||||||||||||||
| 87 | } | ||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | 3 | ▶ | sub read_xls { | ||||||||||||||||||
|
Subroutine
read_xls: called CC=4 SCAR=23.7
Pod
undocumented
| |||||||||||||||||||||
| 90 | 3 | my ( $xls, %opt ) = @_; | |||||||||||||||||||
| 91 | 3 | ▶ | return read_xlsx($xls, %opt) if($xls=~/\.xlsx$/i); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | 0 | my $workbook = ReadData($xls); | |||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | my @data = | ||||||||||||||||||||
| 96 | $opt{only_header} | ||||||||||||||||||||
| 97 | 0 | ▶ | ? Spreadsheet::Read::cellrow( $workbook->[1], 1 ) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 98 | : Spreadsheet::Read::rows( $workbook->[1] ); | ||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | 0 | ▶ | shift @data if ( $opt{skip_header} ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | 0 | return \@data; | |||||||||||||||||||
| 103 | } | ||||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | 1 | 1; | |||||||||||||||||||