1 package SQL::Translator::Parser::Excel;
5 SQL::Translator::Parser::Excel - parser for Excel
11 my $translator = SQL::Translator->new;
12 $translator->parser('Excel');
16 Parses an Excel spreadsheet file using Spreadsheet::ParseExcel.
24 Indicates that the columns should be scanned to determine data types
25 and field sizes. True by default.
33 our ($DEBUG, @EXPORT_OK);
34 $DEBUG = 0 unless defined $DEBUG;
35 our $VERSION = '1.59';
37 use Spreadsheet::ParseExcel;
39 use SQL::Translator::Utils qw(debug normalize_name);
41 use base qw(Exporter);
43 @EXPORT_OK = qw(parse);
48 'Numeric' => 'DOUBLE',
51 # -------------------------------------------------------------------
54 # Note that $data, in the case of this parser, is unuseful.
55 # Spreadsheet::ParseExcel works on files, not data streams.
56 # -------------------------------------------------------------------
59 my $args = $tr->parser_args;
60 my $filename = $tr->filename || return;
61 my $wb = Spreadsheet::ParseExcel::Workbook->Parse( $filename );
62 my $schema = $tr->schema;
65 my $wb_count = $wb->{'SheetCount'} || 0;
66 for my $num ( 0 .. $wb_count - 1 ) {
68 my $ws = $wb->Worksheet( $num );
69 my $table_name = normalize_name( $ws->{'Name'} || "Table$table_no" );
71 my @cols = $ws->ColRange;
72 next unless $cols[1] > 0;
74 my $table = $schema->add_table( name => $table_name );
77 for my $col ( $cols[0] .. $cols[1] ) {
78 my $cell = $ws->Cell(0, $col);
79 my $col_name = normalize_name( $cell->{'Val'} );
80 my $data_type = ET_to_ST( $cell->{'Type'} );
81 push @field_names, $col_name;
83 my $field = $table->add_field(
85 data_type => $data_type,
89 is_auto_increment => undef,
90 ) or die $table->error;
93 $table->primary_key( $field->name );
94 $field->is_primary_key(1);
99 # If directed, look at every field's values to guess size and type.
102 defined $args->{'scan_fields'} &&
103 $args->{'scan_fields'} == 0
105 my %field_info = map { $_, {} } @field_names;
108 my $iR = $ws->{'MinRow'} == 0 ? 1 : $ws->{'MinRow'};
109 defined $ws->{'MaxRow'} && $iR <= $ws->{'MaxRow'};
113 my $iC = $ws->{'MinCol'};
114 defined $ws->{'MaxCol'} && $iC <= $ws->{'MaxCol'};
117 my $field = $field_names[ $iC ];
118 my $data = $ws->{'Cells'}[ $iR ][ $iC ]->{'_Value'};
119 next if !defined $data || $data eq '';
120 my $size = [ length $data ];
123 if ( $data =~ /^-?\d+$/ ) {
127 $data =~ /^-?[,\d]+\.[\d+]?$/
129 $data =~ /^-?[,\d]+?\.\d+$/
135 map { s/,//g; length $_ || 1 }
138 $size = [ $w + $d, $d ];
145 next unless defined $size->[ $i ];
146 my $fsize = $field_info{ $field }{'size'}[ $i ] || 0;
147 if ( $size->[ $i ] > $fsize ) {
148 $field_info{ $field }{'size'}[ $i ] = $size->[ $i ];
152 $field_info{ $field }{ $type }++;
156 for my $field ( keys %field_info ) {
157 my $size = $field_info{ $field }{'size'} || [ 1 ];
159 $field_info{ $field }{'char'} ? 'char' :
160 $field_info{ $field }{'float'} ? 'float' :
161 $field_info{ $field }{'integer'} ? 'integer' : 'char';
163 if ( $data_type eq 'char' && scalar @$size == 2 ) {
164 $size = [ $size->[0] + $size->[1] ];
167 my $field = $table->get_field( $field );
168 $field->size( $size ) if $size;
169 $field->data_type( $data_type );
179 $ET_to_ST{$et} || $ET_to_ST{'Text'};
184 # -------------------------------------------------------------------
185 # Education is an admirable thing,
186 # but it is as well to remember that
187 # nothing that is worth knowing can be taught.
189 # -------------------------------------------------------------------
195 Mike Mellilo <mmelillo@users.sourceforge.net>,
196 darren chamberlain E<lt>dlc@users.sourceforge.netE<gt>,
197 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.
201 Spreadsheet::ParseExcel, SQL::Translator.