Skip to content

Commit

Permalink
Item8177: Handling excel tables without having a foswiki form defined
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.foswiki.org/trunk/ExcelImportExportPlugin@4062 0b4bb1d4-4e5a-0410-9cc4-b2b747904278
  • Loading branch information
SvenHess authored and SvenHess committed Jun 9, 2009
1 parent adce00f commit ef20062
Showing 1 changed file with 187 additions and 63 deletions.
250 changes: 187 additions & 63 deletions lib/Foswiki/Plugins/ExcelImportExportPlugin/Import.pm
Expand Up @@ -2,6 +2,9 @@
#
# (c) 2006 Motorola, thomas.weigert@motorola.com
# (c) 2006 Foswiki:Main.ClausLanghans
# Adding Ability to generate tables without defining a foswiki
# form first - by Sven Hess, shess@seibert-media.net
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
Expand Down Expand Up @@ -336,7 +339,13 @@ Generate a TML table from an Excel attachment.

sub excel2table {
my ( $session, $params, $topic, $webName ) = @_;


my $log = '';
my $form;
my $fieldDefs;
my $form_defined;
my $table;

my %config = ();
$config{UPLOADFILE} = $params->{"_DEFAULT"} || $params->{file} || $topic;
$config{UPLOADTOPIC} = $params->{topic} || $topic;
Expand All @@ -347,13 +356,15 @@ sub excel2table {
$Foswiki::Plugins::SESSION->normalizeWebTopicName( $webName,
$config{UPLOADTOPIC} );

my $log = '';


my $xlsfile = $Foswiki::cfg{PubDir}
. "/$config{UPLOADWEB}/$config{UPLOADTOPIC}/$config{UPLOADFILE}.xls";
$log .=
" attachment file=$config{UPLOADWEB}/$config{UPLOADTOPIC}/$config{UPLOADFILE}\n";

my $attachedfile = "$config{UPLOADWEB}/$config{UPLOADTOPIC}/$config{UPLOADFILE}.xls";

my $Book = Spreadsheet::ParseExcel::Workbook->Parse($xlsfile);
if ( not defined $Book ) {
throw Foswiki::OopsException(
Expand All @@ -363,89 +374,202 @@ sub excel2table {
topic => $_[1],
params => [ 'Cannot read file ', $xlsfile, '', '' ]
);

}

my $form = new Foswiki::Form( $session, $webName, $config{FORM} );
my $fieldDefs = $form->{fields};
my $table = '|';
foreach my $field ( @{$fieldDefs} ) {
$table .= '*' . $field->{title} . '*|';

# check if template param is set
if($config{FORM} eq "") {
$form_defined = 0;
}
$table .= "\n";

my %colname;
else {

$form = new Foswiki::Form( $session, $webName, $config{FORM} );
$fieldDefs = $form->{fields};

# check if form data is defined
if(@{$fieldDefs} == 0) {

$form_defined = 0;
$log .= "No form data defined\n";
}
else {

$form_defined = 1;

$table = '|';
foreach my $field ( @{$fieldDefs} ) {
$table .= '*' . $field->{title} . '*|';

}
$table .= "\n";
}
}

my %colname;
my $maxcols = 0;

foreach my $WorkSheet ( @{ $Book->{Worksheet} } ) {
Foswiki::Func::writeDebug(
"--------- SHEET:" . $WorkSheet->{Name} . "\n" )
if $config{DEBUG};

if($form_defined) {
for (
my $col = $WorkSheet->{MinCol} ;
defined $WorkSheet->{MaxCol} && $col <= $WorkSheet->{MaxCol} ;
$col++
)
{
my $cell = $WorkSheet->{Cells}[0][$col];
if ( defined $cell and $cell->Value ne '' ) {
$colname{$col} = $cell->Value if ($cell);
$log .= " Column $col = $colname{$col}\n";
}
}

for (
my $row = $WorkSheet->{MinRow} + 1 ;
defined $WorkSheet->{MaxRow} && $row <= $WorkSheet->{MaxRow} ;
$row++
)
{
my %data; # contains the row
my $line = '|';
for (
my $col = $WorkSheet->{MinCol} ;
defined $WorkSheet->{MaxCol} && $col <= $WorkSheet->{MaxCol} ;
$col++
)
{
my $cell = $WorkSheet->{Cells}[$row][$col];
if ($cell) {
Foswiki::Func::writeDebug(
"( $row , $col ) =>" . $cell->Value . "\n" )
if $config{DEBUG};
$data{ $colname{$col} } = $cell->Value;
}
}

# Generating the table

foreach my $field ( @{$fieldDefs} ) {
my $foundIt = 0;

# search through all columns and find that with the name of the field
foreach my $colname ( values %colname ) {

if ( $field->{title} eq $colname ) {
my $msg =
" ( $row , $colname ) => $data{$colname}";
Foswiki::Func::writeDebug($msg) if $config{DEBUG};
$log .= "$msg\n";

# replace CR/LF and "
$data{$colname} =~ s/(\r*\n|\r)/<br \/>/gos;
$data{$colname} =~ s/\|/\&\#124;/gos;
$line .= ' ' . $data{$colname} . ' |';
$foundIt = 1;
last; # found the field
}
}
$line .= ' |' unless $foundIt;
}

$line .= "\n";
$table .= $line;
}
}
# no form data has been defined:
# all tables in spreadsheet to wiki tables
# more than 1 empty rows initialize new table
else {

$maxcols = $WorkSheet->{MaxCol};
my $theader = 1;
my $emptyRow = 0;

# loop through all rows
for (
my $col = $WorkSheet->{MinCol} ;
defined $WorkSheet->{MaxCol} && $col <= $WorkSheet->{MaxCol} ;
$col++
)
{
my $cell = $WorkSheet->{Cells}[0][$col];
if ( defined $cell and $cell->Value ne '' ) {
$colname{$col} = $cell->Value if ($cell);
$log .= " Column $col = $colname{$col}\n";
}
}

for (
my $row = $WorkSheet->{MinRow} + 1 ;
my $row = $WorkSheet->{MinRow};
defined $WorkSheet->{MaxRow} && $row <= $WorkSheet->{MaxRow} ;
$row++
)
{
my %data; # contains the row
my $line = '|';
my $line = "";
my $emptyCol = 0;

# loop through all colums
for (
my $col = $WorkSheet->{MinCol} ;
defined $WorkSheet->{MaxCol} && $col <= $WorkSheet->{MaxCol} ;
$col++
)
{
my $cell = $WorkSheet->{Cells}[$row][$col];
if ($cell) {
Foswiki::Func::writeDebug(
"( $row , $col ) =>" . $cell->Value . "\n" )
if $config{DEBUG};
$data{ $colname{$col} } = $cell->Value;
if(not defined $cell) {
$line .= "| ";
$emptyCol++;
next;
}
}

# Generating the table

foreach my $field ( @{$fieldDefs} ) {
my $foundIt = 0;

# search through all columns and find that with the name of the field
foreach my $colname ( values %colname ) {

if ( $field->{title} eq $colname ) {
my $msg =
" ( $row , $colname ) => $data{$colname}";
Foswiki::Func::writeDebug($msg) if $config{DEBUG};
$log .= "$msg\n";

# replace CR/LF and "
$data{$colname} =~ s/(\r*\n|\r)/<br \/>/gos;
$data{$colname} =~ s/\|/\&\#124;/gos;
$line .= ' ' . $data{$colname} . ' |';
$foundIt = 1;
last; # found the field
}

$emptyCol = 0;
my $value = $cell->Value;

# replace CR/LF and "
$value =~ s/(\r*\n|\r)/<br \/>/gos;
$value =~ s/\|/\&\#124;/gos;

# not a nice way to substitute euro symbol
$value =~ s/\[\$.1\]/&euro;/gos;

if ($config{DEBUG}) {
Foswiki::Func::writeDebug("( $row , $col ) =>" . $value . "\n" );
}
$line .= ' |' unless $foundIt;

# Generating the table
if($theader) {
$line .= "| *".$value."* ";
}
else {
$line .= "| ".$value." ";
}
}

$line .= "\n";

if($emptyCol >= $maxcols) {
$emptyRow++;
}
else {
$emptyRow = 0;
}

$log .= $emptyCol;

# more than 2 empty rows initialize new table
if($emptyRow < 1) {
$line .= "|\n";
$theader = 0;
}
else {
$line = "\n" if($emptyRow == 1);
$line = "" if($emptyRow > 1);
$theader = 1;
}

$table .= $line;
}
last; # only the first sheet
}
}

# add filename
$table .= "data: $config{UPLOADFILE}.xls\n";

last; # only the first sheet
}


# error printing
if($table eq "") {
my $error = '<font color="red">error: '.$attachedfile.' not found.</font>';
return $error;
}

return $table;
}

Expand Down

0 comments on commit ef20062

Please sign in to comment.