Accessing SpreadSheet Data
#!/usr/bin/perl
use warnings;
use strict;
use OpenOffice::OODoc;
my $file = $ARGV[0];
unless(-r $file) {
print "Could not open unikey list at $file\n";
exit 1;
}
my $doc = odfDocument(file => $file);
&printTableDetails();
sub printTableDetails() {
my @tables = $doc->getTableList();
foreach (@tables) {
my $name = $_->{'att'}->{'table:name'};
#next unless ($name eq "Memberships");
print "$name\n";
my ($rows, $cols) = &getTableSize($name);
print " Size: $rows, $cols\n";
foreach my $y (0 .. $rows -1) {
my @rs = $doc->getRowCells($name, $y);
foreach (@rs) {
my $val = $doc->getCellValue($_);
print "$val,";
}
print "\n";
}
}
}
sub getTableSize() {
my ($table) = @_;
my $rows = &getRowCount($doc, $table);
my $cols = &getColCount($doc, $table);
return ($rows, $cols);
}
sub getRowCount() {
my ($doc, $table) = @_;
unless ($doc && $table) {
print "Usage: getRowCount('doc', 'tablename')\n";
return;
}
my ($maxrows, $maxcols) = $doc->getTableSize($table);
my $i = 0;
my $inc = 500;
my $prev = $i;
while () {
my $val = $doc->getCellValue($table, $i, 0);
last if (!$val && $inc == 1);
unless ($val) {
$inc = 1 if ($inc == 5);
$inc = 5 if ($inc == 10);
$inc = 10 if ($inc == 50);
$inc = 50 if ($inc == 100);
$inc = 100 if ($inc == 500);
$i = $prev;
}
last if ($i > $maxrows);
$prev = $i;
$i += $inc;
}
return $i;
}
sub getColCount() {
my ($doc, $table) = @_;
unless ($doc && $table) {
print "Usage: getColCount('doc', 'tablename')\n";
return;
}
my ($maxrows, $maxcols) = $doc->getTableSize($table);
my $i = 0;
while () {
my $val = $doc->getCellValue($table, 0, $i);
last unless ($val);
last if ($i > $maxcols);
$i++;
}
return $i;
}
sub printHash() {
my ($hash) = @_;
foreach my $key (keys(%$hash)) {
print "$key -> $hash->{$key}\n";
}
}