Skip to content

Instantly share code, notes, and snippets.

@kei-p
Last active April 20, 2016 09:10
Show Gist options
  • Save kei-p/3c5d3b50113803ac9d1e853cb5683b18 to your computer and use it in GitHub Desktop.
Save kei-p/3c5d3b50113803ac9d1e853cb5683b18 to your computer and use it in GitHub Desktop.
Fetch google spreadsheet
require 'bundler/setup'
require 'google_drive'
require 'json'
sheet_id = ENV['SHEET_ID']
json_file = ENV['AUTH_JSON']
options = JSON.parse(File.read(json_file))
key = OpenSSL::PKey::RSA.new(options['private_key'])
auth = Signet::OAuth2::Client.new(
token_credential_uri: options['token_uri'],
audience: options['token_uri'],
scope: %w(
https://www.googleapis.com/auth/drive
https://docs.google.com/feeds/
https://docs.googleusercontent.com/
https://spreadsheets.google.com/feeds/
),
issuer: options['client_email'],
signing_key: key)
auth.fetch_access_token!
session = GoogleDrive.login_with_oauth(auth)
ws = session.spreadsheet_by_key(sheet_id).worksheets[0]
table = (1..ws.num_rows).map do |row|
(1..ws.num_cols).map do |col|
ws[row, col]
end
end
table.each do |row|
puts row.join("\t")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment