spreadsheet_to_json 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/spreadsheet_to_json.rb +4 -73
- data/lib/spreadsheet_to_json/config.rb +38 -0
- data/lib/spreadsheet_to_json/json_to_sheet.rb +32 -0
- data/lib/spreadsheet_to_json/sheet_to_json.rb +78 -0
- data/lib/spreadsheet_to_json/spreadsheet.rb +27 -0
- metadata +11 -8
- data/lib/json_to_spreadsheet.rb +0 -30
- data/lib/spreadsheet.rb +0 -18
- data/lib/util/auth.rb +0 -28
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f7f2cca1d78ebef5de7bc27832161ab3610c3302
|
4
|
+
data.tar.gz: 9aea7739dc36f85dec28bcfa326bbf8b1bbef8c0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5c8a4c9f500d99527ddda482bd3e9f31b8316e8084c249e6fd5a02e8c05addd877060bd88526494779a2ca21ca92d29bb9ca59b7b5c36425443a24e974099b41
|
7
|
+
data.tar.gz: e69c917410443a02c08ffc782534451216deb377815eda7a30137741541f4a0bb4687e93517ff78308c3fec71f70b533733c83f8c651ff64dc3064c7b18f44e7
|
data/lib/spreadsheet_to_json.rb
CHANGED
@@ -1,73 +1,4 @@
|
|
1
|
-
|
2
|
-
require '
|
3
|
-
require '
|
4
|
-
require '
|
5
|
-
require './lib/spreadsheet'
|
6
|
-
|
7
|
-
class SpreadsheetToJson
|
8
|
-
def initialize(spreadsheet)
|
9
|
-
@spreadsheet = spreadsheet
|
10
|
-
end
|
11
|
-
|
12
|
-
def convert_rows_by_sheet_id(worksheet_id, rows, keys_row_num)
|
13
|
-
rows_to_json(
|
14
|
-
@spreadsheet.get_sheet_by_id(worksheet_id),
|
15
|
-
rows,
|
16
|
-
keys_row_num
|
17
|
-
)
|
18
|
-
end
|
19
|
-
|
20
|
-
def convert_rows_by_sheet_name(worksheet_name, rows, keys_row_num)
|
21
|
-
rows_to_json(
|
22
|
-
@spreadsheet.get_sheet_by_name(worksheet_name),
|
23
|
-
rows,
|
24
|
-
keys_row_num
|
25
|
-
)
|
26
|
-
end
|
27
|
-
|
28
|
-
def convert_a_worksheet_by_name(worksheet_name, keys_row_num)
|
29
|
-
worksheet = @spreadsheet.get_sheet_by_name(worksheet_name)
|
30
|
-
rows_to_json(
|
31
|
-
worksheet,
|
32
|
-
[*keys_row_num+1..worksheet.num_rows],
|
33
|
-
keys_row_num
|
34
|
-
)
|
35
|
-
end
|
36
|
-
|
37
|
-
def convert_a_worksheet_by_id(worksheet_id, keys_row_num)
|
38
|
-
worksheet = @spreadsheet.get_sheet_by_id(worksheet_id)
|
39
|
-
rows_to_json(
|
40
|
-
worksheet,
|
41
|
-
[*keys_row_num+1..worksheet.num_rows],
|
42
|
-
keys_row_num
|
43
|
-
)
|
44
|
-
end
|
45
|
-
|
46
|
-
def output_json_file(content, output_path)
|
47
|
-
unless File.exist?(output_path)
|
48
|
-
File.new(output_path, 'w+')
|
49
|
-
end
|
50
|
-
File.open(output_path, 'w+') {|file| file.write(content) }
|
51
|
-
end
|
52
|
-
|
53
|
-
private
|
54
|
-
def rows_to_json(worksheet, rows, keys_row_num)
|
55
|
-
rows.collect {|row_num| construct_hash(worksheet, keys_row_num-2, row_num-2)}.to_json
|
56
|
-
end
|
57
|
-
|
58
|
-
def construct_hash(worksheet, keys_row_num, row=1)
|
59
|
-
if row < 1
|
60
|
-
puts "illeagal row num"
|
61
|
-
exit
|
62
|
-
end
|
63
|
-
|
64
|
-
begin
|
65
|
-
json_keys = worksheet.list[keys_row_num].to_hash.values
|
66
|
-
rescue NoMethodError
|
67
|
-
puts "worksheet_id is not exist"
|
68
|
-
exit
|
69
|
-
end
|
70
|
-
json_values = worksheet.list[row].to_hash.values
|
71
|
-
Hash[json_keys.zip(json_values)]
|
72
|
-
end
|
73
|
-
end
|
1
|
+
require 'spreadsheet_to_json/spreadsheet'
|
2
|
+
require 'spreadsheet_to_json/config'
|
3
|
+
require 'spreadsheet_to_json/sheet_to_json'
|
4
|
+
require 'spreadsheet_to_json/json_to_sheet'
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'google_drive'
|
2
|
+
|
3
|
+
module SpreadsheetToJson
|
4
|
+
class Config
|
5
|
+
class << self
|
6
|
+
# Get the spreadsheet_key
|
7
|
+
def get_spreadsheet_key(config_path)
|
8
|
+
get_settings_from_yml(config_path)['spreadsheet_key']
|
9
|
+
end
|
10
|
+
|
11
|
+
# Get the access_token of the auth
|
12
|
+
def get_access_token(config_path)
|
13
|
+
config = get_settings_from_yml(config_path)
|
14
|
+
client = OAuth2::Client.new(
|
15
|
+
config['auth']['client_id'],
|
16
|
+
config['auth']['client_secret'],
|
17
|
+
site: config['auth']['site'],
|
18
|
+
token_url: config['auth']['token_url'],
|
19
|
+
authorize_url: config['auth']['token_url']
|
20
|
+
)
|
21
|
+
auth_token = OAuth2::AccessToken.from_hash(
|
22
|
+
client,
|
23
|
+
{
|
24
|
+
:refresh_token => config['auth']['refresh_token'],
|
25
|
+
:expires_at => 3600
|
26
|
+
}
|
27
|
+
)
|
28
|
+
auth_token.refresh!.token
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
# Get config file
|
33
|
+
def get_settings_from_yml(path)
|
34
|
+
File.open(path) { |file| YAML.load(file) }
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
module SpreadsheetToJson
|
4
|
+
# This is a class to load JSON file to spreadsheet
|
5
|
+
class JsonToSheet
|
6
|
+
attr_reader :spreadsheet
|
7
|
+
|
8
|
+
def initialize(spreadsheet)
|
9
|
+
@spreadsheet = spreadsheet
|
10
|
+
end
|
11
|
+
|
12
|
+
# Load json to sheet
|
13
|
+
def convert_json_to_sheet(json_file_path, sheet_name)
|
14
|
+
arrays = json_to_hasharray(File.read(json_file_path))
|
15
|
+
if arrays.count <= 0
|
16
|
+
puts "the json file is empty"
|
17
|
+
exit
|
18
|
+
end
|
19
|
+
worksheet = spreadsheet.get_sheet_by_name(sheet_name)
|
20
|
+
worksheet.list.keys = arrays[0].keys
|
21
|
+
arrays.each do |each|
|
22
|
+
worksheet.list.push(each)
|
23
|
+
end
|
24
|
+
worksheet.save
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
def json_to_hasharray(json_content)
|
29
|
+
JSON.parse(json_content)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
module SpreadsheetToJson
|
2
|
+
class SheetToJson
|
3
|
+
attr_reader :spreadsheet
|
4
|
+
|
5
|
+
def initialize(spreadsheet)
|
6
|
+
@spreadsheet = spreadsheet
|
7
|
+
end
|
8
|
+
|
9
|
+
# Convert the selected rows to json with worksheet id, keys_row_num is the sheet's first row number with keys
|
10
|
+
def convert_rows_by_sheet_id(worksheet_id, rows, keys_row_num)
|
11
|
+
rows_to_json(
|
12
|
+
spreadsheet.get_sheet_by_id(worksheet_id),
|
13
|
+
rows,
|
14
|
+
keys_row_num
|
15
|
+
)
|
16
|
+
end
|
17
|
+
|
18
|
+
# Convert the selected rows to json with worksheet name
|
19
|
+
def convert_rows_by_sheet_name(worksheet_name, rows, keys_row_num)
|
20
|
+
rows_to_json(
|
21
|
+
spreadsheet.get_sheet_by_name(worksheet_name),
|
22
|
+
rows,
|
23
|
+
keys_row_num
|
24
|
+
)
|
25
|
+
end
|
26
|
+
|
27
|
+
# Convert a total sheet to json by worksheet name
|
28
|
+
def convert_a_worksheet_by_name(worksheet_name, keys_row_num)
|
29
|
+
worksheet = spreadsheet.get_sheet_by_name(worksheet_name)
|
30
|
+
rows_to_json(
|
31
|
+
worksheet,
|
32
|
+
[*keys_row_num+1..worksheet.num_rows],
|
33
|
+
keys_row_num
|
34
|
+
)
|
35
|
+
end
|
36
|
+
|
37
|
+
# Convert a total sheet to json by worksheet id
|
38
|
+
def convert_a_worksheet_by_id(worksheet_id, keys_row_num)
|
39
|
+
worksheet = spreadsheet.get_sheet_by_id(worksheet_id)
|
40
|
+
rows_to_json(
|
41
|
+
worksheet,
|
42
|
+
[*keys_row_num+1..worksheet.num_rows],
|
43
|
+
keys_row_num
|
44
|
+
)
|
45
|
+
end
|
46
|
+
|
47
|
+
# Write the json to a file
|
48
|
+
def output_json_file(content, output_path)
|
49
|
+
unless File.exist?(output_path)
|
50
|
+
File.new(output_path, 'w+')
|
51
|
+
end
|
52
|
+
File.open(output_path, 'w+') {|file| file.write(content) }
|
53
|
+
end
|
54
|
+
|
55
|
+
private
|
56
|
+
# Construct rows to json content
|
57
|
+
def rows_to_json(worksheet, rows, keys_row_num)
|
58
|
+
rows.map { |row_num| construct_hash(worksheet, keys_row_num-2, row_num-2) }.to_json
|
59
|
+
end
|
60
|
+
|
61
|
+
# Construct a hash with one row. param row is the constructed row number
|
62
|
+
def construct_hash(worksheet, keys_row_num, row=1)
|
63
|
+
if row < 1
|
64
|
+
puts "Illeagal row num"
|
65
|
+
exit
|
66
|
+
end
|
67
|
+
|
68
|
+
begin
|
69
|
+
json_keys = worksheet.list[keys_row_num].to_hash.values
|
70
|
+
rescue NoMethodError
|
71
|
+
puts "Worksheet does not exist"
|
72
|
+
exit
|
73
|
+
end
|
74
|
+
json_values = worksheet.list[row].to_hash.values
|
75
|
+
Hash[json_keys.zip(json_values)]
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'google_drive'
|
2
|
+
|
3
|
+
module SpreadsheetToJson
|
4
|
+
class Spreadsheet
|
5
|
+
attr_reader :spreadsheet
|
6
|
+
# Initalize the spreadsheet with options hash inlcuding access_token and spreadsheet_key
|
7
|
+
def initialize(options = {})
|
8
|
+
if options.empty?
|
9
|
+
puts "Please pass the parameter options with keys
|
10
|
+
of access_token and spreadsheet_key"
|
11
|
+
exit
|
12
|
+
end
|
13
|
+
session = GoogleDrive.login_with_oauth(options[:access_token])
|
14
|
+
@spreadsheet = session.spreadsheet_by_key(options[:spreadsheet_key])
|
15
|
+
end
|
16
|
+
|
17
|
+
# Get a sheet by worksheet's id
|
18
|
+
def get_sheet_by_id(worksheet_id=0)
|
19
|
+
spreadsheet.worksheets[worksheet_id]
|
20
|
+
end
|
21
|
+
|
22
|
+
# Get a sheet by a worksheet's name
|
23
|
+
def get_sheet_by_name(worksheet_name)
|
24
|
+
spreadsheet.worksheet_by_title(worksheet_name)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
metadata
CHANGED
@@ -1,27 +1,30 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: spreadsheet_to_json
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Xu Jianyong
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-08-02 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: |-
|
14
|
-
A gem to help you convert your google spreadsheet data to json
|
15
|
-
|
14
|
+
A gem to help you convert your google spreadsheet data to json
|
15
|
+
format and load your json to spreadsheet. With this gem, you can
|
16
|
+
choose the worksheet that you want to convert and point the row
|
17
|
+
number you want to convert!
|
16
18
|
email: xujianyong1986@gmail.com
|
17
19
|
executables: []
|
18
20
|
extensions: []
|
19
21
|
extra_rdoc_files: []
|
20
22
|
files:
|
21
|
-
- lib/json_to_spreadsheet.rb
|
22
|
-
- lib/spreadsheet.rb
|
23
23
|
- lib/spreadsheet_to_json.rb
|
24
|
-
- lib/
|
24
|
+
- lib/spreadsheet_to_json/config.rb
|
25
|
+
- lib/spreadsheet_to_json/json_to_sheet.rb
|
26
|
+
- lib/spreadsheet_to_json/sheet_to_json.rb
|
27
|
+
- lib/spreadsheet_to_json/spreadsheet.rb
|
25
28
|
homepage: https://github.com/jackxu/google_spreadsheet_to_json
|
26
29
|
licenses:
|
27
30
|
- MIT
|
@@ -46,5 +49,5 @@ rubygems_version: 2.2.2
|
|
46
49
|
signing_key:
|
47
50
|
specification_version: 4
|
48
51
|
summary: This is a little gem that helps you to convert your google spreadsheet to
|
49
|
-
json format
|
52
|
+
json format and load your json to spreadsheet
|
50
53
|
test_files: []
|
data/lib/json_to_spreadsheet.rb
DELETED
@@ -1,30 +0,0 @@
|
|
1
|
-
require 'google_drive'
|
2
|
-
require 'yaml'
|
3
|
-
require 'json'
|
4
|
-
require './lib/spreadsheet'
|
5
|
-
|
6
|
-
class JsonToSpreadsheet
|
7
|
-
def initialize(spreadsheet)
|
8
|
-
@spreadsheet = spreadsheet
|
9
|
-
end
|
10
|
-
|
11
|
-
def convert_json_to_sheet(json_file_path, sheet_name)
|
12
|
-
arrays = json_to_hasharray(File.read(json_file_path))
|
13
|
-
puts (arrays.class)
|
14
|
-
if arrays.count <= 0
|
15
|
-
puts "The JSON file is empty"
|
16
|
-
exit
|
17
|
-
end
|
18
|
-
worksheet = @spreadsheet.get_sheet_by_name(sheet_name)
|
19
|
-
worksheet.list.keys = arrays[0].keys
|
20
|
-
arrays.each do |each|
|
21
|
-
worksheet.list.push(each)
|
22
|
-
end
|
23
|
-
worksheet.save
|
24
|
-
end
|
25
|
-
|
26
|
-
private
|
27
|
-
def json_to_hasharray(json_content)
|
28
|
-
JSON.parse(json_content)
|
29
|
-
end
|
30
|
-
end
|
data/lib/spreadsheet.rb
DELETED
@@ -1,18 +0,0 @@
|
|
1
|
-
require 'google_drive'
|
2
|
-
require './lib/util/auth'
|
3
|
-
|
4
|
-
class Spreadsheet
|
5
|
-
def initialize(config_path)
|
6
|
-
session = GoogleDrive.login_with_oauth(Auth.get_auth_token(config_path))
|
7
|
-
@instance = session.spreadsheet_by_key(Auth.get_settings_from_yml(config_path)['spreadsheet_key'])
|
8
|
-
end
|
9
|
-
|
10
|
-
def get_sheet_by_id(worksheet_id=0)
|
11
|
-
@instance.worksheets[worksheet_id]
|
12
|
-
end
|
13
|
-
|
14
|
-
def get_sheet_by_name(worksheet_name)
|
15
|
-
@instance.worksheet_by_title(worksheet_name)
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
data/lib/util/auth.rb
DELETED
@@ -1,28 +0,0 @@
|
|
1
|
-
require 'google_drive'
|
2
|
-
|
3
|
-
class Auth
|
4
|
-
class << self
|
5
|
-
def get_auth_token(config_path)
|
6
|
-
config = get_settings_from_yml(config_path)
|
7
|
-
client = OAuth2::Client.new(
|
8
|
-
config['auth']['client_id'],
|
9
|
-
config['auth']['client_secret'],
|
10
|
-
site: config['auth']['site'],
|
11
|
-
token_url: config['auth']['token_url'],
|
12
|
-
authorize_url: config['auth']['token_url']
|
13
|
-
)
|
14
|
-
auth_token = OAuth2::AccessToken.from_hash(
|
15
|
-
client,
|
16
|
-
{
|
17
|
-
:refresh_token => config['auth']['refresh_token'],
|
18
|
-
:expires_at => 3600
|
19
|
-
}
|
20
|
-
)
|
21
|
-
auth_token.refresh!.token
|
22
|
-
end
|
23
|
-
|
24
|
-
def get_settings_from_yml(path)
|
25
|
-
File.open(path) { |file| YAML.load(file) }
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|