]> git.donarmstrong.com Git - bin.git/commitdiff
add convert to xls command
authorDon Armstrong <don@donarmstrong.com>
Mon, 28 Apr 2008 22:23:13 +0000 (22:23 +0000)
committerDon Armstrong <don@donarmstrong.com>
Mon, 28 Apr 2008 22:23:13 +0000 (22:23 +0000)
convert_to_xls [new file with mode: 0755]

diff --git a/convert_to_xls b/convert_to_xls
new file mode 100755 (executable)
index 0000000..ff6bd6d
--- /dev/null
@@ -0,0 +1,95 @@
+#! /usr/bin/perl
+# conver_to_xls converts tab separated files to xls and is released
+# under the terms of the GPL version 2, or any later version, at your
+# option. See the file README and COPYING for more information.
+# Copyright 2008 by Don Armstrong <don@donarmstrong.com>.
+# $Id: perl_script 1153 2008-04-08 00:04:20Z don $
+
+
+use warnings;
+use strict;
+
+use Getopt::Long;
+use Pod::Usage;
+
+=head1 NAME
+
+convert_to_xls - convert tab separated files to xls
+
+=head1 SYNOPSIS
+
+convert_to_xls tsv_file1 [tsv_file2] [options] > foo.xls
+
+ Options:
+  --debug, -d debugging level (Default 0)
+  --help, -h display this help
+  --man, -m display manual
+
+=head1 OPTIONS
+
+=over
+
+=item B<--debug, -d>
+
+Debug verbosity. (Default 0)
+
+=item B<--help, -h>
+
+Display brief useage information.
+
+=item B<--man, -m>
+
+Display this manual.
+
+=back
+
+=head1 EXAMPLES
+
+
+=cut
+
+use Spreadsheet::WriteExcel;
+use IO::File;
+
+use vars qw($DEBUG);
+
+my %options = (debug           => 0,
+              help            => 0,
+              man             => 0,
+              );
+
+GetOptions(\%options,
+          'debug|d+','help|h|?','man|m');
+
+pod2usage() if $options{help};
+pod2usage({verbose=>2}) if $options{man};
+
+$DEBUG = $options{debug};
+
+my @USAGE_ERRORS;
+if (not @ARGV) {
+     push @USAGE_ERRORS,"You must give at least one file";
+}
+
+pod2usage(join("\n",@USAGE_ERRORS)) if @USAGE_ERRORS;
+
+my @FILES=@ARGV;
+@ARGV=();
+
+my $wb = Spreadsheet::WriteExcel->new(\*STDOUT) or die "Unable to create new spreadsheet";
+for my $file (@FILES) {
+     my $fh = IO::File->new($file,'r') or die "Unable to open $file for reading: $!";
+     my $ws = $wb->add_worksheet($file) or die "Unable to create new worksheet";
+     my $row = 1;
+     while (<$fh>) {
+         chomp;
+         my @row = map {s/^"//; s/"$//; $_} split /\t/,$_;
+         for my $col (0..$#row) {
+              $ws->write($row,$col+1,$row[$col]);
+         }
+     }
+}
+$wb->close;
+
+
+__END__