X-Git-Url: https://git.donarmstrong.com/?p=bin.git;a=blobdiff_plain;f=txt2xls;h=c79fb53b2d1c2ff63594602f14c280b34ac97cbc;hp=f6efa3a6300c5112418ba65b964372b89284f317;hb=4621d23526ccf936d1ad076ece0bc663510dfc52;hpb=c2be9416877fb4459cacacb4ab197d455d3c70b5 diff --git a/txt2xls b/txt2xls index f6efa3a..c79fb53 100755 --- a/txt2xls +++ b/txt2xls @@ -101,7 +101,8 @@ GetOptions(\%options, 'sci_format|sci-format=s', 'max_digits|max-digits=i', 'rmode|r-mode|r!', - 'remove_name|remove-name=s@', + 'remove_name|remove-name=s@', + 'output|output_file|output-file=s', 'debug|d+','help|h|?','man|m'); pod2usage() if $options{help}; @@ -132,9 +133,20 @@ elsif ($options{ssv}) { $sep_char = ' '; } +if (not @{$options{remove_name}}) { + $options{remove_name} = ['.+\/', + ]; +} + my %wb_formats = (); my $csv = Text::CSV->new({sep_char=>$sep_char}); -my $wb = Spreadsheet::WriteExcel->new(\*STDOUT); +my $wb; +if (defined $options{output}) { + $wb = Spreadsheet::WriteExcel->new($options{output}); +} else { + $wb = Spreadsheet::WriteExcel->new(\*STDOUT); +} + for my $file (@ARGV) { my $fh; if (not defined $file) { @@ -142,12 +154,12 @@ for my $file (@ARGV) { $file = "STDIN"; } else { - $fh = IO::File->new($file,'r') or + open($fh,'<:encoding(utf8)',$file) or die "Unable to open $file for reading: $!"; } my $ws_name = $file; foreach my $remove (@{$options{remove_name}}) { - $ws_name =~ s{\Q$remove\E}{}g; + $ws_name =~ s{$remove}{}g; } $ws_name =~ s{\.[^\.]+$}{}g; $ws_name =~ s/_+/ /g; @@ -165,7 +177,7 @@ for my $file (@ARGV) { while (<$fh>) { chomp; # parse the line - die "Unable to parse line $. of $file" unless $csv->parse($_); + die "Unable to parse line $. of $file: ".$csv->error_diag() unless $csv->parse($_); my @row = $csv->fields(); if ($row==1 and not $r_mode_autodetected) { @header_row = @row; @@ -187,7 +199,8 @@ for my $file (@ARGV) { my $t_ws_name = $ws_name; my $maxlen = 31-length('.'.$overflow); $t_ws_name =~ s{^(.{0,$maxlen}).*$}{$1}; - $ws = $wb->add_worksheet($ws_name.'.'.$overflow); + $ws = $wb->add_worksheet($t_ws_name.'.'.$overflow); + $overflow++; $row=1; output_row(\@header_row,$row,$ws,$wb,\%wb_formats,\%options); $row++;