1 package SQL::Translator::Parser::Excel;
3 # -------------------------------------------------------------------
4 # $Id: Excel.pm,v 1.14 2004-02-09 22:23:40 kycl4rk Exp $
5 # -------------------------------------------------------------------
6 # Copyright (C) 2002-4 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 $VERSION @EXPORT_OK);
53 $DEBUG = 0 unless defined $DEBUG;
54 $VERSION = sprintf "%d.%02d", q$Revision: 1.14 $ =~ /(\d+)\.(\d+)/;
56 use Spreadsheet::ParseExcel;
58 use SQL::Translator::Utils qw(debug normalize_name);
60 use base qw(Exporter);
62 @EXPORT_OK = qw(parse);
67 'Numeric' => 'DOUBLE',
70 # -------------------------------------------------------------------
73 # Note that $data, in the case of this parser, is unuseful.
74 # Spreadsheet::ParseExcel works on files, not data streams.
75 # -------------------------------------------------------------------
78 my $args = $tr->parser_args;
79 my $filename = $tr->filename || return;
80 my $wb = Spreadsheet::ParseExcel::Workbook->Parse( $filename );
81 my $schema = $tr->schema;
84 my $wb_count = $wb->{'SheetCount'} || 0;
85 for my $num ( 0 .. $wb_count - 1 ) {
87 my $ws = $wb->Worksheet( $num );
88 my $table_name = normalize_name( $ws->{'Name'} || "Table$table_no" );
90 my @cols = $ws->ColRange;
91 next unless $cols[1] > 0;
93 my $table = $schema->add_table( name => $table_name );
96 for my $col ( $cols[0] .. $cols[1] ) {
97 my $cell = $ws->Cell(0, $col);
98 my $col_name = normalize_name( $cell->{'Val'} );
99 my $data_type = ET_to_ST( $cell->{'Type'} );
100 push @field_names, $col_name;
102 my $field = $table->add_field(
104 data_type => $data_type,
108 is_auto_increment => undef,
109 ) or die $table->error;
112 $table->primary_key( $field->name );
113 $field->is_primary_key(1);
118 # If directed, look at every field's values to guess size and type.
121 defined $args->{'scan_fields'} &&
122 $args->{'scan_fields'} == 0
124 my %field_info = map { $_, {} } @field_names;
127 my $iR = $ws->{'MinRow'} == 0 ? 1 : $ws->{'MinRow'};
128 defined $ws->{'MaxRow'} && $iR <= $ws->{'MaxRow'};
132 my $iC = $ws->{'MinCol'};
133 defined $ws->{'MaxCol'} && $iC <= $ws->{'MaxCol'};
136 my $field = $field_names[ $iC ];
137 my $data = $ws->{'Cells'}[ $iR ][ $iC ]->{'_Value'};
138 next if !defined $data || $data eq '';
139 my $size = [ length $data ];
142 if ( $data =~ /^-?\d+$/ ) {
146 $data =~ /^-?[,\d]+\.[\d+]?$/
148 $data =~ /^-?[,\d]+?\.\d+$/
154 map { s/,//g; length $_ || 1 }
157 $size = [ $w + $d, $d ];
164 next unless defined $size->[ $i ];
165 my $fsize = $field_info{ $field }{'size'}[ $i ] || 0;
166 if ( $size->[ $i ] > $fsize ) {
167 $field_info{ $field }{'size'}[ $i ] = $size->[ $i ];
171 $field_info{ $field }{ $type }++;
175 for my $field ( keys %field_info ) {
176 my $size = $field_info{ $field }{'size'} || [ 1 ];
178 $field_info{ $field }{'char'} ? 'char' :
179 $field_info{ $field }{'float'} ? 'float' :
180 $field_info{ $field }{'integer'} ? 'integer' : 'char';
182 if ( $data_type eq 'char' && scalar @$size == 2 ) {
183 $size = [ $size->[0] + $size->[1] ];
186 my $field = $table->get_field( $field );
187 $field->size( $size ) if $size;
188 $field->data_type( $data_type );
196 # -------------------------------------------------------------------
199 $ET_to_ST{$et} || $ET_to_ST{'Text'};
204 # -------------------------------------------------------------------
205 # Education is an admirable thing,
206 # but it is as well to remember that
207 # nothing that is worth knowing can be taught.
209 # -------------------------------------------------------------------
215 Mike Mellilo <mmelillo@users.sourceforge.net>,
216 darren chamberlain E<lt>dlc@users.sourceforge.netE<gt>,
217 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.
221 Spreadsheet::ParseExcel, SQL::Translator.