summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2013-08-29 15:29:33 -0400
committerJesse Luehrs <doy@tozt.net>2013-08-29 15:29:33 -0400
commit6cca8025c28553cd1adbb2a54b43d64685664fb7 (patch)
tree1b9cfa8396519e2ca9f727009c9fb4b2af0b39c9
parentf79aa6392589db518f7292b89a4c8978f5c7c6a5 (diff)
downloadspreadsheet-template-6cca8025c28553cd1adbb2a54b43d64685664fb7.tar.gz
spreadsheet-template-6cca8025c28553cd1adbb2a54b43d64685664fb7.zip
fix this algorithm, again (jasonshaev)
-rw-r--r--lib/Spreadsheet/Template/Helpers/Xslate.pm13
-rw-r--r--t/cell-to-row-col.t27
2 files changed, 33 insertions, 7 deletions
diff --git a/lib/Spreadsheet/Template/Helpers/Xslate.pm b/lib/Spreadsheet/Template/Helpers/Xslate.pm
index 8712656..56df981 100644
--- a/lib/Spreadsheet/Template/Helpers/Xslate.pm
+++ b/lib/Spreadsheet/Template/Helpers/Xslate.pm
@@ -3,7 +3,6 @@ use strict;
use warnings;
use JSON;
-use POSIX;
my $JSON = JSON->new;
@@ -82,13 +81,13 @@ sub _cell_to_row_col {
my ($col, $row) = $cell =~ /([A-Z]+)([0-9]+)/;
- (my $ncol = $col) =~ tr/A-Z/1-9A-Q/;
- $ncol = POSIX::strtol($ncol, 27);
- $ncol -= 1;
+ my $ncol = 0;
+ for my $char (split //, $col) {
+ $ncol *= 26;
+ $ncol += ord($char) - ord('A') + 1;
+ }
- my $nrow = $row - 1;
-
- return [ $nrow, $ncol ];
+ return [ $row - 1, $ncol - 1 ];
}
sub _formats {
diff --git a/t/cell-to-row-col.t b/t/cell-to-row-col.t
new file mode 100644
index 0000000..c20432e
--- /dev/null
+++ b/t/cell-to-row-col.t
@@ -0,0 +1,27 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use Test::More;
+
+use Spreadsheet::Template::Helpers::Xslate;
+
+my %tests = (
+ A1 => [0, 0],
+ Z3 => [2, 25],
+ AA5 => [4, 26],
+ IV256 => [255, 255],
+ ZZ10 => [9, 701],
+ AAA8 => [7, 702],
+ XFD22 => [21, 16383],
+);
+
+for my $cell (sort keys %tests) {
+ # XXX not public API, but i'm lazy
+ is_deeply(
+ Spreadsheet::Template::Helpers::Xslate::_cell_to_row_col($cell),
+ $tests{$cell},
+ "correct value for $cell"
+ );
+}
+
+done_testing;