Skip to content

Instantly share code, notes, and snippets.

@noumi0k
Last active August 24, 2023 06:31
Show Gist options
  • Save noumi0k/3e685898d6c73762609c1930951d5530 to your computer and use it in GitHub Desktop.
Save noumi0k/3e685898d6c73762609c1930951d5530 to your computer and use it in GitHub Desktop.
spreadsheet.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"authorship_tag": "ABX9TyNJRv5EJHUaIp8e6Vr6/+zI",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/noumi0k/3e685898d6c73762609c1930951d5530/spreadsheet.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"source": [
"!pip install gspread oauth2client"
],
"metadata": {
"id": "tvwy94jaPPJM"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"import gspread\n",
"from oauth2client.service_account import ServiceAccountCredentials\n",
"\n",
"# 認証情報ファイルのパス\n",
"credentials_path = '/content/認証用ファイル.json'\n",
"\n",
"# スコープの指定\n",
"scope = ['https://spreadsheets.google.com/feeds',\n",
" 'https://www.googleapis.com/auth/drive']\n",
"\n",
"# 認証情報の設定\n",
"credentials = ServiceAccountCredentials.from_json_keyfile_name(credentials_path, scope)\n",
"\n",
"# Googleスプレッドシートに接続\n",
"gc = gspread.authorize(credentials)\n",
"\n",
"# スプレッドシートのキーを指定\n",
"spreadsheet_key = 'スプレッドシートのURL参照'\n",
"# https://docs.google.com/spreadsheets/d/この部分/edit#gid=0\n",
"\n",
"# ワークシートの選択(デフォルトは1番目のワークシート)\n",
"worksheet = gc.open_by_key(spreadsheet_key).sheet1\n",
"\n",
"# データの読み込み\n",
"data = worksheet.get_all_records()\n",
"\n",
"# データの処理\n",
"for row in data:\n",
" # 行ごとに処理を行う\n",
" print(row) # 列名に応じて処理を行う例\n",
"\n"
],
"metadata": {
"id": "_8gOymyTOMCc"
},
"execution_count": null,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment