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;
47 use Spreadsheet::ParseExcel;
49 use SQL::Translator::Utils qw(debug normalize_name);
51 use base qw(Exporter);
53 @EXPORT_OK = qw(parse);
55 # -------------------------------------------------------------------
58 # Note that $data, in the case of this parser, is unuseful.
59 # Spreadsheet::ParseExcel works on files, not data streams.
60 # -------------------------------------------------------------------
63 my $filename = $tr->filename || return;
64 my $wb = Spreadsheet::ParseExcel::Workbook->Parse($filename);
65 my (%parsed, $wb_count, $num);
68 $wb_count = $wb->{'SheetCount'} || 0;
69 for $num (0 .. $wb_count - 1) {
70 my $ws = $wb->Worksheet($num);
71 my $name = $ws->{Name} || ++$table_no;
73 $name = normalize_name($name);
75 my @cols = $ws->ColRange;
76 next unless $cols[1] > 0;
85 for my $col ($cols[0] .. $cols[1]) {
86 my $cell = $ws->Cell(0, $col);
87 $parsed{$name}->{'fields'}->{$cell->{Val}} = {
92 # Default datatype is 'char'
93 data_type => ET_to_ST($cell->{Type}),
95 # default size is 8bits; something more reasonable?
101 # field field is the primary key
102 is_primary_key => ($col == 0) ? 1 : undef,
112 'Date' => 'DATETIME',
113 'Numeric' => 'DOUBLE',
117 $ET_to_ST{$et} || $ET_to_ST{'Text'};
126 Mike Mellilo <mmelillo@users.sourceforge.net>,
127 darren chamberlain E<lt>dlc@users.sourceforge.netE<gt>
128 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>
132 perl(1), Spreadsheet::ParseExcel.