Created
March 17, 2019 09:08
-
-
Save mitu217/42fd57094bfcf89ec27c47a1fc0ffd3d to your computer and use it in GitHub Desktop.
カラム番号をスプレッドシートとかのカラム定義に変換するやつ(非推奨。R1C1形式を使うこと)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func columnIndexFromGenericToSpreadsheet(columnIndex int) (string, error) { | |
return columnIndexFromGenericToSpreadsheetImpl(columnIndex, "ABCDEFGHIJKLMNOPQRSTUVWXYZ") | |
} | |
func columnIndexFromGenericToSpreadsheetImpl(columnIndex int, spreadsheetColumnIndexList string) (string, error) { | |
var ( | |
genericColumnIndex = "" | |
listLength = len(spreadsheetColumnIndexList) | |
) | |
for { | |
div := columnIndex / listLength | |
if div > 0 { | |
prefix, err := columnIndexFromGenericToSpreadsheetImpl(div, " ABCDEFGHIJKLMNOPQRSTUVWXYZ") | |
if err != nil { | |
return "", err | |
} | |
genericColumnIndex = prefix + genericColumnIndex | |
columnIndex -= div * listLength | |
spreadsheetColumnIndexList = strings.TrimSpace(spreadsheetColumnIndexList) | |
continue | |
} | |
remain := columnIndex % listLength | |
res := spreadsheetColumnIndexList[remain : remain+1] | |
genericColumnIndex += res | |
break | |
} | |
return genericColumnIndex, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment