前提・実現したいこと
Pythonを始めたての初心者です。質問させてください。
Pythonでスプレットシートを操作したく、下記の記事を参考に初期設定を進めていました。
https://tanuhack.com/operate-spreadsheet/
こちらに書かれている手順の最後、「プログラムを実行する」にかかれているプログラムをコマンドプロントで実行したところうまく動かず、以下のエラーメッセージが発生しました。
どうにかして解決できないでしょうか?よろしくお願い申し上げます。
発生している問題・エラーメッセージ
>>> import gspread >>> import json >>> >>> from oauth2client.service_account import ServiceAccountCredentials >>> >>> scope = ['https://spreadsheets.google.com/feeds','https://www.googleapis.com/auth/drive'] >>> >>> credentials = ServiceAccountCredentials.from_json_keyfile_name('ダウンロードしたファイル名.json', scope) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:\Users\ユーザー\AppData\Local\Programs\Python\Python38\lib\site-packages\oauth2client\service_account.py", line 219, in from_json_keyfile_name with open(filename, 'r') as file_obj: FileNotFoundError: [Errno 2] No such file or directory: 'ダウンロードしたファイル名.json' >>> >>> gc = gspread.authorize(credentials) Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'credentials' is not defined >>> >>> SPREADSHEET_KEY = 'スプレットシートキー' >>> >>> worksheet = gc.open_by_key(SPREADSHEET_KEY).sheet1 Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'gc' is not defined >>> >>> import_value = int(worksheet.acell('A1').value) Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'worksheet' is not defined >>> >>> export_value = import_value+100 Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'import_value' is not defined >>> worksheet.update_cell(1,2, export_value)
該当のソースコード
import gspread import json #ServiceAccountCredentials:Googleの各サービスへアクセスできるservice変数を生成します。 from oauth2client.service_account import ServiceAccountCredentials #2つのAPIを記述しないとリフレッシュトークンを3600秒毎に発行し続けなければならない scope = ['https://spreadsheets.google.com/feeds','https://www.googleapis.com/auth/drive'] #認証情報設定 #ダウンロードしたjsonファイル名をクレデンシャル変数に設定(秘密鍵、Pythonファイルから読み込みしやすい位置に置く) credentials = ServiceAccountCredentials.from_json_keyfile_name('ダウンロードしたJSONファイル名.json', scope) #OAuth2の資格情報を使用してGoogle APIにログインします。 gc = gspread.authorize(credentials) #共有設定したスプレッドシートキーを変数[SPREADSHEET_KEY]に格納する。 SPREADSHEET_KEY = 'スプレッドシートキー' #共有設定したスプレッドシートのシート1を開く worksheet = gc.open_by_key(SPREADSHEET_KEY).sheet1 #A1セルの値を受け取る import_value = int(worksheet.acell('A1').value) #A1セルの値に100加算した値をB1セルに表示させる export_value = import_value+100 worksheet.update_cell(1,2, export_value)
試したこと
設定のし直し、WEB上での解決案模索
補足情報(FW/ツールのバージョンなど)
なし
あなたの回答
tips
プレビュー