Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Get excel column letter from number

by Anonymous Monk
on Jul 26, 2016 at 11:12 UTC ( [id://1168554]=note: print w/replies, xml ) Need Help??


in reply to Get excel column letter from number

What's the maximum for column numbers? A simple routine for up to three letters:

sub ltr { ("A".."ZZZ")[@_] } print ltr(26);

Replies are listed 'Best First'.
Re^2: Get excel column letter from number
by duelafn (Parson) on Jul 26, 2016 at 13:10 UTC

    Simple and elegant given that Excel has a max column of "XFD". I'd modify this to cache the column names and throw a fit if we go too far (eventually, the column cap will be increased or removed):

    sub ltr_cached { state $ltr_cache = ["A".."ZZZ"]; return ($$ltr_cache[$_[0]] or die "column too big: $_[0]") }

    Good Day,
        Dean

Re^2: Get excel column letter from number
by pryrt (Abbot) on Jul 26, 2016 at 13:32 UTC

    For simplicity and perlness points, an extra ++ to this one... :-)

    Running haukex's test-and-benchmark with AM's ltr() included (without 285075=>'PERL' in the test list), ltr() appears about 10x more efficient than colname(). A four-Z-based ltr(), however, is about 75% less efficient than colname().

    Excel 2007 upped the limit from 2-letter/256-column to 3-letter/16384-column, and that hasn't changed as of Excel 2016, so the more-efficient ZZZ-based ltr() is sufficient for now. :-)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1168554]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-04-16 21:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found