spreadsheet_to_json 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/spreadsheet_to_json.rb +57 -0
  3. metadata +47 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f6ea1f08ddb691d4358cc326c599e938c7156ee8
4
+ data.tar.gz: a73b2304166583529583afbeac2eb2187d1d9a40
5
+ SHA512:
6
+ metadata.gz: 52b084a9cd9ef1d174c6c2d5dbc2fc7335fc91d017a38079912865621e02c69cd572d2ba362897687abf5c2e228ebcf0bd815edbb4ce62580b86871c6d393a9e
7
+ data.tar.gz: a908ba349004bc4413965c3219ae660a4f7445a7d3a2baefb520f4017d902408d714ecdf9c2723999e3a22e181d39a1fa7e8aaaf82ad8c955825db01abca0034
@@ -0,0 +1,57 @@
1
+ #!/usr/bin/ruby
2
+ require 'google_drive'
3
+ require 'yaml'
4
+ require 'json'
5
+
6
+ module SpreadsheetToJson
7
+ class << self
8
+ def convert_to_json(worksheet_id=0, rows=[*2..get_spreadsheet.num_rows-2], keys_row_num)
9
+ spreadsheet = get_spreadsheet(worksheet_id)
10
+ rows.collect {|row_num| construct_hash(spreadsheet, keys_row_num-2, row_num-2)}.to_json
11
+ end
12
+
13
+ private
14
+ def get_spreadsheet(worksheet_id=0)
15
+ session = GoogleDrive.login_with_oauth(init_auth_token)
16
+ session.spreadsheet_by_key(get_settings_from_yml['spreadsheet_key']).worksheets[worksheet_id]
17
+ end
18
+
19
+ def init_auth_token
20
+ config = get_settings_from_yml
21
+ client = OAuth2::Client.new(
22
+ config['auth']['client_id'],
23
+ config['auth']['client_secret'],
24
+ site: config['auth']['site'],
25
+ token_url: config['auth']['token_url'],
26
+ authorize_url: config['auth']['token_url']
27
+ )
28
+ auth_token = OAuth2::AccessToken.from_hash(
29
+ client,
30
+ {
31
+ :refresh_token => config['auth']['refresh_token'],
32
+ :expires_at => 3600
33
+ }
34
+ )
35
+ auth_token.refresh!.token
36
+ end
37
+
38
+ def get_settings_from_yml
39
+ File.open("./spreadsheet_access.yml") { |file| YAML.load(file) }
40
+ end
41
+
42
+ def construct_hash(spreadsheet, keys_row_num, row=1)
43
+ if row < 1
44
+ puts "illeagal row num"
45
+ end
46
+
47
+ begin
48
+ json_keys = spreadsheet.list[keys_row_num].to_hash.values
49
+ rescue NoMethodError
50
+ puts "worksheet_id is not exist"
51
+ exit
52
+ end
53
+ json_values = spreadsheet.list[row].to_hash.values
54
+ Hash[json_keys.zip(json_values)]
55
+ end
56
+ end
57
+ end
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: spreadsheet_to_json
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Xu Jianyong
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-06-16 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: |-
14
+ A gem to help you convert your google spreadsheet data to json format. With this gem, you can
15
+ choose the worksheet that you want to convert and point the row number you want to convert!
16
+ email: xujianyong1986@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/spreadsheet_to_json.rb
22
+ homepage: ''
23
+ licenses:
24
+ - MIT
25
+ metadata: {}
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubyforge_project:
42
+ rubygems_version: 2.2.2
43
+ signing_key:
44
+ specification_version: 4
45
+ summary: This is a little gem that helps you to convert your google spreadsheet to
46
+ json format
47
+ test_files: []