to_google_spreadsheet 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in to_google_spreadsheet.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,68 @@
1
+ # to_google_spreadsheet
2
+
3
+ to_google_spreadsheet makes it trivial to populate a Google spreadsheet with an Array of Ruby objects.
4
+
5
+ ```ruby
6
+ # Adds the users Bob and John to the default spreadsheet in the worksheet Employees
7
+ [{:name => "Bob", :age => 26}, {:name => "John", :age => 28}].to_google_spreadsheet("Employees")
8
+ ```
9
+
10
+ Results in:
11
+
12
+ ![Image of Google spreadsheets result once running above command](http://f.cl.ly/items/2O3w2k0Y410R3c3r3T1K/Screen%20shot%202011-06-27%20at%209.16.11%20.png)
13
+
14
+ ## Usage
15
+
16
+ ### Installation
17
+
18
+ $ gem install to_google_spreadsheet
19
+
20
+ ### Setup
21
+
22
+ You must provide your Google Docs credentials, optionally you can supply a default spreadsheet to use:
23
+
24
+ ```ruby
25
+ module ToGoogleSpreadsheet
26
+ CREDENTIALS = ["account@company.com", "seekrit"]
27
+ DEFAULT_SPREADSHEET = "spreadsheet_key" # see note below
28
+ end
29
+ ```
30
+
31
+ The spreadsheet key is found in the URL when visiting the spreadsheet:
32
+
33
+ ![Image showing how the key is found in the URL in Google Docs](http://f.cl.ly/items/3j2q0S063j3v1I1P3x1p/Screen%20shot%202011-06-27%20at%209.24.56%20.png)
34
+
35
+ ### Usage
36
+
37
+ `#to_google_spreadsheet` is simply a method defined on Array, thus any subclasses hereof should work with `to_google_spreadsheet` as expected. `Array#to_google_spreadsheet` takes two arguments:
38
+
39
+ ```ruby
40
+ class Array
41
+ include ToGoogleSpreadsheet
42
+
43
+ def to_google_spreadsheet(worksheet, spreadsheet = nil)
44
+ session = GoogleSpreadsheet.login(*CREDENTIALS)
45
+ spreadsheet = session.spreadsheet_by_key(spreadsheet || DEFAULT_SPREADSHEET)
46
+ @ws = spreadsheet.find_or_create_worksheet_by_name(worksheet)
47
+ @ws.set_header_columns(self.first)
48
+ @ws.populate(self)
49
+ @ws.save
50
+ end
51
+ end
52
+ ```
53
+
54
+ The first argument is the name of the worksheet within the spreadsheet, e.g. "Accounts", if it doesn't already exist, it will be created. The second, optional, argument is the spreadsheet key, if it is not supplied, the default spreadsheet, defined by `ToGoogleSpreadsheet::DEFAULT_SPREADSHEET`, will be used.
55
+
56
+ ## Supported objects
57
+
58
+ ```ruby
59
+ assert array.all? {|e| [OpenStruct,Hash,ActiveRecord::Base].any? { |type| e.kind_of?(type) }} to a Google spreadsheet
60
+ ```
61
+
62
+ I.e. anything that is `#kind_of? Hash`, an `OpenStruct` or an ActiveRecord model. It is [easy to add support for additional types][at].
63
+
64
+ [at]: https://github.com/firmafon/to_google_spreadsheet/blob/master/lib/to_google_spreadsheet.rb#L40-44
65
+
66
+ # License
67
+
68
+ to_google_spreadsheet is released under the MIT license.
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,59 @@
1
+ require 'rubygems'
2
+ require 'google_spreadsheet'
3
+ require "./lib/to_google_spreadsheet/version"
4
+
5
+ # get the openstruct attribute hash
6
+ class OpenStruct
7
+ def _hash
8
+ @table
9
+ end
10
+ end
11
+
12
+ module GoogleSpreadsheet
13
+ class Spreadsheet
14
+ def find_or_create_worksheet_by_name(name)
15
+ worksheets.find {|ws| ws.title == name}
16
+ ws ||= add_worksheet(name) # create it if it doesn't exists
17
+ ws
18
+ end
19
+ end
20
+
21
+ class Worksheet
22
+ def set_header_columns(row)
23
+ get_hash_from_row(row).keys.each_with_index do |key, col_nr|
24
+ self[1, col_nr + 1] = key.to_s.capitalize
25
+ end
26
+ end
27
+
28
+ def populate(rows)
29
+ # TODO: Stop looping, put directly into the Google Spreadsheet hash
30
+ # provided by the Gem
31
+ # https://github.com/gimite/google-spreadsheet-ruby/blob/master/lib/google_spreadsheet.rb#L693-700
32
+ rows.each_with_index do |row, row_nr|
33
+ get_hash_from_row(row).each_with_index do |val, col_nr|
34
+ self[row_nr + 2, col_nr + 1] = val.last
35
+ end
36
+ end
37
+ end
38
+
39
+ private
40
+ def get_hash_from_row(row)
41
+ return row.attributes if row.respond_to?(:attributes) # ar
42
+ return row._hash if row.respond_to?(:_hash) # ostruct
43
+ row # fallback to handle hashes
44
+ end
45
+ end
46
+ end
47
+
48
+ class Array
49
+ include ToGoogleSpreadsheet
50
+
51
+ def to_google_spreadsheet(worksheet, spreadsheet = nil)
52
+ session = GoogleSpreadsheet.login(*CREDENTIALS)
53
+ spreadsheet = session.spreadsheet_by_key(spreadsheet || DEFAULT_SPREADSHEET)
54
+ @ws = spreadsheet.find_or_create_worksheet_by_name(worksheet)
55
+ @ws.set_header_columns(self.first)
56
+ @ws.populate(self)
57
+ @ws.save
58
+ end
59
+ end
@@ -0,0 +1,3 @@
1
+ module ToGoogleSpreadsheet
2
+ VERSION = "0.0.1"
3
+ end
data/test/test.rb ADDED
@@ -0,0 +1,28 @@
1
+ require './lib/to_google_spreadsheet'
2
+
3
+ # data = [
4
+ # {
5
+ # :name => "Niklas",
6
+ # :age => 23,
7
+ # :job => "Product designer"
8
+ # },
9
+ # {
10
+ # :name => "Simon",
11
+ # :age => 16,
12
+ # :job => "Developer"
13
+ # },
14
+ # {
15
+ # :name => "Peter",
16
+ # :age => 40,
17
+ # :job => "CEO"
18
+ # }
19
+ # ]
20
+
21
+ module ToGoogleSpreadsheet
22
+ CREDENTIALS = ["system-docs@firmafon.dk", "pinkpony42"]
23
+ DEFAULT_SPREADSHEET = "tA6lV1NLsBsDNuP_y8sISIw"
24
+ end
25
+
26
+ [{:name => "Bob", :age => 26}, {:name => "John", :age => 28}].to_google_spreadsheet("Employees")
27
+
28
+ #data.to_google_spreadsheet("Employees")
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "to_google_spreadsheet/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "to_google_spreadsheet"
7
+ s.version = ToGoogleSpreadsheet::VERSION
8
+ s.authors = ["Simon H. Eskildsen"]
9
+ s.email = ["sirup@sirupsen.com"]
10
+ s.homepage = "https://github.com/firmafon/to_google_spreadsheet"
11
+ s.summary = %q{Convert an Array where Array.all? {|e| [OpenStruct,Hash,ActiveRecord::Base].any? { |type| e.kind_of?(type) }} to a Google spreadsheet}
12
+ s.description = %q{Convert an Array where Array.all? {|e| [OpenStruct,Hash,ActiveRecord::Base].any? { |type| e.kind_of?(type) }} to a Google spreadsheet}
13
+
14
+ s.rubyforge_project = "to_google_spreadsheet"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+
22
+ s.add_dependency "google-spreadsheet-ruby"
23
+ s.add_development_dependency "rspec"
24
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: to_google_spreadsheet
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Simon H. Eskildsen
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-06-27 00:00:00 +02:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: google-spreadsheet-ruby
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: "0"
25
+ type: :runtime
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ prerelease: false
30
+ requirement: &id002 !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: "0"
36
+ type: :development
37
+ version_requirements: *id002
38
+ description: Convert an Array where Array.all? {|e| [OpenStruct,Hash,ActiveRecord::Base].any? { |type| e.kind_of?(type) }} to a Google spreadsheet
39
+ email:
40
+ - sirup@sirupsen.com
41
+ executables: []
42
+
43
+ extensions: []
44
+
45
+ extra_rdoc_files: []
46
+
47
+ files:
48
+ - .gitignore
49
+ - .rspec
50
+ - Gemfile
51
+ - README.md
52
+ - Rakefile
53
+ - lib/to_google_spreadsheet.rb
54
+ - lib/to_google_spreadsheet/version.rb
55
+ - test/test.rb
56
+ - to_google_spreadsheet.gemspec
57
+ has_rdoc: true
58
+ homepage: https://github.com/firmafon/to_google_spreadsheet
59
+ licenses: []
60
+
61
+ post_install_message:
62
+ rdoc_options: []
63
+
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: "0"
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: "0"
78
+ requirements: []
79
+
80
+ rubyforge_project: to_google_spreadsheet
81
+ rubygems_version: 1.6.2
82
+ signing_key:
83
+ specification_version: 3
84
+ summary: Convert an Array where Array.all? {|e| [OpenStruct,Hash,ActiveRecord::Base].any? { |type| e.kind_of?(type) }} to a Google spreadsheet
85
+ test_files:
86
+ - test/test.rb