Skip to content

Instantly share code, notes, and snippets.

@johnallen3d
Created November 7, 2019 00:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnallen3d/38a1e64d74ac8435b4fabc4e9d95483f to your computer and use it in GitHub Desktop.
Save johnallen3d/38a1e64d74ac8435b4fabc4e9d95483f to your computer and use it in GitHub Desktop.
Convert a Google Sheet into a Ruby Array of Hashes
require 'google/apis/sheets_v4'
require 'googleauth'
# with the following environment variables set
# GOOGLE_ACCOUNT_TYPE=service_account
# GOOGLE_CLIENT_EMAIL=...
# GOOGLE_PRIVATE_KEY=...
authorization = Google::Auth::ServiceAccountCredentials.make_creds(
scope: ['https://www.googleapis.com/auth/drive']
)
service = Google::Apis::SheetsV4::SheetsService.new.tap do |service|
service.authorization = authorization
end
keys, *values = service.get_spreadsheet_values(
'spreadsheet_id',
'A:ZZ'
).values
rows = values.map { |row| Hash[keys.zip(row)] }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment