Category: | Utillity Scripts |
Author/Contact Info | /msg Corion |
Description: | This script is a raw utility that I use to merge "information" and "data" sheets from different Excel workbooks. The basic syntax is as follows:
The above command line will create results-20091302.xls containing a worksheet "Information" and one worksheet "Sheet1" from the respective Excel files. There is no renaming functionality and duplicate sheet names are fatal. |
#!/usr/bin/perl -w use strict; use Spreadsheet::ParseExcel; use Spreadsheet::WriteExcel; use File::Glob qw(bsd_glob); use Getopt::Long; use POSIX qw(strftime); GetOptions( 'output|o=s' => \my $outfile, 'strftime|t' => \my $do_strftime, ) or die; if ($do_strftime) { $outfile = strftime $outfile, localtime; }; my $output = Spreadsheet::WriteExcel->new($outfile) or die "Couldn't create '$outfile': $!"; for (@ARGV) { my ($filename,$sheetname,$targetname); my @files; if (m!^(.*\.xls):(.*?)(?::([\w ]+))$!) { ($filename,$sheetname,$targetname) = ($1,qr($2),$3); warn $filename; if ($do_strftime) { $filename = strftime $filename, localtime; }; @files = glob $filename; } else { ($filename,$sheetname,$targetname) = ($_,qr(.*),undef); if ($do_strftime) { $filename = strftime $filename, localtime; }; push @files, glob $filename; }; for my $f (@files) { my $excel = Spreadsheet::ParseExcel::Workbook->Parse($f); foreach my $sheet (@{$excel->{Worksheet}}) { if ($sheet->{Name} !~ /$sheetname/) { warn "Skipping '" . $sheet->{Name} . "' (/$sheetname/) +"; next; }; $targetname ||= $sheet->{Name}; #warn sprintf "Copying %s to %s\n", $sheet->{Name}, $targe +tname; my $s = $output->add_worksheet($targetname); $sheet->{MaxRow} ||= $sheet->{MinRow}; foreach my $row ($sheet->{MinRow} .. $sheet->{MaxRow}) { my @rowdata = map { $sheet->{Cells}->[$row]->[$_]->{Val}; } $sheet->{MinCol} .. $sheet->{MaxCol}; $s->write($row,0,\@rowdata); } } }; }; $output->close; |
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: xlsmerge - Merge worksheets from different Excel files into one Workbook
by ambrus (Abbot) on Jun 15, 2010 at 13:03 UTC | |
by Corion (Patriarch) on Jun 15, 2010 at 13:05 UTC | |
Re: xlsmerge - Merge worksheets from different Excel files into one Workbook
by Anonymous Monk on Nov 19, 2013 at 20:29 UTC | |
by Corion (Patriarch) on Nov 19, 2013 at 21:09 UTC | |
A reply falls below the community's threshold of quality. You may see it by logging in. |
Back to
Code Catacombs