[R-gregmisc-users] SF.net SVN: r-gregmisc:[1392] trunk/gdata/inst/perl
Brought to you by:
warnes
From: <wa...@us...> - 2010-01-24 07:49:23
|
Revision: 1392 http://r-gregmisc.svn.sourceforge.net/r-gregmisc/?rev=1392&view=rev Author: warnes Date: 2010-01-24 07:49:16 +0000 (Sun, 24 Jan 2010) Log Message: ----------- Combine sheetCount.pl and sheetNames.pl and modify to support Excel 2007 'xlsx' format Modified Paths: -------------- trunk/gdata/inst/perl/sheetCount.pl trunk/gdata/inst/perl/xls2csv.pl Added Paths: ----------- trunk/gdata/inst/perl/sheetNames.pl Removed Paths: ------------- trunk/gdata/inst/perl/sheetNames.pl Modified: trunk/gdata/inst/perl/sheetCount.pl =================================================================== --- trunk/gdata/inst/perl/sheetCount.pl 2010-01-24 07:26:58 UTC (rev 1391) +++ trunk/gdata/inst/perl/sheetCount.pl 2010-01-24 07:49:16 UTC (rev 1392) @@ -2,23 +2,48 @@ BEGIN { use File::Basename; -unshift(@INC, dirname $0); +# Add current path to perl library search path +use lib dirname($0); } use strict; use Spreadsheet::ParseExcel; +use Spreadsheet::XLSX; +use File::Spec::Functions; # declare some varibles local -my($row, $col, $sheet, $cell, $usage, $basename, $sheetnumber, $filename); +my($row, $col, $sheet, $cell, $usage, + $filename, $volume, $directories, $whoami, + $basename, $sheetnumber, $filename, + $text); + ## +## Figure out whether I'm called as sheetCount.pl or sheetNames.pl +## +($volume,$directories,$whoami) = File::Spec->splitpath( $0 ); + +if($whoami eq "sheetCount.pl") + { + $text="number"; + } +elsif ($whoami eq "sheetNames.pl") + { + $text="names"; + } +else + { + die("This script is named '$whoami', but must be named either 'sheetCount.pl' or 'sheetNames.pl' to function properly.\n"); + } + +## ## Usage information ## $usage = <<EOF; sheetCount.pl <excel file> -Output is the number of sheets in the excel file. +Output is the $text of sheets in the excel file. EOF @@ -38,12 +63,44 @@ ## open spreadsheet ## -my $oExcel = new Spreadsheet::ParseExcel; - open(FH, "<$fileName") or die "Unable to open file '$fileName'.\n"; close(FH); -my $oBook = $oExcel->Parse($fileName); +my $oBook; -print $oBook->{SheetCount} , "\n"; +## First try as a Excel 2007+ 'xml' file +eval + { + local $SIG{__WARN__} = sub {}; + $oBook = Spreadsheet::XLSX -> new ($ARGV[0]); + }; +## Then Excel 97-2004 Format +if($@) + { + $oBook = new Spreadsheet::ParseExcel->Parse($ARGV[0]) or \ + die "Error parsing file '$ARGV[0]'.\n"; + } + +if($whoami eq "sheetCount.pl") + { + print $oBook->{SheetCount} , "\n"; + } +elsif ($whoami eq "sheetNames.pl") + { + ## Get list all worksheets in the file + my @sheetlist = (@{$oBook->{Worksheet}}); + + foreach my $sheet (@sheetlist) + { + print "\"$sheet->{Name}\" "; + } + + print "\n"; + } +else + { + die("This script is named '$whoami', but must be named either 'sheetCount.pl' or 'sheetNames.pl' to function properly.\n"); + } + + Deleted: trunk/gdata/inst/perl/sheetNames.pl =================================================================== --- trunk/gdata/inst/perl/sheetNames.pl 2010-01-24 07:26:58 UTC (rev 1391) +++ trunk/gdata/inst/perl/sheetNames.pl 2010-01-24 07:49:16 UTC (rev 1392) @@ -1,57 +0,0 @@ -#!/usr/bin/perl - -BEGIN { -use File::Basename; -unshift(@INC, dirname $0); -} - -use strict; -use Spreadsheet::ParseExcel; - -# declare some varibles local -my($row, $col, $sheet, $cell, $usage, $basename, $sheetnumber, $filename); - -## -## Usage information -## -$usage = <<EOF; - -sheetCount.pl <excel file> - -Output is the number of sheets in the excel file. - -EOF - -## -## parse arguments -## - -if(!defined($ARGV[0])) - { - print $usage; - exit 1; - } - -my $fileName=$ARGV[0]; - -## -## open spreadsheet -## - -my $oExcel = new Spreadsheet::ParseExcel; - -open(FH, "<$fileName") or die "Unable to open file '$fileName'.\n"; -close(FH); - -my $oBook = $oExcel->Parse($fileName); - -## Get list all worksheets in the file -my @sheetlist = (@{$oBook->{Worksheet}}); - -foreach my $sheet (@sheetlist) -{ - print "\"$sheet->{Name}\" "; -} - -print "\n"; - Added: trunk/gdata/inst/perl/sheetNames.pl =================================================================== --- trunk/gdata/inst/perl/sheetNames.pl (rev 0) +++ trunk/gdata/inst/perl/sheetNames.pl 2010-01-24 07:49:16 UTC (rev 1392) @@ -0,0 +1 @@ +link sheetCount.pl \ No newline at end of file Property changes on: trunk/gdata/inst/perl/sheetNames.pl ___________________________________________________________________ Added: svn:special + * Modified: trunk/gdata/inst/perl/xls2csv.pl =================================================================== --- trunk/gdata/inst/perl/xls2csv.pl 2010-01-24 07:26:58 UTC (rev 1391) +++ trunk/gdata/inst/perl/xls2csv.pl 2010-01-24 07:49:16 UTC (rev 1392) @@ -122,13 +122,13 @@ close(FH); print "Loading '$ARGV[0]'...\n"; - ## First try as a Excel 2007+ 'xml' file eval { local $SIG{__WARN__} = sub {}; $oBook = Spreadsheet::XLSX -> new ($ARGV[0]); }; +## Then Excel 97-2004 Format if($@) { $oBook = new Spreadsheet::ParseExcel->Parse($ARGV[0]) or \ This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |