Paste: Excel column headers

Author: missingfaktor
Mode: factor
Date: Thu, 22 Dec 2011 13:52:41
Plain Text |
! A simple program that generates Excel spreadsheet column id corresponding to given column number.
! For example:
!   14 - N
!   26 - Z
!   27 - AA
!   29 - AC
USING: kernel math math.ranges sequences strings ;
IN: excel-columns

! Use the following after 'alphabet' definition to make it a compile time constant.
!   << \ alphabet alphabet define-constant >>
: alphabet    (   -- seq  ) CHAR: A CHAR: Z [a,b] ;
: nth-char    ( n -- char ) 25 + 26 mod alphabet nth 1string ;
: break-26    ( m -- n o  ) 26 [ /i ] [ mod ] 2bi ;
: column-id   ( i -- str  ) dup 26 <= [ nth-char ] [ break-26 [ column-id ] bi@ append ] if ;

: test-run    (   --      ) 1000 [1,b] [ dup column-id 2array . ] each

MAIN: test-run

New Annotation

Summary:
Author:
Mode:
Body: