File Coverage

blib/lib/Spreadsheet/ExcelHashTable.pm
Criterion Covered Total %
statement 18 205 8.7
branch 0 56 0.0
condition 0 3 0.0
subroutine 6 20 30.0
pod 7 9 77.7
total 31 293 10.5


line stmt bran cond sub pod time code
1             package Spreadsheet::ExcelHashTable;
2 1     1   19409 use 5.006;
  1         3  
  1         33  
3 1     1   4 use strict;
  1         2  
  1         31  
4 1     1   5 use warnings;
  1         6  
  1         27  
5 1     1   1785 use Spreadsheet::ParseExcel;
  1         76162  
  1         32  
6 1     1   1941 use Spreadsheet::WriteExcel;
  1         83782  
  1         135  
7 1     1   960 use Tie::IxHash;
  1         2158  
  1         1903  
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             our $VERSION = '0.02';
20            
21            
22             sub new {
23 0     0 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";
  0            
31 0 0         $self->{excel_file} = shift if(@_);
32 0           my $parser = Spreadsheet::ParseExcel->new();
33 0           $self->{excel_obj} = $parser->parse($self->{excel_file});
34 0           return $self;
35             }
36            
37             sub set_read_xls {
38 0     0 1   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             sub parse_table {
45 0     0 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 0         if ($self->excel_checker()) { $self->error(); die; }
  0            
  0            
52 0           my $cell_info = $self->_search_var($var);
53 0 0         if ( ! exists $self->{excel_hash}->{$var} ) { $self->{excel_hash}->{$var} = {}; }
  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             sub _get_table_hash {
61 0     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 0         if ($row_key =~ /\s+$/ ) { $row_key =~ s/\s+$//g ; }
  0            
66 0 0         chop($row_key) if ($row_key =~ /\s+$/ );
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 0         if ( $att =~ /\s+$/ ) { $att =~ s/\s+$//g; }
  0            
74 0 0         if ( $row_key =~ /\s+$/ ) { $row_key =~ s/\s+$//g; }
  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 0         if ( defined $cell ) {
83 0           $val = $cell->value();
84 0 0         chop($val) if ($val =~ /\s+$/ );
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 0         if ($row_key =~ /\s+$/ ) { $row_key =~ s/\s+$//g; }
  0            
89 0 0         if ($col_key =~ /\s+$/ ) { $col_key =~ s/\s+$//g; }
  0            
90 0           $self->{excel_hash}->{$self->{table_name}}->{$row_key}->{$col_key} = $val;
91             }
92             }
93             }
94             sub _get_table_col_max {
95 0     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 0         if ( ! defined $cell ) { $col_v_max = $col-1; last; }
  0            
  0            
105 0 0         if ( $cell->value eq "" ) { $col_v_max = $col-1; last; }
  0            
  0            
106             }
107 0 0         if ( $col_v_max == $col_v ) {
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             sub error {
117 0     0 1   my $self = shift;
118 0           foreach my $msg ( keys %{$self->{error_hash}} ) {
  0            
119 0           print "\n\t$msg\n";
120             }
121             }
122            
123            
124             sub _get_table_row_max {
125 0     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 0         if ( ! defined $cell ) { $row_v_max = $row-1; last; }
  0            
  0            
135 0 0         if ( $cell->value eq "" ) { $row_v_max = $row-1; last; }
  0            
  0            
136             }
137 0 0         if ( $row_v_max == $row_v ) {
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             sub _search_var {
147 0     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 0         if ( defined $cell ) {
156 0           my $val = $cell->value;
157 0 0         if ($val =~ /\s+$/ ) { $val =~ s/\s+//g; }
  0            
158 0 0         if ( $val eq "$var") { return "$row $col"; }
  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             sub excel_checker {
168 0     0 1   my $self = shift;
169 0 0         my $sheet = shift if @_;
170 0 0         my $var = shift if @_;
171 0 0         if ( ! defined $var ) { $var = $self->{table_name} ; }
  0            
172 0 0         if ( ! defined $sheet ) { $sheet = $self->{excel_sheet} ; }
  0            
173 0           $self->{sheet_obj} = $self->{excel_obj}->worksheet($sheet);
174 0 0         if (! defined $self->{sheet_obj} ) { $self->{error_hash}->{"Work Sheet $sheet Not found in $self->{excel_file}"} = "" ; return 1;}
  0            
  0            
175 0           my $cell_info = $self->_search_var($var);
176 0 0         if ( $cell_info eq "" ) { return 1 ; }
  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 0 0       if ( $col_v_max eq "" || $row_v_max eq "" ) { return 1; }
  0            
181 0           undef $var;
182 0           undef $sheet;
183 0           return 0;
184             }
185            
186             sub _print_table {
187 0     0     my $self = shift;
188 0           foreach my $key ( keys %{$self->{excel_hash}} ) {
  0            
189 0           print "Table: >$key< \n";
190 0           foreach my $k ( keys %{$self->{excel_hash}->{$key}} ) {
  0            
191 0           print ">$k<:\n";
192 0           foreach my $j ( keys %{$self->{excel_hash}->{$key}->{$k}} ) {
  0            
193 0           print ">$j<: >$self->{excel_hash}->{$key}->{$k}->{$j}<\n";
194             }
195             }
196             }
197             }
198            
199             sub set_write_xls {
200 0     0 1   my $self = shift;
201 0           my $xls = shift;
202 0           $self->{work_book_obj} = Spreadsheet::WriteExcel->new($xls);
203             }
204            
205             sub write_excel {
206 0     0 1   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}} ) {
  0            
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}} ) {
  0            
223 0           $col = 2;
224             #print "$row_key\n";
225 0           foreach my $col_key ( keys %{$self->{excel_hash}->{$table}->{$row_key}} ) {
  0            
226             #print "$row $col $col_key\n";
227 0           $col = $col + 1;
228 0 0         if( $row == 2) { $work_sheet->write($row, $col, $col_key, $format ); }
  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             sub get_table {
236 0     0 1   my $self = shift;
237 0           my $table = shift;
238 0           return $self->{excel_hash}->{$table};
239             }
240            
241             sub get_xl_tables {
242 0     0 1   my $self = shift;
243 0           return $self->{excel_hash};
244             }
245            
246             1;
247            
248             __END__