1 package SQL::Translator::Parser::Excel;
3 # -------------------------------------------------------------------
4 # $Id: Excel.pm,v 1.13 2003-11-06 16:54:43 kycl4rk Exp $
5 # -------------------------------------------------------------------
6 # Copyright (C) 2003 Ken Y. Clark <kclark@cpan.org>,
7 # darren chamberlain <darren@cpan.org>,
8 # Chris Mungall <cjm@fruitfly.org>,
9 # Mike Mellilo <mmelillo@users.sourceforge.net>
11 # This program is free software; you can redistribute it and/or
12 # modify it under the terms of the GNU General Public License as
13 # published by the Free Software Foundation; version 2.
15 # This program is distributed in the hope that it will be useful, but
16 # WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 # General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with this program; if not, write to the Free Software
22 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
24 # -------------------------------------------------------------------
28 SQL::Translator::Parser::Excel - parser for Excel
34 my $translator = SQL::Translator->new;
35 $translator->parser('Excel');
39 Parses an Excel spreadsheet file using Spreadsheet::ParseExcel.
47 Indicates that the columns should be scanned to determine data types
48 and field sizes. True by default.
55 use vars qw($DEBUG $VERSION @EXPORT_OK);
56 $DEBUG = 0 unless defined $DEBUG;
57 $VERSION = sprintf "%d.%02d", q$Revision: 1.13 $ =~ /(\d+)\.(\d+)/;
59 use Spreadsheet::ParseExcel;
61 use SQL::Translator::Utils qw(debug normalize_name);
63 use base qw(Exporter);
65 @EXPORT_OK = qw(parse);
70 'Numeric' => 'DOUBLE',
73 # -------------------------------------------------------------------
76 # Note that $data, in the case of this parser, is unuseful.
77 # Spreadsheet::ParseExcel works on files, not data streams.
78 # -------------------------------------------------------------------
81 my $args = $tr->parser_args;
82 my $filename = $tr->filename || return;
83 my $wb = Spreadsheet::ParseExcel::Workbook->Parse( $filename );
84 my $schema = $tr->schema;
87 my $wb_count = $wb->{'SheetCount'} || 0;
88 for my $num ( 0 .. $wb_count - 1 ) {
90 my $ws = $wb->Worksheet( $num );
91 my $table_name = normalize_name( $ws->{'Name'} || "Table$table_no" );
93 my @cols = $ws->ColRange;
94 next unless $cols[1] > 0;
96 my $table = $schema->add_table( name => $table_name );
99 for my $col ( $cols[0] .. $cols[1] ) {
100 my $cell = $ws->Cell(0, $col);
101 my $col_name = normalize_name( $cell->{'Val'} );
102 my $data_type = ET_to_ST( $cell->{'Type'} );
103 push @field_names, $col_name;
105 my $field = $table->add_field(
107 data_type => $data_type,
111 is_auto_increment => undef,
112 ) or die $table->error;
115 $table->primary_key( $field->name );
116 $field->is_primary_key(1);
121 # If directed, look at every field's values to guess size and type.
124 defined $args->{'scan_fields'} &&
125 $args->{'scan_fields'} == 0
127 my %field_info = map { $_, {} } @field_names;
130 my $iR = $ws->{'MinRow'} == 0 ? 1 : $ws->{'MinRow'};
131 defined $ws->{'MaxRow'} && $iR <= $ws->{'MaxRow'};
135 my $iC = $ws->{'MinCol'};
136 defined $ws->{'MaxCol'} && $iC <= $ws->{'MaxCol'};
139 my $field = $field_names[ $iC ];
140 my $data = $ws->{'Cells'}[ $iR ][ $iC ]->{'_Value'};
141 next if !defined $data || $data eq '';
142 my $size = [ length $data ];
145 if ( $data =~ /^-?\d+$/ ) {
149 $data =~ /^-?[,\d]+\.[\d+]?$/
151 $data =~ /^-?[,\d]+?\.\d+$/
157 map { s/,//g; length $_ || 1 }
160 $size = [ $w + $d, $d ];
167 next unless defined $size->[ $i ];
168 my $fsize = $field_info{ $field }{'size'}[ $i ] || 0;
169 if ( $size->[ $i ] > $fsize ) {
170 $field_info{ $field }{'size'}[ $i ] = $size->[ $i ];
174 $field_info{ $field }{ $type }++;
178 for my $field ( keys %field_info ) {
179 my $size = $field_info{ $field }{'size'} || [ 1 ];
181 $field_info{ $field }{'char'} ? 'char' :
182 $field_info{ $field }{'float'} ? 'float' :
183 $field_info{ $field }{'integer'} ? 'integer' : 'char';
185 if ( $data_type eq 'char' && scalar @$size == 2 ) {
186 $size = [ $size->[0] + $size->[1] ];
189 my $field = $table->get_field( $field );
190 $field->size( $size ) if $size;
191 $field->data_type( $data_type );
199 # -------------------------------------------------------------------
202 $ET_to_ST{$et} || $ET_to_ST{'Text'};
207 # -------------------------------------------------------------------
208 # Education is an admirable thing,
209 # but it is as well to remember that
210 # nothing that is worth knowing can be taught.
212 # -------------------------------------------------------------------
218 Mike Mellilo <mmelillo@users.sourceforge.net>,
219 darren chamberlain E<lt>dlc@users.sourceforge.netE<gt>,
220 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.
224 Spreadsheet::ParseExcel, SQL::Translator.