Last active
August 29, 2015 14:01
-
-
Save tsibley/9c7215572a48df66a959 to your computer and use it in GitHub Desktop.
explode_csv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use Module::Runtime qw< require_module >; | |
use Text::CSV; | |
my $filename = $ARGV[0]; | |
my $parser_class = $filename =~ /\.xlsx$/ | |
? "Spreadsheet::ParseXLSX" | |
: "Spreadsheet::ParseExcel"; | |
require_module($parser_class); | |
my $parser = $parser_class->new; | |
my $wb = $parser->parse($filename) || die $parser->error; | |
(my $dir = $filename) =~ s/\.[^.]*$//; | |
mkdir $dir; | |
chdir $dir; | |
for my $ws ($wb->worksheets) { | |
my $csv = Text::CSV->new({ binary => 1, eol => "\n" }); | |
my $filename = $ws->get_name . '.csv'; | |
open my $fh, '>:encoding(UTF-8)', $filename | |
or die "Couldn't open $filename: $!"; | |
my ($rmin, $rmax) = $ws->row_range; | |
my ($cmin, $cmax) = $ws->col_range; | |
for my $row (0..$rmin - 1) { | |
$csv->print($fh, [ ('') x ($cmax + 1) ]); | |
} | |
for my $row ($rmin..$rmax) { | |
my @row = (('') x $cmin); | |
for my $col ($cmin..$cmax) { | |
my $cell = $ws->get_cell($row, $col); | |
push @row, $cell ? $cell->value : ''; | |
} | |
$csv->print($fh, \@row); | |
} | |
close $fh | |
or die "Couldn't close $filename: $!"; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
From f5f6a43ab262f3d2245a59f4add6342023d490b7 Mon Sep 17 00:00:00 2001 | |
From: Thomas Sibley <trsibley@uw.edu> | |
Date: Mon, 5 May 2014 12:50:41 -0700 | |
Subject: [PATCH] Seamless .xlsx support for explode_csv | |
--- | |
explode_csv | 9 +++++++-- | |
1 file changed, 7 insertions(+), 2 deletions(-) | |
diff --git a/explode_csv b/explode_csv | |
index d32f5a9..8f50ebc 100755 | |
--- a/explode_csv | |
+++ b/explode_csv | |
@@ -2,12 +2,17 @@ | |
use strict; | |
use warnings; | |
-use Spreadsheet::ParseExcel; | |
+use Module::Runtime qw< require_module >; | |
use Text::CSV; | |
my $filename = $ARGV[0]; | |
+my $parser_class = $filename =~ /\.xlsx$/ | |
+ ? "Spreadsheet::ParseXLSX" | |
+ : "Spreadsheet::ParseExcel"; | |
-my $parser = Spreadsheet::ParseExcel->new; | |
+require_module($parser_class); | |
+ | |
+my $parser = $parser_class->new; | |
my $wb = $parser->parse($filename) || die $parser->error; | |
(my $dir = $filename) =~ s/\.[^.]*$//; | |
-- | |
1.8.5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment