├── .gitignore ├── script └── gen-rdoc ├── Gemfile ├── lib ├── google_spreadsheet │ ├── acl.rb │ ├── error.rb │ ├── file.rb │ ├── list.rb │ ├── list_row.rb │ ├── record.rb │ ├── session.rb │ ├── table.rb │ ├── util.rb │ ├── acl_entry.rb │ ├── collection.rb │ ├── spreadsheet.rb │ ├── worksheet.rb │ ├── oauth1_fetcher.rb │ ├── oauth2_fetcher.rb │ ├── authentication_error.rb │ └── client_login_fetcher.rb └── google_spreadsheet.rb ├── Rakefile ├── test ├── account.yaml.example └── test_google_spreadsheet.rb ├── README.rdoc ├── google-spreadsheet-ruby.gemspec └── LICENSE.txt /.gitignore: -------------------------------------------------------------------------------- 1 | experiment 2 | doc 3 | test/account.yaml 4 | *~ 5 | *.gem 6 | .rvmrc 7 | Gemfile.lock 8 | -------------------------------------------------------------------------------- /script/gen-rdoc: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | rdoc --main=README.rdoc --title="Google Spreadsheet Ruby" --force-update README.rdoc 4 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source "http://rubygems.org" 2 | gemspec :name => "google-spreadsheet-ruby" 3 | gem "highline", ">= 1.5.1", :group => :test 4 | -------------------------------------------------------------------------------- /lib/google_spreadsheet/acl.rb: -------------------------------------------------------------------------------- 1 | # Author: Hiroshi Ichikawa 2 | # The license of this source is "New BSD Licence" 3 | 4 | require "google_spreadsheet" 5 | -------------------------------------------------------------------------------- /lib/google_spreadsheet/error.rb: -------------------------------------------------------------------------------- 1 | # Author: Hiroshi Ichikawa 2 | # The license of this source is "New BSD Licence" 3 | 4 | require "google_spreadsheet" 5 | -------------------------------------------------------------------------------- /lib/google_spreadsheet/file.rb: -------------------------------------------------------------------------------- 1 | # Author: Hiroshi Ichikawa 2 | # The license of this source is "New BSD Licence" 3 | 4 | require "google_spreadsheet" 5 | -------------------------------------------------------------------------------- /lib/google_spreadsheet/list.rb: -------------------------------------------------------------------------------- 1 | # Author: Hiroshi Ichikawa 2 | # The license of this source is "New BSD Licence" 3 | 4 | require "google_spreadsheet" 5 | -------------------------------------------------------------------------------- /lib/google_spreadsheet/list_row.rb: -------------------------------------------------------------------------------- 1 | # Author: Hiroshi Ichikawa 2 | # The license of this source is "New BSD Licence" 3 | 4 | require "google_spreadsheet" 5 | -------------------------------------------------------------------------------- /lib/google_spreadsheet/record.rb: -------------------------------------------------------------------------------- 1 | # Author: Hiroshi Ichikawa 2 | # The license of this source is "New BSD Licence" 3 | 4 | require "google_spreadsheet" 5 | -------------------------------------------------------------------------------- /lib/google_spreadsheet/session.rb: -------------------------------------------------------------------------------- 1 | # Author: Hiroshi Ichikawa 2 | # The license of this source is "New BSD Licence" 3 | 4 | require "google_spreadsheet" 5 | -------------------------------------------------------------------------------- /lib/google_spreadsheet/table.rb: -------------------------------------------------------------------------------- 1 | # Author: Hiroshi Ichikawa 2 | # The license of this source is "New BSD Licence" 3 | 4 | require "google_spreadsheet" 5 | -------------------------------------------------------------------------------- /lib/google_spreadsheet/util.rb: -------------------------------------------------------------------------------- 1 | # Author: Hiroshi Ichikawa 2 | # The license of this source is "New BSD Licence" 3 | 4 | require "google_spreadsheet" 5 | -------------------------------------------------------------------------------- /lib/google_spreadsheet/acl_entry.rb: -------------------------------------------------------------------------------- 1 | # Author: Hiroshi Ichikawa 2 | # The license of this source is "New BSD Licence" 3 | 4 | require "google_spreadsheet" 5 | -------------------------------------------------------------------------------- /lib/google_spreadsheet/collection.rb: -------------------------------------------------------------------------------- 1 | # Author: Hiroshi Ichikawa 2 | # The license of this source is "New BSD Licence" 3 | 4 | require "google_spreadsheet" 5 | -------------------------------------------------------------------------------- /lib/google_spreadsheet/spreadsheet.rb: -------------------------------------------------------------------------------- 1 | # Author: Hiroshi Ichikawa 2 | # The license of this source is "New BSD Licence" 3 | 4 | require "google_spreadsheet" 5 | -------------------------------------------------------------------------------- /lib/google_spreadsheet/worksheet.rb: -------------------------------------------------------------------------------- 1 | # Author: Hiroshi Ichikawa 2 | # The license of this source is "New BSD Licence" 3 | 4 | require "google_spreadsheet" 5 | -------------------------------------------------------------------------------- /lib/google_spreadsheet/oauth1_fetcher.rb: -------------------------------------------------------------------------------- 1 | # Author: Hiroshi Ichikawa 2 | # The license of this source is "New BSD Licence" 3 | 4 | require "google_spreadsheet" 5 | -------------------------------------------------------------------------------- /lib/google_spreadsheet/oauth2_fetcher.rb: -------------------------------------------------------------------------------- 1 | # Author: Hiroshi Ichikawa 2 | # The license of this source is "New BSD Licence" 3 | 4 | require "google_spreadsheet" 5 | -------------------------------------------------------------------------------- /lib/google_spreadsheet/authentication_error.rb: -------------------------------------------------------------------------------- 1 | # Author: Hiroshi Ichikawa 2 | # The license of this source is "New BSD Licence" 3 | 4 | require "google_spreadsheet" 5 | -------------------------------------------------------------------------------- /lib/google_spreadsheet/client_login_fetcher.rb: -------------------------------------------------------------------------------- 1 | # Author: Hiroshi Ichikawa 2 | # The license of this source is "New BSD Licence" 3 | 4 | require "google_spreadsheet" 5 | -------------------------------------------------------------------------------- /lib/google_spreadsheet.rb: -------------------------------------------------------------------------------- 1 | # Author: Hiroshi Ichikawa 2 | # The license of this source is "New BSD Licence" 3 | 4 | require "rubygems" 5 | require "google_drive" 6 | 7 | 8 | GoogleSpreadsheet = GoogleDrive 9 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require 'rake' 2 | require 'rake/testtask' 3 | 4 | task :default => [:test] 5 | 6 | desc "Run unit tests" 7 | Rake::TestTask.new("test") { |t| 8 | t.pattern = 'test/*.rb' 9 | t.verbose = true 10 | t.warning = true 11 | } 12 | -------------------------------------------------------------------------------- /test/account.yaml.example: -------------------------------------------------------------------------------- 1 | # This file is in Public Domain. 2 | 3 | # test_google_spreadsheet.rb asks you for your Google account each time by default. 4 | # If you want to automate it, copy this file to account.yaml and set some proper values. 5 | 6 | # Set this to true to store session to $HOME/.ruby_google_spreadsheet.token and reuse it. 7 | use_saved_session: false 8 | 9 | # Or you can write your Google account information here directly. 10 | mail: "example@gmail.com" 11 | password: "password" 12 | -------------------------------------------------------------------------------- /README.rdoc: -------------------------------------------------------------------------------- 1 | This library is now part of google_drive gem: https://github.com/gimite/google-drive-ruby 2 | 3 | For new code: I suggest to use google_drive gem directly. 4 | 5 | For existing code: You can keep using google-spreadsheet-ruby gem. Now google-spreadsheet-ruby is a library which simply does: 6 | 7 | require "google_drive" 8 | GoogleSpreadsheet = GoogleDrive 9 | 10 | and it provides the same API as before. 11 | 12 | Note that gimite-google-spreadsheet-ruby at gems.github.com is no longer updated, because github stopped hosting it. 13 | 14 | 15 | = Author 16 | 17 | Hiroshi Ichikawa - http://gimite.net/en/index.php?Contact 18 | -------------------------------------------------------------------------------- /google-spreadsheet-ruby.gemspec: -------------------------------------------------------------------------------- 1 | Gem::Specification.new do |s| 2 | 3 | s.name = "google-spreadsheet-ruby" 4 | s.version = "0.3.1" 5 | s.authors = ["Hiroshi Ichikawa"] 6 | s.email = ["gimite+github@gmail.com"] 7 | s.summary = "This is a library to read/write Google Spreadsheet." 8 | s.description = "This is a library to read/write Google Spreadsheet." 9 | s.homepage = "https://github.com/gimite/google-spreadsheet-ruby" 10 | s.rubygems_version = "1.2.0" 11 | s.license = "New BSD" 12 | 13 | s.files = ["README.rdoc"] + Dir["lib/**/*"] 14 | s.require_paths = ["lib"] 15 | s.has_rdoc = true 16 | s.extra_rdoc_files = ["README.rdoc"] + Dir["doc_src/**/*"] 17 | s.rdoc_options = ["--main", "README.rdoc"] 18 | 19 | s.add_dependency("google_drive", [">= 0.3.0"]) 20 | s.add_development_dependency("rake", [">= 0.8.0"]) 21 | 22 | end 23 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | The license of this software is "New BSD Licence". 2 | 3 | Copyright (c) 2013, Hiroshi Ichikawa 4 | Copyright (c) 2013, David R. Albrecht 5 | Copyright (c) 2013, Guy Boertje 6 | Copyright (c) 2013, Phuogn Nguyen 7 | All rights reserved. 8 | 9 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 10 | 11 | - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 12 | - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 13 | - Neither the name of Hiroshi Ichikawa nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 14 | 15 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 16 | -------------------------------------------------------------------------------- /test/test_google_spreadsheet.rb: -------------------------------------------------------------------------------- 1 | $LOAD_PATH.unshift(File.dirname(__FILE__) + "/../lib") 2 | require "rubygems" 3 | require "bundler/setup" 4 | 5 | require "test/unit" 6 | require "google_spreadsheet" 7 | require "highline" 8 | 9 | 10 | class TC_GoogleSpreadsheet < Test::Unit::TestCase 11 | 12 | def test_all() 13 | 14 | puts("This test will create spreadsheets with your account, read/write them") 15 | puts("and finally delete them (if everything goes well).") 16 | account_path = File.join(File.dirname(__FILE__), "account.yaml") 17 | if File.exist?(account_path) 18 | account = YAML.load_file(account_path) 19 | else 20 | account = {} 21 | end 22 | if account["use_saved_session"] 23 | session = GoogleSpreadsheet.saved_session 24 | elsif account["mail"] && account["password"] 25 | session = GoogleSpreadsheet.login(account["mail"], account["password"]) 26 | else 27 | highline = HighLine.new() 28 | mail = highline.ask("Mail: ") 29 | password = highline.ask("Password: "){ |q| q.echo = false } 30 | session = GoogleSpreadsheet.login(mail, password) 31 | end 32 | 33 | ss_title = "google-spreadsheet-ruby test " + Time.now.strftime("%Y-%m-%d-%H-%M-%S") 34 | ss = session.create_spreadsheet(ss_title) 35 | assert_equal(ss_title, ss.title) 36 | 37 | ws = ss.worksheets[0] 38 | assert_equal(ss.worksheets_feed_url, ws.spreadsheet.worksheets_feed_url) 39 | ws.title = "hoge" 40 | ws.max_rows = 20 41 | ws.max_cols = 10 42 | ws[1, 1] = "3" 43 | ws[1, 2] = "5" 44 | ws[1, 3] = "=A1+B1" 45 | ws[1, 4] = 13 46 | assert_equal(20, ws.max_rows) 47 | assert_equal(10, ws.max_cols) 48 | assert_equal(1, ws.num_rows) 49 | assert_equal(4, ws.num_cols) 50 | assert_equal("3", ws[1, 1]) 51 | assert_equal("5", ws[1, 2]) 52 | assert_equal("13", ws[1, 4]) 53 | ws.save() 54 | 55 | ws.reload() 56 | assert_equal(20, ws.max_rows) 57 | assert_equal(10, ws.max_cols) 58 | assert_equal(1, ws.num_rows) 59 | assert_equal(4, ws.num_cols) 60 | assert_equal("3", ws[1, 1]) 61 | assert_equal("5", ws[1, 2]) 62 | assert_equal("8", ws[1, 3]) 63 | assert_equal("13", ws[1, 4]) 64 | if RUBY_VERSION >= "1.9.0" 65 | assert_equal(Encoding::UTF_8, ws[1, 1].encoding) 66 | end 67 | 68 | assert_equal("3\t5\t8\t13", ss.export_as_string("tsv", 0)) 69 | 70 | ss2 = session.spreadsheet_by_key(ss.key) 71 | assert_equal(ss_title, ss2.title) 72 | assert_equal(ss.worksheets_feed_url, ss2.worksheets_feed_url) 73 | assert_equal(ss.human_url, ss2.human_url) 74 | assert_equal("hoge", ss2.worksheets[0].title) 75 | assert_equal("3", ss2.worksheets[0][1, 1]) 76 | assert_equal("hoge", ss2.worksheet_by_title("hoge").title) 77 | assert_equal(nil, ss2.worksheet_by_title("foo")) 78 | if RUBY_VERSION >= "1.9.0" 79 | assert_equal(Encoding::UTF_8, ss2.title.encoding) 80 | assert_equal(Encoding::UTF_8, ss2.worksheets[0].title.encoding) 81 | end 82 | 83 | ss3 = session.spreadsheet_by_url("http://spreadsheets.google.com/ccc?key=#{ss.key}&hl=en") 84 | assert_equal(ss.worksheets_feed_url, ss3.worksheets_feed_url) 85 | ss4 = session.spreadsheet_by_url(ss.worksheets_feed_url) 86 | assert_equal(ss.worksheets_feed_url, ss4.worksheets_feed_url) 87 | 88 | assert_not_nil(session.spreadsheets.find(){ |s| s.title == ss_title }) 89 | assert_not_nil(session.spreadsheets("title" => ss_title). 90 | find(){ |s| s.title == ss_title }) 91 | 92 | ss5 = session.spreadsheet_by_title(ss_title) 93 | assert_not_nil(ss5) 94 | assert_equal(ss_title, ss5.title) 95 | 96 | ws2 = session.worksheet_by_url(ws.cells_feed_url) 97 | assert_equal(ws.cells_feed_url, ws2.cells_feed_url) 98 | assert_equal("hoge", ws2.title) 99 | 100 | ss_copy_title = "google-spreadsheet-ruby test copy " + Time.now.strftime("%Y-%m-%d-%H-%M-%S") 101 | ss_copy = ss.duplicate(ss_copy_title) 102 | assert_not_nil(session.spreadsheets("title" => ss_copy_title). 103 | find(){ |s| s.title == ss_copy_title }) 104 | assert_equal("3", ss_copy.worksheets[0][1, 1]) 105 | 106 | # Access via GoogleSpreadsheet::Worksheet#list. 107 | ws.list.keys = ["x", "y"] 108 | ws.list.push({"x" => "1", "y" => "2"}) 109 | ws.list.push({"x" => "3", "y" => "4"}) 110 | assert_equal(["x", "y"], ws.list.keys) 111 | assert_equal(2, ws.list.size) 112 | assert_equal("1", ws.list[0]["x"]) 113 | assert_equal("2", ws.list[0]["y"]) 114 | assert_equal("3", ws.list[1]["x"]) 115 | assert_equal("4", ws.list[1]["y"]) 116 | assert_equal("x", ws[1, 1]) 117 | assert_equal("y", ws[1, 2]) 118 | assert_equal("1", ws[2, 1]) 119 | assert_equal("2", ws[2, 2]) 120 | assert_equal("3", ws[3, 1]) 121 | assert_equal("4", ws[3, 2]) 122 | ws.list[0]["x"] = "5" 123 | ws.list[1] = {"x" => "6", "y" => "7"} 124 | assert_equal("5", ws.list[0]["x"]) 125 | assert_equal("2", ws.list[0]["y"]) 126 | assert_equal("6", ws.list[1]["x"]) 127 | assert_equal("7", ws.list[1]["y"]) 128 | 129 | assert_equal([2, 1], ws.cell_name_to_row_col("A2")) 130 | assert_equal([2, 26], ws.cell_name_to_row_col("Z2")) 131 | assert_equal([2, 27], ws.cell_name_to_row_col("AA2")) 132 | assert_equal([2, 28], ws.cell_name_to_row_col("AB2")) 133 | 134 | # Makes sure we can access cells by name as well as by (row, col) pairs. 135 | assert_equal(ws[2, 1], ws["A2"]) 136 | assert_equal(ws.input_value(2, 1), ws.input_value("A2")) 137 | 138 | # Makes sure we can write to a cell by name. 139 | ws[2, 1] = "5" 140 | ws["A2"] = "555" 141 | assert_equal("555", ws[2, 1]) 142 | 143 | # Test of update_cells(). 144 | ws.update_cells(1, 1, [["1", "2"], ["3", "4"]]) 145 | assert_equal("1", ws[1, 1]) 146 | assert_equal("4", ws[2, 2]) 147 | ws.update_cells(2, 1, [["5", "6"], ["7", "8"]]) 148 | assert_equal("1", ws[1, 1]) 149 | assert_equal("6", ws[2, 2]) 150 | assert_equal("7", ws[3, 1]) 151 | 152 | ss.delete() 153 | assert_nil(session.spreadsheets("title" => ss_title). 154 | find(){ |s| s.title == ss_title }) 155 | ss_copy.delete(true) 156 | assert_nil(session.spreadsheets("title" => ss_copy_title). 157 | find(){ |s| s.title == ss_copy_title }) 158 | ss.delete(true) 159 | 160 | end 161 | 162 | 163 | def test_collection() 164 | 165 | browser_url = 166 | "https://docs.google.com/?tab=mo&authuser=0#folders/" + 167 | "0B9GfDpQ2pBVUODNmOGE0NjIzMWU3ZC00NmUyLTk5NzEtYaFkZjY1MjAyxjMc" 168 | collection_feed_url = 169 | "https://docs.google.com/feeds/default/private/full/folder%3A" + 170 | "0B9GfDpQ2pBVUODNmOGE0NjIzMWU3ZC00NmUyLTk5NzEtYaFkZjY1MjAyxjMc" 171 | session = GoogleSpreadsheet::Session.new_dummy() 172 | 173 | collection = session.collection_by_url(browser_url) 174 | assert_equal(collection_feed_url, collection.collection_feed_url) 175 | 176 | collection = session.collection_by_url(collection_feed_url) 177 | assert_equal(collection_feed_url, collection.collection_feed_url) 178 | 179 | end 180 | 181 | end 182 | --------------------------------------------------------------------------------