Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
namespace Sample
module ExcelUtil =
let baseNum = Seq.length <| seq {'A'..'Z'}
let convPad = int 'A' - 1
let a1AddressToIndex a1Address =
let len = String.length a1Address
if len < 1 then
invalidArg "a1Address" "入力は null または Empty 以外の文字列である必要があります。"
let addr = a1Address.ToUpper()
seq {len-1..-1..0}
|> Seq.mapi (fun i n ->
if addr.[n]<'A' || 'Z'<addr.[n] then
invalidArg "a1Address" "入力は英字だけを含む文字列である必要があります。"
(int addr.[n] - convPad) * pown baseNum i)
|> Seq.sum
let indexToA1Address index =
if index < 1 then invalidArg "index" "入力は正の数値である必要があります。"
let f x y = let x = x-1 in x%y+1, x/y
index
|> Seq.unfold (fun i -> if i > 0 then Some (f i baseNum) else None)
|> Seq.map (fun n -> n + convPad |> char |> string)
|> Seq.reduce (fun s1 s2 -> s2 + s1)
module Program =
open ExcelUtil
[<EntryPoint>]
let main _ =
let testCase = ["A",1; "B",2; "Z",26; "AA",27; "XFD",16384]
testCase
|> Seq.map (fun (x,y) -> a1AddressToIndex x, y)
|> Seq.forall (fun (x,y) -> x = y)
|> printfn "%b"
testCase
|> Seq.map (fun (x,y) -> x, indexToA1Address y)
|> Seq.forall (fun (x,y) -> x = y)
|> printfn "%b"
0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment