LoginSignup
1
1

More than 5 years have passed since last update.

Googleスプレッドシートからデータを取り出すgemを作った

Last updated at Posted at 2017-06-23

google_spreadsheet_fetcherというgemを作ったのでメモ。

経緯

非エンジニアに作業をお願いしてその結果を取り込み時にGoogleスプレッドシートを経由する事が多いので作った。

  • 日本語・英語の翻訳データを記載していって、i18n用のyamlファイルを作りたい
  • プロフィールを一気に設定したい

とか。

使い方

Google Cloud Platformでプロジェクトを作る

を見ながら作る。

Google Drive APIをONにする

認証情報を作成

  • OAuthクライアントID
  • アプリケーションの種類は「その他」

image1.png

シークレット情報をJSONでダウンロード

image2.png

後はコードを書く

credential_store_file = Rails.root.join('config', 'credential-oauth2-supporter.json').to_s
sheet_key = 'YOUR_SHEET_KEY'

GoogleSpreadsheetFetcher.configure do |config|
  config.client_secrets_file_path = Rails.root.join('config', 'client_secrets_pokota_supporter.json').to_s
end

user_id = 'sample'
fetcher = GoogleSpreadsheetFetcher::Fetcher.new(credential_store_file, user_id, sheet_key)

fetcher.fetch_by_index(0)
fetcher.fetch_by_title('title')
fetcher.fetch_by_gid('gid')
  • コードを実行すると初回だけ、ブラウザが立ち上がりOAuthの認証画面に行く
  • 認証を済ますとコードが発行されるので、そのコードをコピペする
1
1
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
1
1