This is a Ruby library to read/write files/spreadsheets in Google Drive/Docs.

NOTE: This is NOT a library to create Google Drive App.

Migration from 0.3.x or before to ver. 1.0.x

Ver. 0.3.x and the versions before no longer works, because the API used was deprecated and shut down. You need to migrate to ver. 1.0.x.

Ver. 1.0.x is not 100% backward compatible with 0.3.x. Some methods have been removed. Especially, GoogleDrive.login has been removed, and you must use GoogleDrive.saved_session or GoogleDrive.login_with_oauth instead, as in the example code below.

How to install

$ sudo gem install google_drive

How to use

First, follow “Create a client ID and client secret” in this page to get a client ID and client secret for OAuth. Set “Application type” to “Other” in the form to create a client ID if you use GoogleDrive.saved_session method as in the example below.

Example to read/write files in Google Drive:

require "google/api_client"
require "google_drive"

# The client ID and client secret you obtained in the step above.

# Creates a session. This will prompt the credential via command line for the
# first time and save it to ./stored_token.json file for later usages.
# If you are developing a Web app, and you want to ask the user to log in in
# the Web app instead of via command line, follow the example code in:
session = GoogleDrive.saved_session("./stored_token.json", nil, CLIENT_ID, CLIENT_SECRET)

# Gets list of remote files.
session.files.each do |file|
  p file.title

# Uploads a local file.
session.upload_from_file("/path/to/hello.txt", "hello.txt", convert: false)

# Downloads to a local file.
file = session.file_by_title("hello.txt")

# Updates content of the remote file.

Example to read/write spreadsheets:

require "google/api_client"
require "google_drive"

# Same as the code above to get access_token...

# Creates a session.
session = GoogleDrive.(access_token)

# First worksheet of
# Or
ws = session.spreadsheet_by_key("pz7XtlQC-PYx-jrVMJErTcg").worksheets[0]

# Gets content of A2 cell.
p ws[2, 1]  #==> "hoge"

# Changes content of cells.
# Changes are not sent to the server until you call
ws[2, 1] = "foo"
ws[2, 2] = "bar"

# Dumps all cells.
( do |row|
  ( do |col|
    p ws[row, col]

# Yet another way to do so.
p ws.rows  #==> [["fuga", ""], ["foo", "bar]]

# Reloads the worksheet to get changes by other clients.

API document:

Source code

The license of this source is “New BSD Licence”

Supported environments

Ruby 1.9.x or later. Checked with Ruby 1.9.3 and 2.1.3.


Hiroshi Ichikawa -