module CsvToGsheet::Runner

Public Class Methods

invoke() click to toggle source
# 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