Module: SheetReader
- Defined in:
- lib/sheet_reader.rb,
lib/sheet_reader/errors.rb,
lib/sheet_reader/version.rb
Defined Under Namespace
Classes: BadSheetId, Error, MissingEnvVars, Unauthorized
Constant Summary collapse
- REQUIRED_ENV_VARS =
%w[GOOGLE_CLIENT_EMAIL GOOGLE_ACCOUNT_TYPE GOOGLE_PRIVATE_KEY]
- VERSION =
'1.0.0'
Class Method Summary collapse
-
.read(sheet_id, sheet_name = "") ⇒ Object
Fetches the content of a google spreadsheet.
Class Method Details
.read(sheet_id, sheet_name = "") ⇒ Object
Fetches the content of a google spreadsheet
Example:
>> SheetReader.read("1ukhJwquqRTgfX-G-nxV6AsAH726TOsKQpPJfpqNjWGg")
=> [{"foo"=>"hey", "bar"=>"ho"},
{"foo"=>"let's ", "bar"=>"go"}]
Arguments:
sheet_id: (String) The google sheet identifier.
sheet_name: (String) The sheet name, by default it's the first one
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/sheet_reader.rb', line 21 def self.read(sheet_id, sheet_name = "") raise MissingEnvVars unless required_env_vars? ensure_valid_key_format begin sheets = Google::Apis::SheetsV4::SheetsService.new scopes = ['https://www.googleapis.com/auth/spreadsheets.readonly'] sheets. = Google::Auth.get_application_default(scopes) raw_values = sheets.get_spreadsheet_values(sheet_id, "#{sheet_name}!A:ZZ").values rescue Google::Apis::ClientError => e raise BadSheetId if e. =~ /notFound/ raise Unauthorized if e. =~ /forbidden/ rescue raise Error end rows_as_hashes(raw_values) end |