/usr/lib/R/site-library/gdata/perl/xls2tab.pl is in r-cran-gdata 2.18.0-1.1.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 | #!/usr/bin/perl
BEGIN {
use File::Basename;
# Add current path to perl library search path
use lib dirname($0);
}
use strict;
#use Spreadsheet::ParseExcel;
#use Spreadsheet::ParseXLSX;
use File::Spec::Functions;
use Getopt::Std;
##
# Try to load the modules we need
##
require 'module_tools.pl';
my(
$HAS_Spreadsheet_ParseExcel,
$HAS_Compress_Raw_Zlib,
$HAS_Spreadsheet_ParseXLSX
) = check_modules_and_notify();
# declare some varibles local
my($row, $col, $sheet, $cell, $usage,
$targetfile,$basename, $sheetnumber,
$filename, $volume, $directories, $whoami,
$sep, $sepName, $sepLabel, $sepExt,
$skipBlankLines, %switches,
$parser, $oBook, $formatter,
$using_1904_date
);
##
## Figure out whether I'm called as xls2csv.pl or xls2tab.pl
##
($volume,$directories,$whoami) = File::Spec->splitpath( $0 );
if($whoami eq "xls2csv.pl")
{
$sep=",";
$sepName="comma";
$sepLabel="CSV";
$sepExt="csv";
}
elsif ($whoami eq "xls2tsv.pl")
{
$sep="\t";
$sepName="tab";
$sepLabel="TSV";
$sepExt="tsv";
}
elsif ($whoami eq "xls2tab.pl")
{
$sep="\t";
$sepName="tab";
$sepLabel="TAB";
$sepExt="tab";
}
else
{
die("This script is named '$whoami', but must be named 'xls2csv.pl', 'xls2tsv', or 'xls2tab.pl' to function properly.\n");
}
##
## Usage information
##
$usage = <<EOF;
$whoami [-s] <excel file> [<output file>] [<worksheet number>]
Translate the Microsoft Excel spreadsheet file contained in <excel
file> into $sepName separated value format ($sepLabel) and store in
<output file>, skipping blank lines unless "-s" is present.
If <output file> is not specified, the output file will have the same
name as the input file with '.xls', or 'xlsx' removed and '.$sepExt'
appended.
If no worksheet number is given, each worksheet will be written to
a separate file with the name '<output file>_<worksheet name>.$sepExt'.
EOF
##
## parse arguments
##
# Handle switches (currently, just -s)
getopts('s', \%switches);
$skipBlankLines=!$switches{s};
# Now the rest of the arguments
if( !defined($ARGV[0]) )
{
print $usage;
exit 1;
}
if( defined($ARGV[1]) )
{
$basename = $targetfile = $ARGV[1];
$basename =~ s/\.$sepExt$//i;
}
else
{
($volume,$directories,$basename) = File::Spec->splitpath( $ARGV[0] );
$basename =~ s/\.xlsx*//i;
}
my $targetsheetname;
my $sheetnumber;
if(defined($ARGV[2]) )
{
if ( $ARGV[2] =~ m|^\d+$| )
{
$sheetnumber = $ARGV[2];
die "Sheetnumber must be an integer larger than 0.\n" if $sheetnumber < 1;
}
else
{
$targetsheetname = $ARGV[2];
}
}
##
## open spreadsheet
##
my $oExcel;
my $oBook;
$oExcel = new Spreadsheet::ParseExcel;
$formatter = Spreadsheet::ParseExcel::FmtDefault->new();
open(FH, "<$ARGV[0]") or die "Unable to open file '$ARGV[0]'.\n";
close(FH);
print "\n";
print "Loading '$ARGV[0]'...\n";
## First try as a Excel 2007+ 'xml' file
eval
{
local $SIG{__WARN__} = sub {};
$parser = Spreadsheet::ParseXLSX -> new();
$oBook = $parser->parse ($ARGV[0]);
};
## Then Excel 97-2004 Format
if ( !defined $oBook )
{
$parser = Spreadsheet::ParseExcel -> new();
$oBook = $parser->parse($ARGV[0]) or \
die "Error parsing file '$ARGV[0]'.\n";
}
print "Done.\n";
## Does this file use 1904-01-01 as the reference date instead of
## 1900-01-01?
$using_1904_date = ( $oBook->using_1904_date() == 1 ) || # ParseExcel
( $oBook->{Flag1904} == 1 ); # ParseXLSX
## Show the user some summary information before we start extracting
## date
print "\n";
print "Orignal Filename: ", $ARGV[0], "\n";
print "Number of Sheets: ", $oBook->{SheetCount} , "\n";
if($using_1904_date)
{
print "Date reference : 1904-01-01\n";
}
else
{
print "Date reference : 1900-01-01\n";
}
print "\n";
## Get list all worksheets in the file
my @sheetlist = (@{$oBook->{Worksheet}});
my $sheet;
## If we want a specific sheet drop everything else
if ( defined($sheetnumber) )
{
$sheet = $oBook->Worksheet($sheetnumber-1) or die "No sheet number $sheetnumber.\n";
@sheetlist = ( $sheet );
}
elsif ( defined($targetsheetname) )
{
$sheet = $oBook->Worksheet($targetsheetname) or die "No sheet named '$targetsheetname'.\n";
@sheetlist = ( $sheet );
}
##
## iterate across each worksheet, writing out a separat csv file
##
my $i=0;
my $sheetname;
my $found=0;
foreach my $sheet (@sheetlist)
{
$i++;
$sheetname = $sheet->{Name};
if( defined($sheetnumber) || defined($targetsheetname) || $oBook->{SheetCount}==1 )
{
if( defined($targetfile) )
{
$filename = $targetfile;
}
else
{
$filename = "${basename}.$sepExt";
}
}
else
{
$filename = "${basename}_${sheetname}.$sepExt";
}
if( defined($sheetnumber) )
{
print "Writing sheet number $sheetnumber ('$sheetname') to file '$filename'\n";
}
elsif ( defined($targetsheetname) )
{
print "Writing sheet '$sheetname' to file '$filename'\n";
}
else
{
print "Writing sheet number $i ('$sheetname') to file '$filename'\n";
}
open(OutFile,">$filename");
my $cumulativeBlankLines=0;
my $minrow = $sheet->{MinRow};
my $maxrow = $sheet->{MaxRow};
my $mincol = $sheet->{MinCol};
my $maxcol = $sheet->{MaxCol};
print "Minrow=$minrow Maxrow=$maxrow Mincol=$mincol Maxcol=$maxcol\n";
for(my $row = $minrow; $row <= $maxrow; $row++)
{
my $outputLine = "";
for(my $col = $mincol; $col <= $maxcol; $col++)
{
my $cell = $sheet->{Cells}[$row][$col];
my $format = $formatter->FmtString($cell, $oBook);
if( defined($cell) )
{
if ($cell->type() eq "Date") # && $using_1904_date )
{
my $is_date = ( $format =~ m/y/ &&
$format =~ m/m/ &&
$format =~ m/d/ );
my $is_time = ( $format =~ m/h[:\]]*m/ ||
$format =~ m/m[:\]]*s/ );
if($is_date && $is_time)
{
$format = "yyyy-mm-dd hh:mm:ss.00";
}
elsif ($is_date)
{
$format = "yyyy-mm-dd";
}
elsif ($is_time)
{
$format = "hh:mm:ss.00"
}
$_ = ExcelFmt($format,
$cell->unformatted(),
$using_1904_date);
}
else
{
$_=$cell->value();
}
# convert '#NUM!' strings to missing (empty) values
s/#NUM!//;
# convert "#DIV/0!" strings to missing (emtpy) values
s|#DIV/0!||;
# escape double-quote characters in the data since
# they are used as field delimiters
s/\"/\\\"/g;
}
else
{
$_ = '';
}
$outputLine .= "\"" . $_ . "\"" if(length($_)>0);
# separate cells with specified separator
$outputLine .= $sep if( $col != $maxcol) ;
}
# skip blank/empty lines
if( $skipBlankLines && ($outputLine =~ /^[$sep ]*$/) )
{
$cumulativeBlankLines++
}
else
{
print OutFile "$outputLine\n"
}
}
close OutFile;
print " (Ignored $cumulativeBlankLines blank lines.)\n"
if $skipBlankLines;
print "\n";
}
|