Skip to content

Instantly share code, notes, and snippets.

@tannerwelsh
Forked from Pcushing/google_drive_parsing.rb
Created June 23, 2012 05:40
Show Gist options
  • Save tannerwelsh/2977041 to your computer and use it in GitHub Desktop.
Save tannerwelsh/2977041 to your computer and use it in GitHub Desktop.
Log into google docs and parse the data, work in progress
require 'rubygems'
require 'google_drive'
require 'csv'
# Go get your consumer key, client_secret, and client_id for Google Drive here https://code.google.com/apis/console/
consumer_key = 'INSERT YOUR CONSUMER_KEY HERE'
client_secret = 'INSERT YOUR CLIENT_SECRET HERE'
client_id = 'INSERT YOUR CLIENT_ID HERE'
client = OAuth2::Client.new(
client_id, client_secret,
:site => "https://accounts.google.com",
:token_url => "/o/oauth2/token",
:authorize_url => "/o/oauth2/auth")
puts "Oh, hey there client #{ client }"
auth_url = client.auth_code.authorize_url(
:redirect_uri => "urn:ietf:wg:oauth:2.0:oob", #Comes from instructions on http://rubydoc.info/github/gimite/google-drive-ruby/master/GoogleDrive.login_with_oauth
:scope =>
"https://docs.google.com/feeds/ " +
"https://docs.googleusercontent.com/ " +
"https://spreadsheets.google.com/feeds/")
puts "We've got ourselves an auth_url #{ auth_url } so why don't you go get
us a PIN:"
authorization_code = gets.chomp
# Redirect the user to auth_url and get authorization code from redirect URL.
auth_token = client.auth_code.get_token(
authorization_code,
:redirect_uri => "urn:ietf:wg:oauth:2.0:oob")
puts "We've got ourselves an auth_token #{ auth_token }. No big deal..."
session = GoogleDrive.login_with_oauth(auth_token)
puts "We've got ourselves a session via oauth #{ session }, yo!"
# Identify the spreadsheet you want to use and grab the key
# https://docs.google.com/spreadsheet/ccc?key=KEYNAME
spreadsheet_key = "INSERT SPREADSHEET KEY HERE"
ws = session.spreadsheet_by_key(spreadsheet_key).worksheets[0]
# Determine file output
filename = "output_file.csv"
output = CSV.open(filename, "w")
puts "and here's a worksheet #{ ws } which is now being transferred into #{ filename }."
# Dump our data into csv file of your choosing.
(1..ws.num_rows).each do |row|
text = []
(1..ws.num_cols).each do |col|
text << ws[row, col]
end
output << text
end
@tannerwelsh
Copy link
Author

Hey Patrick -
Nice work on this code. Looks great.
I added the CSV output functionality, and abstracted the worksheet out so that it could be used on any Google spreadsheet. Hope this proves useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment