Created
July 12, 2017 14:41
-
-
Save itrion/fcb42b6ea8942dbd7b0dd70849e4582d to your computer and use it in GitHub Desktop.
calling google spreadsheet api using ruby client lib
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
SECRET_JSON_VARIABLE = 'GOOGLE_APPLICATION_CREDENTIALS'.freeze | |
raise 'please define an env variable GOOGLE_APPLICATION_CREDENTIALS pointing to your client_secrets.json' unless ENV[SECRET_JSON_VARIABLE] | |
raise 'please provide the spreadsheet id' if ARGV[0].nil? | |
require 'google/clients/spreadsheet_client' | |
require 'google/models/spreadsheet' | |
spreadsheet_id = ARGV[0] | |
spreadsheet = Google::Models::Spreadsheet.new(spreadsheet_id: spreadsheet_id) | |
sheet1 = spreadsheet.sheet('Hoja 1') | |
sheet1.each { |row| p row } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Google | |
module Models | |
class Spreadsheet | |
def initialize(options = {}) | |
@service = Google::Clients::SpreadsheetClient.new | |
@spreadsheet_id = options[:spreadsheet_id] | |
@sheets = {} | |
end | |
def sheet(sheet_name) | |
@sheets[sheet_name] ||= @service.get_spreadsheet_values(@spreadsheet_id, sheet_name).values | |
end | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'google/apis/sheets_v4' | |
require 'google/api_client/client_secrets' | |
module Google | |
module Clients | |
class SpreadsheetClient | |
def initialize | |
@service = Google::Apis::SheetsV4::SheetsService.new | |
@service.authorization = Google::Auth.get_application_default(Google::Apis::SheetsV4::AUTH_SPREADSHEETS) | |
end | |
def get_spreadsheet_values(spreadsheet_id, range) | |
@service.get_spreadsheet_values(spreadsheet_id, range) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment