LoginSignup
0
0

More than 3 years have passed since last update.

列番号 ⇒ A1への変更はgspread.utilsで良い

Posted at

過去のこの記事でpythonでSpreadsheet操作をするライブライであるgspreadの使い方をまとめています。

この記事の中で数値から列のアルファベットに変換する関数を紹介しています。

数値からアルファベットに変更する関数
def conv_num_to_col(num):  
  if num <= 26:
    return chr(64 + num)
  else:
    if num % 26 == 0:
      return conv_num_to_col(num//26-1) + 'Z'
    else:
      return conv_num_to_col(num//26) + chr(64+num%26)

print(conv_num_to_col(1)) # A
print(conv_num_to_col(3)) # C
print(conv_num_to_col(6)) # F

こちらの列番号からアルファベットを取得する仕組みが元々gspreadに存在しました。
それが gspread.utils.rowcol_to_a1(row, col) です。

gspread.utils
import gspread

print(gspread.utils.rowcol_to_a1(2, 2)) # B2
print(gspread.utils.rowcol_to_a1(4, 4)) # D4

gspreadをimportするなら活用しましょう。

ちなみにgspreadのサイトを見ると a1_range_to_grid_range() があり、
引数でA1形式でレンジを入れると範囲の開始行列、終了行列が
返ってくるというメソッドです。

しかし、importした中に該当のメソッドが無くて使用できませんでした。
(2021年02月時点)

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