1 package SQL::Translator::Parser::Excel;
3 # -------------------------------------------------------------------
4 # $Id: Excel.pm 1440 2009-01-17 16:31:57Z jawnsy $
5 # -------------------------------------------------------------------
6 # Copyright (C) 2002-2009 SQLFairy Authors
8 # This program is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU General Public License as
10 # published by the Free Software Foundation; version 2.
12 # This program is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 # -------------------------------------------------------------------
25 SQL::Translator::Parser::Excel - parser for Excel
31 my $translator = SQL::Translator->new;
32 $translator->parser('Excel');
36 Parses an Excel spreadsheet file using Spreadsheet::ParseExcel.
44 Indicates that the columns should be scanned to determine data types
45 and field sizes. True by default.
52 use vars qw($DEBUG @EXPORT_OK);
53 $DEBUG = 0 unless defined $DEBUG;
55 use Spreadsheet::ParseExcel;
57 use SQL::Translator::Utils qw(debug normalize_name);
59 use base qw(Exporter);
61 @EXPORT_OK = qw(parse);
66 'Numeric' => 'DOUBLE',
69 # -------------------------------------------------------------------
72 # Note that $data, in the case of this parser, is unuseful.
73 # Spreadsheet::ParseExcel works on files, not data streams.
74 # -------------------------------------------------------------------
77 my $args = $tr->parser_args;
78 my $filename = $tr->filename || return;
79 my $wb = Spreadsheet::ParseExcel::Workbook->Parse( $filename );
80 my $schema = $tr->schema;
83 my $wb_count = $wb->{'SheetCount'} || 0;
84 for my $num ( 0 .. $wb_count - 1 ) {
86 my $ws = $wb->Worksheet( $num );
87 my $table_name = normalize_name( $ws->{'Name'} || "Table$table_no" );
89 my @cols = $ws->ColRange;
90 next unless $cols[1] > 0;
92 my $table = $schema->add_table( name => $table_name );
95 for my $col ( $cols[0] .. $cols[1] ) {
96 my $cell = $ws->Cell(0, $col);
97 my $col_name = normalize_name( $cell->{'Val'} );
98 my $data_type = ET_to_ST( $cell->{'Type'} );
99 push @field_names, $col_name;
101 my $field = $table->add_field(
103 data_type => $data_type,
107 is_auto_increment => undef,
108 ) or die $table->error;
111 $table->primary_key( $field->name );
112 $field->is_primary_key(1);
117 # If directed, look at every field's values to guess size and type.
120 defined $args->{'scan_fields'} &&
121 $args->{'scan_fields'} == 0
123 my %field_info = map { $_, {} } @field_names;
126 my $iR = $ws->{'MinRow'} == 0 ? 1 : $ws->{'MinRow'};
127 defined $ws->{'MaxRow'} && $iR <= $ws->{'MaxRow'};
131 my $iC = $ws->{'MinCol'};
132 defined $ws->{'MaxCol'} && $iC <= $ws->{'MaxCol'};
135 my $field = $field_names[ $iC ];
136 my $data = $ws->{'Cells'}[ $iR ][ $iC ]->{'_Value'};
137 next if !defined $data || $data eq '';
138 my $size = [ length $data ];
141 if ( $data =~ /^-?\d+$/ ) {
145 $data =~ /^-?[,\d]+\.[\d+]?$/
147 $data =~ /^-?[,\d]+?\.\d+$/
153 map { s/,//g; length $_ || 1 }
156 $size = [ $w + $d, $d ];
163 next unless defined $size->[ $i ];
164 my $fsize = $field_info{ $field }{'size'}[ $i ] || 0;
165 if ( $size->[ $i ] > $fsize ) {
166 $field_info{ $field }{'size'}[ $i ] = $size->[ $i ];
170 $field_info{ $field }{ $type }++;
174 for my $field ( keys %field_info ) {
175 my $size = $field_info{ $field }{'size'} || [ 1 ];
177 $field_info{ $field }{'char'} ? 'char' :
178 $field_info{ $field }{'float'} ? 'float' :
179 $field_info{ $field }{'integer'} ? 'integer' : 'char';
181 if ( $data_type eq 'char' && scalar @$size == 2 ) {
182 $size = [ $size->[0] + $size->[1] ];
185 my $field = $table->get_field( $field );
186 $field->size( $size ) if $size;
187 $field->data_type( $data_type );
195 # -------------------------------------------------------------------
198 $ET_to_ST{$et} || $ET_to_ST{'Text'};
203 # -------------------------------------------------------------------
204 # Education is an admirable thing,
205 # but it is as well to remember that
206 # nothing that is worth knowing can be taught.
208 # -------------------------------------------------------------------
214 Mike Mellilo <mmelillo@users.sourceforge.net>,
215 darren chamberlain E<lt>dlc@users.sourceforge.netE<gt>,
216 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.
220 Spreadsheet::ParseExcel, SQL::Translator.