0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【powershell】 10進数と26進数を相互変換して Excel を扱いやすくする

Last updated at Posted at 2019-10-12

powershell でもやってみました。コンソールから Excel を操作する ようなときに使えます。

26進数 → 10進数

function Convert-Colname2Int ([string]$s) {
    $alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    $s = $s.ToUpper()
    $ret = 0
    for ($i = 0; $i -lt $s.Length; $i++) {
        $ret = $ret*26 + $alphabet.IndexOf($s[$i])+1
    }
    return $ret
}

# Alias
Set-Alias colname2int Convert-Colname2Int

10進数 → 26進数

function Convert-Int2Colname ([int]$i) {
    if ($i -le 0) {
        return
    }

    if ($i -le 26) {
        return ([char]($i + 64)).ToString()
    }

    if (($i % 26) -eq 0) {
        $alphabetIndex = 26
    }
    else {
        $alphabetIndex = $i % 26
    }

    $unitOfAtoZ = [math]::Floor(($i - $alphabetIndex) / 26)
    return ("{0}{1}" -f (int2colname $unitOfAtoZ), ([char]($alphabetIndex + 64)).ToString())
}

# Alias
Set-Alias int2colname Convert-Int2Colname

動作

PS > colname2int A
1

PS > colname2int zx
700

PS > int2colname 1
A

PS > int2colname 700
ZX

PS > 1..30|foreach {int2colname $_}
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y
Z
AA
AB
AC
AD

うまく行っている模様。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?