PerlSufi has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks, I have a script that queries the database and writes the information the an excel file. The issue is that I want to take a date at the command line when the script is run and write it to an entire excel column. When I run it, it gives me '211' just on the first cell in the column (D) that I want, and not the full date(ie. 20130729) through the whole column. I am running it like  perl script.pl 20130729
Here is most of my script:
use strict; use warnings; use diagnostics; use DBI; use DateTime; use Date::Format; use MIME::Entity; use Spreadsheet::ParseExcel; use Spreadsheet::WriteExcel; my $user_date = $ARGV[0]; my $database = "workflow"; my $host = "database.local"; my $user = "$user"; my $pw = "$pw"; my $dbh = DBI->connect("dbi:mysql:$database:$host", $user, $pw,) or di +e "Database connection FAILED: $DBI::errstr"; my $sth = $dbh->prepare( "SELECT * stuff"); $sth->execute(); my $xWB = Spreadsheet::WriteExcel->new('/path/to/file.xls'); my $xWS = $xWB->add_worksheet('sheet'); $xWS->write(0,0, 'Account'); $xWS->write(0,1, 'PDF Name'); $xWS->write(0,2,'Bill Number'); $xWS->write(0,3, 'Mailed Date'); $xWS->set_column('A:A', 18); $xWS->set_column('B:B', 25); $xWS->set_column('C:C', 12); $xWS->set_column('D:D', 12); while (my $ar = $sth->fetchrow_arrayref) { s{^.*/}{} for @$ar; ++$row, $col = 0; $xWS->write($row,$col++,$_) for @$ar; } #I want the total number of rows and write the user input date through + all rows in this column #$xWS->write("D2:D$row", $user_date); # 212 is the actual number of rows known by viewing the file $xWS->write('D2:D212', $user_date); $xWB->close(); $sth->finish(); $dbh->disconnect;
I also want to automatically get the total number of rows and write the user input date to all of column D, but I can get to that later. For now I need help understanding why the date is not writing. Any help is very appreciated !

Replies are listed 'Best First'.
Re: User input into Excel
by toolic (Bishop) on Aug 14, 2013 at 18:14 UTC
    Any help is very appreciated !
    When I comment out all of the db code, I can create an excel file with 20130729 in column D (cells 2 to 212) by changing:
    $xWS->write('D2:D212', $user_date);

    to:

    $xWS->write($_, 3, $user_date) for 1 .. 211;

    I don't think 'D2:D212' is a valid cell range specifier (see Spreadsheet::WriteExcel).

      yeah that was probably the reason, thanks a bunch toolic ++
Re: User input into Excel
by poj (Abbot) on Aug 14, 2013 at 19:16 UTC
    Why not write the date at the same time as the data ?
    $xWS->write($row,$col++,$_) for @$ar,$user_date;
    poj
      ah, duh! Thanks poj ! ++