fixed bug for mig-stagebibs when the output is already all in a single line
[migration-tools.git] / xls2tab
1 #!/usr/bin/perl
2
3 #~ Converter Script for XLS to TSV.  Handles Multiple Tabs into separate files.
4 #~ (c)2004 Anima Legato <l3gatosan@gmail.com>
5 #~
6 #~ This code is redistributable and modifiable under the same terms as Perl
7 #~ itself.
8
9 use strict;
10 use warnings;
11
12 use Spreadsheet::ParseExcel::Simple;
13 use File::Spec;
14 use Data::Dumper;
15
16 for (@ARGV) {
17     for (glob $_) {
18         next unless m/\.xls$/i;
19         next unless -r $_;
20         dump_books($_);
21     }
22 }
23
24 sub dump_books {
25     my ($vol, $path, $file) = File::Spec->splitpath(shift);
26     my $eBook = Spreadsheet::ParseExcel::Simple->read(File::Spec->catpath($vol,$path,$file));
27     unless (defined $eBook) {
28         warn "Can't open Spreadsheet in file $file (@".File::Spec->catpath($vol,$path,$file)."\n";
29         return undef;
30     }
31     
32     my @sheet = $eBook->sheets;
33     for (0..@sheet-1) {
34         next unless $sheet[$_]->has_data();
35         my $sfn = $file;
36         $sfn =~ s?\.xls$??i;
37         $sfn .= '_' . $sheet[$_]->sheet->get_name() . '.tsv';
38         $sfn =~ s/ /_/g;
39         open TAB, '>', $sfn or do {
40             warn "Unable to write to $sfn";
41             next;
42         };
43         
44         while ($sheet[$_]->has_data) { 
45             my @row = $sheet[$_]->next_row;
46             print TAB join("\t",@row)."\n";
47         }
48     }
49 } ##--dump_books--##