236
  6   ˆl–ß=¿Jµ!®Å ‰@½q�ÜmÅ1hß�þZ‚³Q’Q"+²€ÏÃ„ÏºïÂÁÀ ?÷     #!/usr/bin/perl -w
# $Id: exceltex 155 2006-04-30 11:25:16Z pez $
#
# helper script for the exceltex latex package. reads contents of
# M$ Excel files using the Spreadsheet::ParseExcel perl module.
#
# (c) 2004-2006 by Hans-Peter Doerr <doerr@cip.physik.uni-freiburg.de>
#
# exceltex is free software. you can redistribute or modify it under
# the terms of the GNU GENERAL PUBLIC LICENSE Version 2. See COPYING for
# details.

use strict;

my $VERSION = "0.5.1";
my $DEBUG   = 0;

# this is the default encoding for all files written by exceltex
my $ENCODING = "latin1";

# some lookup tables
my %L2N;
{ my $i = 1; for ('A' .. 'Z') { $L2N{$_} = $i; ++$i } }

my @N2L;
{ for ('A' .. 'Z') { push(@N2L, $_) } }


#XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
#XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
package util;

#:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
sub DEBUG
{
    my ($msg) = @_;

    if ($DEBUG)
    {
	chomp($msg);
	print STDERR "DEBUG: $msg\n";
    }
}


#:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
sub cell2xy
{
    my ($cell) = @_;

    $cell = uc($cell);
    return undef if (! ($cell =~ m/^([A-Z]+)([0-9]+)$/));

    # return (column, row)
    return (col2x($1), $2-1);
}


#:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
sub xy2cell
{
    my ($x, $y) = @_;

    # is is likely to be slow as hell
    return undef if (! ($x =~ m/^[0-9]+$/g && $y =~ m/^[0-9]+$/g));

    ++$y;
    return x2col($x) .  "$y";
}


#:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
sub col2x
{
  my ($l) = @_;

    my $len = length($l);
    my @S   = split //, $l;

    my $n = 0;

    if ($len == 1) { $n =  $L2N{$l} - 1 }
    if ($len == 2) { $n =  26 * $L2N{$S[0]} + $L2N{$S[1]} - 1 }

    return $n;
}


#:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
sub x2col
{
    my ($n) = @_;

    return undef if (! $n =~ m/[0-9]+/);
    my $l;
    if ($n > 25) { $l =  $N2L[int($n/26)-1] . $N2L[$n % 26] }
    else { $l =  $N2L[$n] }

    return $l;
}





#XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
#XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
package main;

use strict;
use Getopt::Long;
use Encode;

my $Workbooks = {};     # workbook cache
my $nItems    = 0;      # number of processed items
my $nCells    = 0;      # number of processed cells
my $nTabs     = 0;      # number of processed tabs
my $Warnings  = 0;      # number of warnings

my $Jobname    = "";    # jobname of .tex file
my $inputDir   = "";    # data goes here
my $indexFile  = "";


my $optFormat = 1;  # use formatting?
my $optColor  = 1;  # use colors?
my $optComma  = 0;  # use comma for decimals
my $optFmtSci = 0;  # reformat scientific numbers?
my $ignWarn   = 0;  # ignore warnings?
my $euroSymbol  = "EUR"; # use this for the EUR currency symbol

my $cleanup   = 0;      # cleanup inputDir

my $EXTNS = "excltx";

# read global and local config files
# will be overwritten by commandline options
readConfigFile("$ENV{HOME}/.exceltexrc") if (-f "$ENV{HOME}/.exceltexrc");
readConfigFile(".exceltexrc") if (-f ".exceltexrc");

# handle commandline switches
GetOptions('format!'       => \$optFormat,
	   'color!'        => \$optColor,
	   'comma!'        => \$optComma,
	   'reformat-sn!'  => \$optFmtSci,
	   'p|plain'       => sub{$optFormat=0; $optColor=0; $optFmtSci=0;},
	   'd|debug'       => \$DEBUG,
	   'c|cleanup'     => \$cleanup,
           'e|encoding=s'  => \$ENCODING,
           'o|euro-symbol=s' => \$euroSymbol,
	   'h|help'        => sub{ usage(); exit 0;},
	   'v|version'     => sub{ print "exceltex, version $VERSION\n"; exit 0},
	   'w|ignore-warnings' => \$ignWarn,
	   ) || exit 2;

if (@ARGV == 0) {
    usage();
    exit 2;
}



# encoding of output files
$ENCODING = lc($ENCODING);
if (! ($ENCODING eq "la