Humbly, I seek the wisdom of all monkdom in support of my attempt to automate getting the date ("date" is correct) contained in Row 1 of the last populated-Column in an Excel spreadsheet, using Win32::OLE.

By way of background for those spared the pain of Excel, the alphanumeric designations for cells in the top row of an Excel spreadsheet are:   A1..Z1   followed by AA1..AZ1   followed by BA1..BZ1   and so on...

However, the standard (or so I'm led to believe) routine for identifying the last-populated-column,

$Win32::OLE::Warn = 3; # die ( UGLY ) on errors... my $Excel = Win32::OLE->new('Excel.Application', 'Quit'); my $Book = $Excel->Workbooks->Open($FN); my $Sheet = $Book->Worksheets(1); # select worksheet number 1 $LastCol = $Sheet->UsedRange->Find({What=>"*", SearchDirection=>xlPrevious, SearchOrder=>xlByColumns})->{Column};

... returns a decimal number (equal to the number of columns) in $LastCol).

That's a PITA, because Win32::OLE accepts ONLY an alphanumeric, as in this next snippet, to obtain the date itself from Row1, Last_Column,:

$lastdate = $Sheet->Range("CK1")->Win32::OLE::valof({'Value'}); print "Data is THROUGH $lastdate \n\n";

The above generates a warning "Odd number of elements in anonymous hash at getdrills_time3.pl" but I'm pretty sure that's irrelevant because I created that particular problem as I tried to golf this question but the rest of the output is quite satisfactory:     Data is THROUGH 11/17/2005

...whereas it gags when fed $LastCol:

$lastdate = $Sheet->Range($LastCol)->Win32::OLE::valof({'Value'}); print "Data is THROUGH $lastdate \n\n";

The output of the above is:

Win32::OLE(0.1702) error 0x800a03ec<br> in METHOD/PROPERTYGET "Range" at getdrills_time3.pl line 32

Hence, what I wish to do is convert the numeric value of $LastCol ( "89" is what Win32:0LE returns for the last_column of a spreadsheet with data in "CK" columns) to its equivalent alphanumeric designator, CK1, which could then be passed to the last previous code snippet in place of $LastCol.

Clearly, I could read in Row 1, the dates, instead of skipping it, and extract the last date using the (mostly) the same data_extraction techniques in the main body of the script which is far too long for inclusion (as if this weren't). I'm not happy with that, as I'm afraid I'll slow execution or create memory issues.

As an alternate, I have tinkered with creating a hash which I could then use to look up each (reasonable && possible) $Last_col value (for Row1 only) as a number and an alphanumeric, after which extracting the unique alphanumeric equivalent of a given number is simple enough
...but that too seems awkward, kludgy and apt to pain my successors as they flatten their foreheads.

So, having found nothing I recognized as a solution in Win32::Ole docs, faqs, etc ( including explanations of Win32::Ole::Variant ) and much else, I pray for guidance.

Update: 2 hours; 3 valuable answers to a l-o-o-n-g SOPW. Monastery ++
Now to try each, with thanks to John, bmann and traveler!

Further update (200511231145): Again, ++ each of you. For future SOPW,

$lastdate = $Sheet->Cells(1, $LastCol)->Win32::OLE::valof ({ 'Value'}) +; print "jcmcnamara's version: $lastdate \n";

The same applies to bmann's first alternative while his second suggestion appears to depend on having the data_file OPEN in Excel.

And FWIW, for the script I'm actually passing on to my Fire Department, I opted to incorporate traveler's version (again, modified with addition of the valof function) solely because it may be slightly clearer, even if more intimidating, to some future maintainer... which concern is given high priority because I see NO immediate prospect that we'll have another perl_person in the house, but would like the code clear enough to perhaps encourage someone. IOW, if a roof comes in on \me, I'm trying to make it easier for the Dept to find someone to deal with next year's changes... and those which follow.

CAUTION: re all the points in this update, YMMV!


In reply to LastCol of an Excel spreadsheet as an alphanumeric using Win32::Ole by ww

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.