Last active
April 20, 2016 09:10
-
-
Save kei-p/3c5d3b50113803ac9d1e853cb5683b18 to your computer and use it in GitHub Desktop.
Fetch google spreadsheet
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 '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