Skip to content

Instantly share code, notes, and snippets.

@mitu217
Created March 17, 2019 09:08
Show Gist options
  • Save mitu217/42fd57094bfcf89ec27c47a1fc0ffd3d to your computer and use it in GitHub Desktop.
Save mitu217/42fd57094bfcf89ec27c47a1fc0ffd3d to your computer and use it in GitHub Desktop.
カラム番号をスプレッドシートとかのカラム定義に変換するやつ(非推奨。R1C1形式を使うこと)
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