google_sheets_to_csv 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/bin/google_sheets_to_csv +79 -0
  3. metadata +100 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 0e365b16110349cbb83e78591e8bfeaaaff02a36c7e445f2eba6d4a11606763b
4
+ data.tar.gz: 1a32489528b253f3e7040a6c894bd31a78a81921ed74abd3af5ecbc391f79709
5
+ SHA512:
6
+ metadata.gz: 80eaa8980f1f09d1d96d6a9e18501b6707cb644dcb57afa24ef1b1a88a91332952426173ba07c0ae42794992ebabf2d8bc732b5ddacc6d747d678d345b76f750
7
+ data.tar.gz: 8d9b524997a55d7a383e4feb4f9fe5754108b1a92be941c94d3441ac12fdad147854ef1c6541214580795e33d53b6760af681b318830950a2c74d8cadc5537d0
@@ -0,0 +1,79 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'googleauth'
5
+ require 'google/apis/sheets_v4'
6
+ require 'optparse'
7
+ require 'csv'
8
+
9
+ options = { ranges: %w[A:ZZZ] }
10
+
11
+ option_parser = OptionParser.new do |opts|
12
+ opts.banner = <<~USAGE
13
+ Usage: #{__FILE__} [options] [SHEET_ID]
14
+
15
+ Environment Variables:
16
+
17
+ - GOOGLE_SHEETS_TO_CSV_KEY - used for only public sheets
18
+ - GOOGLE_SHEETS_TO_CSV_CREDENTIAL_PATH - used for both public and private sheets
19
+
20
+ USAGE
21
+
22
+ opts.on('-r', '--ranges A:Z,Sheet2', Array, 'A list of A1 notations of the values to retrieve.') do |ranges|
23
+ options[:ranges] = ranges
24
+ end
25
+
26
+ opts.on('-h', '--help', 'Prints this help') do
27
+ puts opts
28
+ exit
29
+ end
30
+ end
31
+ option_parser.parse!
32
+
33
+ sheet_id = ARGV.shift
34
+
35
+ if sheet_id.nil?
36
+ puts option_parser
37
+ exit
38
+ end
39
+
40
+ unless ENV['GOOGLE_SHEETS_TO_CSV_KEY'] || ENV['GOOGLE_SHEETS_TO_CSV_CREDENTIAL_PATH']
41
+ warn <<~WARNING
42
+ Missing environment variables:
43
+
44
+ - GOOGLE_SHEETS_TO_CSV_KEY
45
+ - GOOGLE_SHEETS_TO_CSV_CREDENTIAL_PATH
46
+
47
+ Please set at least one of them.
48
+ WARNING
49
+ end
50
+
51
+ sheet = Google::Apis::SheetsV4::SheetsService.new
52
+ sheet.key = ENV['GOOGLE_SHEETS_TO_CSV_KEY'] if ENV['GOOGLE_SHEETS_TO_CSV_KEY']
53
+ if ENV['GOOGLE_SHEETS_TO_CSV_CREDENTIAL_PATH']
54
+ sheet.authorization =
55
+ Google::Auth::ServiceAccountCredentials
56
+ .make_creds(
57
+ json_key_io: File.open(ENV['GOOGLE_SHEETS_TO_CSV_CREDENTIAL_PATH']),
58
+ scope: Google::Apis::SheetsV4::AUTH_SPREADSHEETS_READONLY
59
+ )
60
+ end
61
+
62
+ begin
63
+ response = sheet.batch_get_spreadsheet_values(sheet_id, ranges: options[:ranges])
64
+ rescue Google::Apis::ClientError => e
65
+ body = JSON.parse(e.body)['error']
66
+ warn body['message']
67
+ case body['status']
68
+ when 'PERMISSION_DENIED'
69
+ warn 'Please try to set GOOGLE_SHEETS_TO_CSV_CREDENTIAL_PATH and grant permissions to the service account email.'
70
+ end
71
+ exit 1
72
+ end
73
+
74
+ csv = CSV.new($stdout)
75
+ response.value_ranges.each do |value_range|
76
+ value_range.values.each do |row|
77
+ csv << row
78
+ end
79
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: google_sheets_to_csv
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Jian Weihang
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-01-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.17'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.17'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '13.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '13.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: google-api-client
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '='
46
+ - !ruby/object:Gem::Version
47
+ version: 0.36.4
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '='
53
+ - !ruby/object:Gem::Version
54
+ version: 0.36.4
55
+ - !ruby/object:Gem::Dependency
56
+ name: googleauth
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '='
60
+ - !ruby/object:Gem::Version
61
+ version: 0.10.0
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '='
67
+ - !ruby/object:Gem::Version
68
+ version: 0.10.0
69
+ description: A Command-Line Tool converting Google Sheets to CSV
70
+ email: tonytonyjan@gmail.com
71
+ executables:
72
+ - google_sheets_to_csv
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - bin/google_sheets_to_csv
77
+ homepage: https://github.com/tonytonyjan/google_sheets_to_csv
78
+ licenses:
79
+ - MIT
80
+ metadata: {}
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubygems_version: 3.0.3
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: A Command-Line Tool converting Google Sheets to CSV
100
+ test_files: []