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 ver. 2.x.x or before
- How to install
- How to use
- API documentation
- Authorization
- Github
- License
- Supported environments
- Author
There are some incompatible API changes. See MIGRATING.md.
Add this line to your application's Gemfile:
gem'google_drive'And then execute:
$ bundle
Or install it yourself as:
$ gem install google_drive
If you need system wide installation, execute below:
$ sudo gem install google_drive
Follow one of the options in Authorization to construct a session object. The example code below assumes "On behalf of you" option.
require"google_drive"# Creates a session. This will prompt the credential via command line for the# first time and save it to config.json file for later usages.# See this document to learn how to create config.json:# https://github.com/gimite/google-drive-ruby/blob/master/doc/authorization.mdsession=GoogleDrive::Session.from_config("config.json")# Gets list of remote files.session.files.eachdo |file|
pfile.titleend# 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")file.download_to_file("/path/to/hello.txt")# Updates content of the remote file.file.update_from_file("/path/to/hello.txt")require"google_drive"# Creates a session. This will prompt the credential via command line for the# first time and save it to config.json file for later usages.# See this document to learn how to create config.json:# https://github.com/gimite/google-drive-ruby/blob/master/doc/authorization.mdsession=GoogleDrive::Session.from_config("config.json")# First worksheet of# https://docs.google.com/spreadsheet/ccc?key=pz7XtlQC-PYx-jrVMJErTcg# Or https://docs.google.com/a/someone.com/spreadsheets/d/pz7XtlQC-PYx-jrVMJErTcg/edit?usp=drive_webws=session.spreadsheet_by_key("pz7XtlQC-PYx-jrVMJErTcg").worksheets[0]# Gets content of A2 cell.pws[2,1]#==> "hoge"# Changes content of cells.# Changes are not sent to the server until you call ws.save().ws[2,1]="foo"ws[2,2]="bar"ws.save# Dumps all cells.(1..ws.num_rows).eachdo |row|
(1..ws.num_cols).eachdo |col|
pws[row,col]endend# Yet another way to do so.pws.rows#==> [["fuga", ""], ["foo", "bar]]# Reloads the worksheet to get changes by other clients.ws.reloadNew BSD Licence.
Ruby 2.0.0 or later. Checked with Ruby 2.4.1.