1 package SQL::Translator::Parser::Excel;
3 # -------------------------------------------------------------------
4 # $Id: Excel.pm,v 1.9 2003-10-09 21:49:53 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
33 use SQL::Translator::Parser::Excel;
35 my $translator = SQL::Translator->new;
36 $translator->parser("SQL::Translator::Parser::Excel");
40 Parses an Excel spreadsheet file for SQL::Translator. You can then
41 turn the data into a database tables or graphs.
49 Indicates that the columns should be scanned to determine data types
50 and field sizes. True by default.
57 use vars qw($DEBUG $VERSION @EXPORT_OK);
58 $DEBUG = 0 unless defined $DEBUG;
59 $VERSION = sprintf "%d.%02d", q$Revision: 1.9 $ =~ /(\d+)\.(\d+)/;
61 use Spreadsheet::ParseExcel;
63 use SQL::Translator::Utils qw(debug normalize_name);
65 use base qw(Exporter);
67 @EXPORT_OK = qw(parse);
72 'Numeric' => 'DOUBLE',
75 # -------------------------------------------------------------------
78 # Note that $data, in the case of this parser, is unuseful.
79 # Spreadsheet::ParseExcel works on files, not data streams.
80 # -------------------------------------------------------------------
83 my $args = $tr->parser_args;
84 my $filename = $tr->filename || return;
85 my $wb = Spreadsheet::ParseExcel::Workbook->Parse( $filename );
86 my $schema = $tr->schema;
89 my $wb_count = $wb->{'SheetCount'} || 0;
90 for my $num ( 0 .. $wb_count - 1 ) {
92 my $ws = $wb->Worksheet( $num );
93 my $table_name = normalize_name( $ws->{'Name'} || "Table$table_no" );
95 my @cols = $ws->ColRange;
96 next unless $cols[1] > 0;
98 my $table = $schema->add_table( name => $table_name );
100 my @field_names = ();
101 for my $col ( $cols[0] .. $cols[1] ) {
102 my $cell = $ws->Cell(0, $col);
103 my $col_name = normalize_name( $cell->{'Val'} );
104 my $data_type = ET_to_ST( $cell->{'Type'} );
105 push @field_names, $col_name;
107 my $field = $table->add_field(
109 data_type => $data_type,
113 is_auto_increment => undef,
114 ) or die $table->error;
117 $table->primary_key( $field->name );
118 $field->is_primary_key(1);
123 # If directed, look at every field's values to guess size and type.
126 defined $args->{'scan_fields'} &&
127 $args->{'scan_fields'} == 0
129 my %field_info = map { $_, {} } @field_names;
132 my $iR = $ws->{'MinRow'} == 0 ? 1 : $ws->{'MinRow'};
133 defined $ws->{'MaxRow'} && $iR <= $ws->{'MaxRow'};
137 my $iC = $ws->{'MinCol'};
138 defined $ws->{'MaxCol'} && $iC <= $ws->{'MaxCol'};
141 my $field = $field_names[ $iC ];
142 my $data = $ws->{'Cells'}[ $iR ][ $iC ]->{'_Value'};
143 next if !defined $data || $data eq '';
144 my $size = [ length $data ];
147 if ( $data =~ /^-?\d+$/ ) {
151 $data =~ /^-?[,\d]+\.[\d+]?$/
153 $data =~ /^-?[,\d]+?\.\d+$/
158 my ( $w, $d ) = map { s/,//g; $_ } split( /\./, $data );
159 $size = [ length $w, length $d ];
166 next unless defined $size->[ $i ];
167 my $fsize = $field_info{ $field }{'size'}[ $i ] || 0;
168 if ( $size->[ $i ] > $fsize ) {
169 $field_info{ $field }{'size'}[ $i ] = $size->[ $i ];
173 $field_info{ $field }{ $type }++;
177 for my $field ( keys %field_info ) {
178 my $size = $field_info{ $field }{'size'} || 0;
180 $field_info{ $field }{'char'} ? 'char' :
181 $field_info{ $field }{'float'} ? 'float' :
182 $field_info{ $field }{'integer'} ? 'integer' : 'char';
184 if ( $data_type eq 'char' && scalar @$size == 2 ) {
185 $size = [ $size->[0] + $size->[1] ];
188 my $field = $table->get_field( $field );
189 $field->size( $size ) if $size;
190 $field->data_type( $data_type );
198 # -------------------------------------------------------------------
201 $ET_to_ST{$et} || $ET_to_ST{'Text'};
206 # -------------------------------------------------------------------
207 # Education is an admirable thing,
208 # but it is as well to remember that
209 # nothing that is worth knowing can be taught.
211 # -------------------------------------------------------------------
217 Mike Mellilo <mmelillo@users.sourceforge.net>,
218 darren chamberlain E<lt>dlc@users.sourceforge.netE<gt>,
219 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.
223 Spreadsheet::ParseExcel, SQL::Translator.