LoginSignup
0
0

More than 1 year has passed since last update.

PowerShell でエクセルのアドレスが何列目なのか計算する関数

Posted at

コレ系のやつ何回作ってもどっかいって見つからないし、多分 $Profile のどこかにあると思うけど名前が思い出せない。

function Split-ExcelAddress {
    param([string]$Address)

    if ($Address -match ':') { throw 1 }

    $Address = $Address -replace '\$',''

    $a = $Address -replace '[0-9]',''
    $row = $Address -replace '[a-zA-Z]',''

    $a = $a.ToUpper()

    $col = 0
    $base = 1
    for($i = $a.Length; $i -gt 0; $i--){
        $col += ([int]($a[$i-1]) - 64) * $base
        $base = $base * 26
    }

    return [pscustomobject]@{
        Row = $row
        Col = $col
    }
}

テスト

Split-ExcelAddress -Address A1
# Row Col
# --- ---
# 1     1

Split-ExcelAddress -Address Z10
# Row Col
# --- ---
# 10   26

Split-ExcelAddress -Address AB13
# Row Col
# --- ---
# 13   28

Split-ExcelAddress -Address BA100
# Row Col
# --- ---
# 100  53

答え合わせは以下を参照した。大体あってると思う。

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