1 package SQL::Translator::Parser::Excel;
3 # -------------------------------------------------------------------
4 # Copyright (C) 2002-2009 SQLFairy Authors
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License as
8 # published by the Free Software Foundation; version 2.
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 # -------------------------------------------------------------------
23 SQL::Translator::Parser::Excel - parser for Excel
29 my $translator = SQL::Translator->new;
30 $translator->parser('Excel');
34 Parses an Excel spreadsheet file using Spreadsheet::ParseExcel.
42 Indicates that the columns should be scanned to determine data types
43 and field sizes. True by default.
50 use vars qw($DEBUG $VERSION @EXPORT_OK);
51 $DEBUG = 0 unless defined $DEBUG;
54 use Spreadsheet::ParseExcel;
56 use SQL::Translator::Utils qw(debug normalize_name);
58 use base qw(Exporter);
60 @EXPORT_OK = qw(parse);
65 'Numeric' => 'DOUBLE',
68 # -------------------------------------------------------------------
71 # Note that $data, in the case of this parser, is unuseful.
72 # Spreadsheet::ParseExcel works on files, not data streams.
73 # -------------------------------------------------------------------
76 my $args = $tr->parser_args;
77 my $filename = $tr->filename || return;
78 my $wb = Spreadsheet::ParseExcel::Workbook->Parse( $filename );
79 my $schema = $tr->schema;
82 my $wb_count = $wb->{'SheetCount'} || 0;
83 for my $num ( 0 .. $wb_count - 1 ) {
85 my $ws = $wb->Worksheet( $num );
86 my $table_name = normalize_name( $ws->{'Name'} || "Table$table_no" );
88 my @cols = $ws->ColRange;
89 next unless $cols[1] > 0;
91 my $table = $schema->add_table( name => $table_name );
94 for my $col ( $cols[0] .. $cols[1] ) {
95 my $cell = $ws->Cell(0, $col);
96 my $col_name = normalize_name( $cell->{'Val'} );
97 my $data_type = ET_to_ST( $cell->{'Type'} );
98 push @field_names, $col_name;
100 my $field = $table->add_field(
102 data_type => $data_type,
106 is_auto_increment => undef,
107 ) or die $table->error;
110 $table->primary_key( $field->name );
111 $field->is_primary_key(1);
116 # If directed, look at every field's values to guess size and type.
119 defined $args->{'scan_fields'} &&
120 $args->{'scan_fields'} == 0
122 my %field_info = map { $_, {} } @field_names;
125 my $iR = $ws->{'MinRow'} == 0 ? 1 : $ws->{'MinRow'};
126 defined $ws->{'MaxRow'} && $iR <= $ws->{'MaxRow'};
130 my $iC = $ws->{'MinCol'};
131 defined $ws->{'MaxCol'} && $iC <= $ws->{'MaxCol'};
134 my $field = $field_names[ $iC ];
135 my $data = $ws->{'Cells'}[ $iR ][ $iC ]->{'_Value'};
136 next if !defined $data || $data eq '';
137 my $size = [ length $data ];
140 if ( $data =~ /^-?\d+$/ ) {
144 $data =~ /^-?[,\d]+\.[\d+]?$/
146 $data =~ /^-?[,\d]+?\.\d+$/
152 map { s/,//g; length $_ || 1 }
155 $size = [ $w + $d, $d ];
162 next unless defined $size->[ $i ];
163 my $fsize = $field_info{ $field }{'size'}[ $i ] || 0;
164 if ( $size->[ $i ] > $fsize ) {
165 $field_info{ $field }{'size'}[ $i ] = $size->[ $i ];
169 $field_info{ $field }{ $type }++;
173 for my $field ( keys %field_info ) {
174 my $size = $field_info{ $field }{'size'} || [ 1 ];
176 $field_info{ $field }{'char'} ? 'char' :
177 $field_info{ $field }{'float'} ? 'float' :
178 $field_info{ $field }{'integer'} ? 'integer' : 'char';
180 if ( $data_type eq 'char' && scalar @$size == 2 ) {
181 $size = [ $size->[0] + $size->[1] ];
184 my $field = $table->get_field( $field );
185 $field->size( $size ) if $size;
186 $field->data_type( $data_type );
194 # -------------------------------------------------------------------
197 $ET_to_ST{$et} || $ET_to_ST{'Text'};
202 # -------------------------------------------------------------------
203 # Education is an admirable thing,
204 # but it is as well to remember that
205 # nothing that is worth knowing can be taught.
207 # -------------------------------------------------------------------
213 Mike Mellilo <mmelillo@users.sourceforge.net>,
214 darren chamberlain E<lt>dlc@users.sourceforge.netE<gt>,
215 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.
219 Spreadsheet::ParseExcel, SQL::Translator.