Created
November 7, 2019 00:13
-
-
Save johnallen3d/38a1e64d74ac8435b4fabc4e9d95483f to your computer and use it in GitHub Desktop.
Convert a Google Sheet into a Ruby Array of Hashes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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