Skip to content

Commit

Permalink
Fix to xl_rowcol_to_cell() to handle Excel2007 extended column values.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcnamara committed Jan 5, 2011
1 parent 669d557 commit c4ec147
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions lib/Spreadsheet/WriteExcel/Utility.pm
Expand Up @@ -197,23 +197,27 @@ sub xl_rowcol_to_cell {
my $col = $_[1];
my $row_abs = $_[2] ? '$' : '';
my $col_abs = $_[3] ? '$' : '';
my $col_str = '';

# Change from 0-indexed to 1 indexed.
$row++;
$col++;

my $int = int ($col / 26);
my $frac = $col % 26;

my $chr1 =''; # Most significant character in AA1
while ( $col ) {
# Set remainder from 1 .. 26
my $remainder = $col % 26 || 26;

if ($int > 0) {
$chr1 = chr( ord('A') + $int -1 );
}
# Convert the $remainder to a character. C-ishly.
my $col_letter = chr( ord( 'A' ) + $remainder - 1 );

my $chr2 = chr( ord('A') + $frac );
# Accumulate the column letters, right to left.
$col_str = $col_letter . $col_str;

# Zero index to 1-index
$row++;
# Get the next order of magnitude.
$col = int( ( $col - 1 ) / 26 );
}

return $col_abs . $chr1 . $chr2 . $row_abs. $row;
return $col_abs . $col_str . $row_abs . $row;
}


Expand Down Expand Up @@ -252,7 +256,7 @@ sub xl_cell_to_rowcol {

my $cell = shift;

$cell =~ /(\$?)([A-I]?[A-Z])(\$?)(\d+)/;
$cell =~ /(\$?)([A-Z]{1,3})(\$?)(\d+)/;

my $col_abs = $1 eq "" ? 0 : 1;
my $col = $2;
Expand Down Expand Up @@ -923,7 +927,7 @@ John McNamara jmcnamara@cpan.org
=head1 COPYRIGHT
© MM-MMX, John McNamara.
MM-MMX, John McNamara.
All Rights Reserved. This module is free software. It may be used, redistributed and/or modified under the same terms as Perl itself.
Expand Down

0 comments on commit c4ec147

Please sign in to comment.