| 1 | package Number::Base::SpreadsheetColumn; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use 5.010001; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use Exporter qw(import); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 2 | our @EXPORT_OK = qw( | |||||||||||||||||||||||||||
| 9 | from_scbase | ||||||||||||||||||||||||||||
| 10 | to_scbase | ||||||||||||||||||||||||||||
| 11 | ); | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 2 | our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY | |||||||||||||||||||||||||||
| 14 | 2 | our $DATE = '2026-02-02'; # DATE | |||||||||||||||||||||||||||
| 15 | 2 | our $DIST = 'Number-Base-SpreadsheetColumn'; # DIST | |||||||||||||||||||||||||||
| 16 | 2 | our $VERSION = '0.001'; # VERSION | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 7 | ▶ | sub from_scbase { | ||||||||||||||||||||||||||
|
Subroutine
from_scbase: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 19 | 7 | my $str = uc shift; | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 7 | ▶ | die "Please only use letters A-Z" if $str =~ /[^A-Z]/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 7 | my $res = 0; | |||||||||||||||||||||||||||
| 24 | 7 | for my $char (split //, $str) { | |||||||||||||||||||||||||||
| 25 | 13 | $res = $res * 26 + (ord($char) - ord('A') + 1); | |||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||
| 27 | 7 | $res-1; | |||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 7 | ▶ | sub to_scbase { | ||||||||||||||||||||||||||
|
Subroutine
to_scbase: called CC=4 SCAR=14.1
Pod
documented
| |||||||||||||||||||||||||||||
| 31 | 7 | my $num = shift; | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 7 | ▶ | die "Currently can't handle fraction or negative number!" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$num < 0 or $num != int $num
MC/DC0%0/2
$num < 0 or $num != int $num
$num < 0
$num != int $num
Truth table33%1/3
A or B
A= $num < 0B= $num != int $num
| |||||||||||||||||||||||||||||
| 34 | if $num < 0 || $num != int($num); | ||||||||||||||||||||||||||||
| 35 | 7 | $num = int($num); | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 7 | my $res = ""; | |||||||||||||||||||||||||||
| 38 | 7 | ▶ | while ($num >= 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | 13 | my $remainder = $num % 26; | |||||||||||||||||||||||||||
| 40 | 13 | my $letter = chr($remainder + ord('A')); | |||||||||||||||||||||||||||
| 41 | #say "D:num=<$num>, remainder=<$remainder>, letter=<$letter>"; | ||||||||||||||||||||||||||||
| 42 | 13 | $res = $letter . $res; | |||||||||||||||||||||||||||
| 43 | 13 | $num = int($num / 26) - 1; | |||||||||||||||||||||||||||
| 44 | #say "D:num=<$num>"; | ||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | 7 | $res; | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 2 | 1; | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | # ABSTRACT: Convert spreadsheet column name (e.g. "A", "Z", "AA") to number (e.g. 0, 25, 26) and vice versa | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | __END__ | ||||||||||||||||||||||||||||