Module: CsvToGsheet::Runner

Defined in:
lib/csv_to_gsheet.rb

Class Method Summary collapse

Class Method Details

.invokeObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/csv_to_gsheet.rb', line 25

def self.invoke
  file_path = ARGV[0]
  file_name = file_path.gsub(/.*\//, '')

  # We clear ARGV here so that the `gets` command later will instead prompt from
  # standard in for the user's code in our `authorize` function
  #
  # See https://stackoverflow.com/questions/2166862/why-is-gets-throwing-an-error-when-arguments-are-passed-to-my-ruby-script
  ARGV.clear

  # Check files to see if they exist and if they don't then tell the user what to do
  #
  # This exits the program early if the credentials.json-equivalent file is not present.
  CsvToGsheet.check_token_files

  # Initialize the API
  service = Google::Apis::SheetsV4::SheetsService.new
  service.client_options.application_name = APPLICATION_NAME
  service.authorization = CsvToGsheet.authorize

  # Read a spreadsheet
  #
  #  # Prints the names and majors of students in a sample spreadsheet:
  #  # https://docs.google.com/spreadsheets/d/1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms/edit
  #  spreadsheet_id = '1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms'
  #  range = 'Class Data!A2:E'
  #  response = service.get_spreadsheet_values(spreadsheet_id, range)
  #  puts 'Name, Major:'
  #  puts 'No data found.' if response.values.empty?
  #  response.values.each do |row|
  #    # Print columns A and E, which correspond to indices 0 and 4.
  #    puts "#{row[0]}, #{row[4]}"
  #  end

  #  # Find or create a spreadsheet
  #  #
  #  preexisting_spreadsheet_id = "19B3KW7Rt1cr0hq6YcKeug96vb5Qxjw1TaNtC578cdQw"
  #
  #  spreadsheet = begin
  #    service.get_spreadsheet(preexisting_spreadsheet_id)
  #  rescue Google::Apis::ClientError => e
  #    # handle the case where we don't know what exception was raised
  #    raise e if e.status_code != 404
  #
  #    # create the spreadsheet here.
  #    spreadsheet = {
  #      properties: {
  #        title: file_name
  #      }
  #    }
  #    spreadsheet = service.create_spreadsheet(spreadsheet,
  #                                             fields: 'spreadsheetId')
  #  end

  # Create a spreadsheet
  #
  spreadsheet = {
    properties: {
      title: file_name
    }
  }
  spreadsheet = service.create_spreadsheet(spreadsheet,
                                           fields: 'spreadsheetId')
  puts "Spreadsheet ID: #{spreadsheet.spreadsheet_id}"

  # append to table in spreadsheet
  #    _ _
  # A| 1 2
  # B| 3 4
  #
  # =>
  #
  #    - -
  # A| 1 2
  # B| 3 4
  # C| 1 2
  # D| 3 4

  result = begin
    result = service.append_spreadsheet_value(spreadsheet.spreadsheet_id,
                                     'A1',
                                     { values: CSV.parse(File.read(file_path)) },
                                     value_input_option: 'USER_ENTERED')
    puts "\n\u001b[42;1mUploaded csv successfully to range #{result.updates.updated_range}!\u001b[0m"

    result
  rescue StandardError => e
    puts "\n\u001b[41;1m. FAIL \u001b[0m"
    puts e.backtrace
    raise e
  end

  url = "https://docs.google.com/spreadsheets/d/#{spreadsheet.spreadsheet_id}/edit#gid=0&range=#{result.updates.updated_range.gsub(/.*!/, '')}"
  puts "Your Google Sheets spreadsheet: #{url}"
  if RUBY_PLATFORM =~ /darwin/
    `open #{url}`
  end
end