1 package SQL::Translator::Parser::Excel;
3 # -------------------------------------------------------------------
4 # Copyright (C) 2003 Ken Y. Clark <kclark@cpan.org>,
5 # darren chamberlain <darren@cpan.org>,
6 # Chris Mungall <cjm@fruitfly.org>,
7 # Mike Mellilo <mmelillo@users.sourceforge.net>
9 # This program is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU General Public License as
11 # published by the Free Software Foundation; version 2.
13 # This program is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 # General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22 # -------------------------------------------------------------------
26 SQL::Translator::Parser::Excel - parser for Excel
31 use SQL::Translator::Parser::Excel;
33 my $translator = SQL::Translator->new;
34 $translator->parser("SQL::Translator::Parser::Excel");
38 Parses an Excel spreadsheet file for SQL::Translator. You can then
39 turn the data into a database tables or graphs.
44 use vars qw($DEBUG $VERSION @EXPORT_OK);
45 $DEBUG = 0 unless defined $DEBUG;
46 $VERSION = sprintf "%d.%02d", q$Revision: 1.7 $ =~ /(\d+)\.(\d+)/;
48 use Spreadsheet::ParseExcel;
50 use SQL::Translator::Utils qw(debug normalize_name);
52 use base qw(Exporter);
54 @EXPORT_OK = qw(parse);
59 'Numeric' => 'DOUBLE',
62 # -------------------------------------------------------------------
65 # Note that $data, in the case of this parser, is unuseful.
66 # Spreadsheet::ParseExcel works on files, not data streams.
67 # -------------------------------------------------------------------
70 my $filename = $tr->filename || return;
71 my $wb = Spreadsheet::ParseExcel::Workbook->Parse( $filename );
72 my $schema = $tr->schema;
75 my $wb_count = $wb->{'SheetCount'} || 0;
76 for my $num ( 0 .. $wb_count - 1 ) {
78 my $ws = $wb->Worksheet( $num );
79 my $table_name = normalize_name( $ws->{'Name'} || "Table$table_no" );
81 my @cols = $ws->ColRange;
82 next unless $cols[1] > 0;
84 my $table = $schema->add_table( name => $table_name );
86 for my $col ( $cols[0] .. $cols[1] ) {
87 my $cell = $ws->Cell(0, $col);
88 my $col_name = normalize_name( $cell->{'Val'} );
89 my $data_type = ET_to_ST( $cell->{'Type'} );
91 my $field = $table->add_field(
93 data_type => $data_type,
97 is_auto_increment => undef,
98 ) or die $table->error;
101 $table->primary_key( $field->name );
102 $field->is_primary_key(1);
112 $ET_to_ST{$et} || $ET_to_ST{'Text'};
117 # -------------------------------------------------------------------
118 # Education is an admirable thing,
119 # but it is as well to remember that
120 # nothing that is worth knowing can be taught.
122 # -------------------------------------------------------------------
128 Mike Mellilo <mmelillo@users.sourceforge.net>,
129 darren chamberlain E<lt>dlc@users.sourceforge.netE<gt>
130 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>
134 perl(1), Spreadsheet::ParseExcel.