blib/lib/Spreadsheet/ExcelHashTable.pm

filter: Statementstmt 9.2% 19 / 206 Branchbran 0.0% 0 / 56 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 30.0% 6 / 20 Podpod 77.7% 7 / 9 totaltotal 10.8% 32 / 296 CC 45 SCAR 41.4
CC
45
Cov
7%
_get_table_hash
182.0
excel_checker
90.0
_search_var
42.0
CRAP 63.0
↑ Summary
1 package Spreadsheet::ExcelHashTable;
2 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Spreadsheet::ParseExcel;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Spreadsheet::WriteExcel;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Tie::IxHash;
Subroutine BEGIN: called CC=1 SCAR=0.0
8
9 =head1 NAME
10
11 Spreadsheet::ExcelHashTable - Converts Excel Table to Perl Hash and vicerversa
12
13 =head1 VERSION
14
15 Version 0.02
16
17 =cut
18
19 2 our $VERSION = '0.02';
20
21
22 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod undocumented
23 0 my $class = shift;
24 0 my $self = {
25 excel_hash => {},
26 excel_file => '',
27 excel_sheet => '',
28 };
29 0 bless ( $self, $class);
30 0 tie %{$self->{excel_hash}}, "Tie::IxHash";
31 0 ▶ $self->{excel_file} = shift if(@_);
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
32 0 my $parser = Spreadsheet::ParseExcel->new();
33 0 $self->{excel_obj} = $parser->parse($self->{excel_file});
34 0 return $self;
35 }
36
37 0 ▶sub set_read_xls {
Subroutine set_read_xls: not called CC=1 SCAR=6.9
Pod documented
38 0 my $self = shift;
39 0 $self->{excel_file} = shift ;
40 0 my $parser = Spreadsheet::ParseExcel->new();
41 0 $self->{excel_obj} = $parser->parse($self->{excel_file});
42 }
43
44 0 ▶sub parse_table {
Subroutine parse_table: not called CC=3 SCAR=24.8
Pod undocumented
45 0 my $self = shift;
46 0 my $sheet = shift;
47 0 my $var = shift;
48 0 $self->{excel_sheet} = $sheet;
49 0 $self->{sheet_obj} = $self->{excel_obj}->worksheet($sheet);
50 0 $self->{table_name} = $var;
51 0 ▶ if ($self->excel_checker()) { $self->error(); die; }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->excel_checker) 0 0 0
52 0 my $cell_info = $self->_search_var($var);
53 0 ▶ if ( ! exists $self->{excel_hash}->{$var} ) { $self->{excel_hash}->{$var} = {}; }
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"excel_hash"}{$var}) 0 0 0
54 0 my ($row_v, $col_v) = split " ", $cell_info;
55 0 my $row_v_max = $self->_get_table_row_max($row_v, $col_v);
56 0 my $col_v_max = $self->_get_table_col_max($row_v, $col_v);
57 0 $self->_get_table_hash($row_v, $col_v, $row_v_max, $col_v_max);
58 }
59
60 0 ▶sub _get_table_hash {
Subroutine _get_table_hash: not called CC=13 SCAR=52.0
61 0 my $self = shift;
62 0 my ($row, $col, $row_max, $col_max) = @_;
63 0 foreach my $r ($row+1 ... $row_max ) {
64 0 my $row_key = $self->{sheet_obj}->get_cell($r,$col)->value ;
65 0 ▶ if ($row_key =~ /\s+$/ ) { $row_key =~ s/\s+$//g ; }
Branch: 1 branch
BranchTrueFalseTotal
if ($row_key =~ /\s+$/) 0 0 0
66 0 ▶ chop($row_key) if ($row_key =~ /\s+$/ );
Branch: 1 branch
BranchTrueFalseTotal
if $row_key =~ /\s+$/ 0 0 0
67 0 $self->{excel_hash}->{$self->{table_name}}->{$row_key} = {};
68 }
69 0 foreach my $c ($col+1 ... $col_max) {
70 0 my $r = $row+1;
71 0 my $row_key = $self->{sheet_obj}->get_cell($r,$col)->value;
72 0 my $att = $self->{sheet_obj}->get_cell($row,$c)->value;
73 0 ▶ if ( $att =~ /\s+$/ ) { $att =~ s/\s+$//g; }
Branch: 1 branch
BranchTrueFalseTotal
if ($att =~ /\s+$/) 0 0 0
74 0 ▶ if ( $row_key =~ /\s+$/ ) { $row_key =~ s/\s+$//g; }
Branch: 1 branch
BranchTrueFalseTotal
if ($row_key =~ /\s+$/) 0 0 0
75 0 $self->{excel_hash}->{$self->{table_name}}->{$row_key}->{$att} = "";
76 0 $r = $r + 1;
77 }
78 0 foreach my $r ( $row+1 ... $row_max ) {
79 0 foreach my $c ( $col+1 ... $col_max ) {
80 0 my $val = "";
81 0 my $cell = $self->{sheet_obj}->get_cell($r,$c);
82 0 ▶ if ( defined $cell ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cell) 0 0 0
83 0 $val = $cell->value();
84 0 ▶ chop($val) if ($val =~ /\s+$/ );
Branch: 1 branch
BranchTrueFalseTotal
if $val =~ /\s+$/ 0 0 0
85 }
86 0 my $row_key = $self->{sheet_obj}->get_cell($r,$col)->value;
87 0 my $col_key = $self->{sheet_obj}->get_cell($row, $c)->value;
88 0 ▶ if ($row_key =~ /\s+$/ ) { $row_key =~ s/\s+$//g; }
Branch: 1 branch
BranchTrueFalseTotal
if ($row_key =~ /\s+$/) 0 0 0
89 0 ▶ if ($col_key =~ /\s+$/ ) { $col_key =~ s/\s+$//g; }
Branch: 1 branch
BranchTrueFalseTotal
if ($col_key =~ /\s+$/) 0 0 0
90 0 $self->{excel_hash}->{$self->{table_name}}->{$row_key}->{$col_key} = $val;
91 }
92 }
93 }
94 0 ▶sub _get_table_col_max {
Subroutine _get_table_col_max: not called CC=5 SCAR=34.0
95 0 my $self = shift;
96 0 my $row_v = shift;
97 0 my $col_v = shift;
98 0 my $col_v_max = $col_v;
99 0 my ( $row_min, $row_max) = $self->{sheet_obj}->row_range;
100 0 my ( $col_min, $col_max) = $self->{sheet_obj}->col_range;
101 0 foreach my $col ( $col_v ... $col_max ) {
102 0 $col_v_max = $col;
103 0 my $cell = $self->{sheet_obj}->get_cell($row_v, $col);
104 0 ▶ if ( ! defined $cell ) { $col_v_max = $col-1; last; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cell) 0 0 0
105 0 ▶ if ( $cell->value eq "" ) { $col_v_max = $col-1; last; }
Branch: 1 branch
BranchTrueFalseTotal
if ($cell->value eq "") 0 0 0
106 }
107 0 ▶ if ( $col_v_max == $col_v ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($col_v_max == $col_v) { } 0 0 0
108 0 my $msg = "ERROR: It is not a Valid Table to create Hash. Double Check <$self->{table_name}>";
109 0 $self->{error_hash}->{$msg} = "";
110 0 return "";
111 } else {
112 0 return $col_v_max;
113 }
114 }
115
116 0 ▶sub error {
Subroutine error: not called CC=2 SCAR=17.9
Pod documented
117 0 my $self = shift;
118 0 foreach my $msg ( keys %{$self->{error_hash}} ) {
119 0 print "\n\t$msg\n";
120 }
121 }
122
123
124 0 ▶sub _get_table_row_max {
Subroutine _get_table_row_max: not called CC=5 SCAR=34.0
125 0 my $self = shift;
126 0 my $row_v = shift;
127 0 my $col_v = shift;
128 0 my $row_v_max = $row_v;
129 0 my ( $row_min, $row_max) = $self->{sheet_obj}->row_range;
130 0 my ( $col_min, $col_max) = $self->{sheet_obj}->col_range;
131 0 foreach my $row ( $row_v ... $row_max ) {
132 0 $row_v_max = $row;
133 0 my $cell = $self->{sheet_obj}->get_cell($row, $col_v);
134 0 ▶ if ( ! defined $cell ) { $row_v_max = $row-1; last; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cell) 0 0 0
135 0 ▶ if ( $cell->value eq "" ) { $row_v_max = $row-1; last; }
Branch: 1 branch
BranchTrueFalseTotal
if ($cell->value eq "") 0 0 0
136 }
137 0 ▶ if ( $row_v_max == $row_v ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row_v_max == $row_v) { } 0 0 0
138 0 my $msg = "ERROR: It is not a Valid Table to create Hash. Double Check <$self->{table_name}>";
139 0 $self->{error_hash}->{$msg} = "";
140 0 return "";
141 } else {
142 0 return $row_v_max;
143 }
144 }
145
146 0 ▶sub _search_var {
Subroutine _search_var: not called CC=6 SCAR=37.4
147 0 my $self = shift;
148 0 my $var = shift;
149
150 0 my ( $row_min, $row_max ) = $self->{sheet_obj}->row_range;
151 0 my ( $col_min, $col_max) = $self->{sheet_obj}->col_range;
152 0 foreach my $col ($col_min ... $col_max ) {
153 0 foreach my $row ( $row_min ... $row_max ) {
154 0 my $cell = $self->{sheet_obj}->get_cell($row, $col);
155 0 ▶ if ( defined $cell ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cell) 0 0 0
156 0 my $val = $cell->value;
157 0 ▶ if ($val =~ /\s+$/ ) { $val =~ s/\s+//g; }
Branch: 1 branch
BranchTrueFalseTotal
if ($val =~ /\s+$/) 0 0 0
158 0 ▶ if ( $val eq "$var") { return "$row $col"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($val eq "$var") 0 0 0
159 }
160 }
161 }
162 0 my $msg = "ERROR: No Cell found with <$var> value in <$self->{excel_file} : $self->{excel_sheet}, Make Sure it is Valid Excel Table\n";
163 0 $self->{error_hash}->{$msg} = "";
164 0 return "";
165 }
166
167 0 ▶sub excel_checker {
Subroutine excel_checker: not called CC=9 SCAR=45.0
Pod documented
168 0 my $self = shift;
169 0 ▶ my $sheet = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
170 0 ▶ my $var = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
171 0 ▶ if ( ! defined $var ) { $var = $self->{table_name} ; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $var) 0 0 0
172 0 ▶ if ( ! defined $sheet ) { $sheet = $self->{excel_sheet} ; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $sheet) 0 0 0
173 0 $self->{sheet_obj} = $self->{excel_obj}->worksheet($sheet);
174 0 ▶ if (! defined $self->{sheet_obj} ) { $self->{error_hash}->{"Work Sheet $sheet Not found in $self->{excel_file}"} = "" ; return 1;}
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"sheet_obj"}) 0 0 0
175 0 my $cell_info = $self->_search_var($var);
176 0 ▶ if ( $cell_info eq "" ) { return 1 ; }
Branch: 1 branch
BranchTrueFalseTotal
if ($cell_info eq "") 0 0 0
177 0 my ($row_v, $col_v ) = split " " , $cell_info;
178 0 my $row_v_max = $self->_get_table_row_max($row_v, $col_v);
179 0 my $col_v_max = $self->_get_table_col_max($row_v, $col_v);
180 0 ▶ if ( $col_v_max eq "" || $row_v_max eq "" ) { return 1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($col_v_max eq "" or $row_v_max eq "") 0 0 0
Condition0%0/3
$col_v_max eq "" or $row_v_max eq ""
l!l&&r!l&&!r
000
MC/DC0%0/2
$col_v_max eq "" or $row_v_max eq ""
$col_v_max eq "" $row_v_max eq ""
Truth table0%0/3
A or B
A= $col_v_max eq ""B= $row_v_max eq ""
ABresult
1X1
011
000
181 0 undef $var;
182 0 undef $sheet;
183 0 return 0;
184 }
185
186 0 ▶sub _print_table {
Subroutine _print_table: not called CC=4 SCAR=30.0
187 0 my $self = shift;
188 0 foreach my $key ( keys %{$self->{excel_hash}} ) {
189 0 print "Table: >$key< \n";
190 0 foreach my $k ( keys %{$self->{excel_hash}->{$key}} ) {
191 0 print ">$k<:\n";
192 0 foreach my $j ( keys %{$self->{excel_hash}->{$key}->{$k}} ) {
193 0 print ">$j<: >$self->{excel_hash}->{$key}->{$k}->{$j}<\n";
194 }
195 }
196 }
197 }
198
199 0 ▶sub set_write_xls {
Subroutine set_write_xls: not called CC=1 SCAR=6.9
Pod documented
200 0 my $self = shift;
201 0 my $xls = shift;
202 0 $self->{work_book_obj} = Spreadsheet::WriteExcel->new($xls);
203 }
204
205 0 ▶sub write_excel {
Subroutine write_excel: not called CC=5 SCAR=34.0
Pod documented
206 0 my $self = shift;
207 0 my $worksheet = shift;
208 0 my $table = shift;
209 0 my $work_sheet = $self->{work_book_obj}->add_worksheet($worksheet);
210 0 my $row = 2;
211 0 my $col = 2;
212 0 my $format = $self->{work_book_obj}->add_format();
213 0 $format->set_bold();
214 0 $format->set_color("blue");
215 0 $format->set_align("center");
216 0 $work_sheet->write($row, $col, $table, $format );
217 0 foreach my $row_key ( keys %{$self->{excel_hash}->{$table}} ) {
218 0 $row = $row + 1;
219 0 $work_sheet->write($row, $col, $row_key, $format );
220 }
221 0 $row = 2;
222 0 foreach my $row_key ( keys %{$self->{excel_hash}->{$table}} ) {
223 0 $col = 2;
224 #print "$row_key\n";
225 0 foreach my $col_key ( keys %{$self->{excel_hash}->{$table}->{$row_key}} ) {
226 #print "$row $col $col_key\n";
227 0 $col = $col + 1;
228 0 ▶ if( $row == 2) { $work_sheet->write($row, $col, $col_key, $format ); }
Branch: 1 branch
BranchTrueFalseTotal
if ($row == 2) 0 0 0
229 0 $work_sheet->write($row+1, $col, $self->{excel_hash}->{$table}->{$row_key}->{$col_key} );
230 }
231 0 $row = $row +1;
232 }
233 }
234
235 0 ▶sub get_table {
Subroutine get_table: not called CC=1 SCAR=6.9
Pod documented
236 0 my $self = shift;
237 0 my $table = shift;
238 0 return $self->{excel_hash}->{$table};
239 }
240
241 0 ▶sub get_xl_tables {
Subroutine get_xl_tables: not called CC=1 SCAR=6.9
Pod documented
242 0 my $self = shift;
243 0 return $self->{excel_hash};
244 }
245
246 2 1;
247
248 __END__
↑ Summary