Skip to main content
Version: v3.0

Importing from Google Sheets

This guide describes how to import data from Google Sheets.

Google Sheets

Google sheets can be access via APIs

Authentication

Set up a service account: https://console.cloud.google.com/iam-admin/serviceaccounts

Use the google-drive-ruby library as it has some useful higher level abstractions and rows can be easily iterated on: https://github.com/gimite/google-drive-ruby

Use the environment variable set up described at https://github.com/googleapis/google-auth-library-ruby#example-environment-variables. Place the environment variables in your .env file.

GOOGLE_ACCOUNT_TYPE=service_account
GOOGLE_CLIENT_ID=000000000000000000000
GOOGLE_CLIENT_EMAIL=xxxx@xxxx.iam.gserviceaccount.com
GOOGLE_PRIVATE_KEY="-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----\n"

Enable APIs:

Grant access to sheet:

You will have to share the sheet with the GOOGLE_CLIENT_EMAIL.

Sample code

# Use nil so it will default to the environment variables
session = GoogleDrive::Session.from_service_account_key(nil)

# Pass the sheet id
ws = session.spreadsheet_by_key("1gZNS_SxDXXQ3R8FOalQZUoEmNhVbfvRWrgiOMbjXmcw").worksheets[0]