Last active
August 29, 2015 13:57
-
-
Save tsnow/9697545 to your computer and use it in GitHub Desktop.
Google Drive Spreadsheet to CSV
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
#!/usr/bin/env ruby | |
gem 'google_drive', '~>0.3.7' | |
require "google_drive" # see: https://github.com/gimite/google-drive-ruby | |
require 'optparse' | |
require 'tempfile' | |
option_parser = OptionParser.new do |o| | |
o.banner = "Usage: './bin/download_from_drive 'Google Drive Title'. Outputs CSV data on stdout." | |
o.on('-u USER') do |u| | |
$user = u | |
end | |
o.on('-p FILE') do |p| # echo -n 'sekret' > gd.secret; download_from_drive -p ./gd.secret | |
$password = File.read(p).chomp | |
end | |
o.on('--title FILE') do |f| | |
$title = f | |
end | |
end | |
option_parser.parse!(ARGV) | |
title = ARGV.shift | |
unless [$user,$password,title].all? | |
puts option_parser | |
exit 1 | |
end | |
# Logs in. | |
# You can also use OAuth. See document of | |
# GoogleDrive.login_with_oauth for details. | |
session = GoogleDrive.login($user, $password) | |
# Gets list of remote files. | |
files = session.files | |
spreadsheet = files.find do |file| | |
file.title == title | |
end | |
names = files.map(&:title) | |
unless spreadsheet | |
$stderr.puts "No spreadsheet by that name found. Names:", names.map(&:inspect) | |
exit 1 | |
end | |
# Spit out csv data | |
puts spreadsheet.export_as_string('csv') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment