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.
32 use vars qw($DEBUG $VERSION @EXPORT_OK);
33 $DEBUG = 0 unless defined $DEBUG;
36 use Spreadsheet::ParseExcel;
38 use SQL::Translator::Utils qw(debug normalize_name);
40 use base qw(Exporter);
42 @EXPORT_OK = qw(parse);
47 'Numeric' => 'DOUBLE',
50 # -------------------------------------------------------------------
53 # Note that $data, in the case of this parser, is unuseful.
54 # Spreadsheet::ParseExcel works on files, not data streams.
55 # -------------------------------------------------------------------
58 my $args = $tr->parser_args;
59 my $filename = $tr->filename || return;
60 my $wb = Spreadsheet::ParseExcel::Workbook->Parse( $filename );
61 my $schema = $tr->schema;
64 my $wb_count = $wb->{'SheetCount'} || 0;
65 for my $num ( 0 .. $wb_count - 1 ) {
67 my $ws = $wb->Worksheet( $num );
68 my $table_name = normalize_name( $ws->{'Name'} || "Table$table_no" );
70 my @cols = $ws->ColRange;
71 next unless $cols[1] > 0;
73 my $table = $schema->add_table( name => $table_name );
76 for my $col ( $cols[0] .. $cols[1] ) {
77 my $cell = $ws->Cell(0, $col);
78 my $col_name = normalize_name( $cell->{'Val'} );
79 my $data_type = ET_to_ST( $cell->{'Type'} );
80 push @field_names, $col_name;
82 my $field = $table->add_field(
84 data_type => $data_type,
88 is_auto_increment => undef,
89 ) or die $table->error;
92 $table->primary_key( $field->name );
93 $field->is_primary_key(1);
98 # If directed, look at every field's values to guess size and type.
101 defined $args->{'scan_fields'} &&
102 $args->{'scan_fields'} == 0
104 my %field_info = map { $_, {} } @field_names;
107 my $iR = $ws->{'MinRow'} == 0 ? 1 : $ws->{'MinRow'};
108 defined $ws->{'MaxRow'} && $iR <= $ws->{'MaxRow'};
112 my $iC = $ws->{'MinCol'};
113 defined $ws->{'MaxCol'} && $iC <= $ws->{'MaxCol'};
116 my $field = $field_names[ $iC ];
117 my $data = $ws->{'Cells'}[ $iR ][ $iC ]->{'_Value'};
118 next if !defined $data || $data eq '';
119 my $size = [ length $data ];
122 if ( $data =~ /^-?\d+$/ ) {
126 $data =~ /^-?[,\d]+\.[\d+]?$/
128 $data =~ /^-?[,\d]+?\.\d+$/
134 map { s/,//g; length $_ || 1 }
137 $size = [ $w + $d, $d ];
144 next unless defined $size->[ $i ];
145 my $fsize = $field_info{ $field }{'size'}[ $i ] || 0;
146 if ( $size->[ $i ] > $fsize ) {
147 $field_info{ $field }{'size'}[ $i ] = $size->[ $i ];
151 $field_info{ $field }{ $type }++;
155 for my $field ( keys %field_info ) {
156 my $size = $field_info{ $field }{'size'} || [ 1 ];
158 $field_info{ $field }{'char'} ? 'char' :
159 $field_info{ $field }{'float'} ? 'float' :
160 $field_info{ $field }{'integer'} ? 'integer' : 'char';
162 if ( $data_type eq 'char' && scalar @$size == 2 ) {
163 $size = [ $size->[0] + $size->[1] ];
166 my $field = $table->get_field( $field );
167 $field->size( $size ) if $size;
168 $field->data_type( $data_type );
176 # -------------------------------------------------------------------
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.