lib/remi/data_subjects/gsheet.rb in remi-0.3.1 vs lib/remi/data_subjects/gsheet.rb in remi-0.3.2
- old
+ new
@@ -44,22 +44,23 @@
def service_list_files(service, folder_id)
service.list_files(q: "'#{folder_id}' in parents", page_size: 10, order_by: 'createdTime desc', fields: 'nextPageToken, files(id, name, createdTime, mimeType)')
end
- def get_spreadsheet_vals(service, spreadsheet_id)
- service.get_spreadsheet_values(spreadsheet_id, 'Sheet1')
+ def get_spreadsheet_vals(service, spreadsheet_id, sheet_name = 'Sheet1')
+ service.get_spreadsheet_values(spreadsheet_id, sheet_name)
end
def extract
service = Google::Apis::SheetsV4::SheetsService.new
service.client_options.application_name = @application_name
service.authorization = authorize()
@data = []
entries.each do |file|
- response = get_spreadsheet_vals(service, file.raw)
+ logger.info "Extracting Google Sheet data from #{file.pathname}, with sheet name : #{@sheet_name}"
+ response = get_spreadsheet_vals(service, file.raw, @sheet_name)
data.push(response)
end
self
end
@@ -83,12 +84,13 @@
end
end
private
- def init_gsheet_extractor(*args, credentials:, folder_id:, **kargs)
+ def init_gsheet_extractor(*args, credentials:, folder_id:, sheet_name: 'Sheet1', **kargs)
@default_folder_id = folder_id
+ @sheet_name = sheet_name
@oob_uri = 'urn:ietf:wg:oauth:2.0:oob'
@application_name = credentials.fetch(:application_name)
@client_secrets_path = File.join(
Dir.home,
@@ -109,28 +111,26 @@
end
class Parser::Gsheet < Parser
def parse(gs_extract)
- google_vals = gs_extract.data
return_hash = nil
- google_vals.each do |google_val|
+ gs_extract.data.each do |gs_data|
if return_hash.nil?
return_hash = Hash.new
- google_val.values[0].each do |header|
+ gs_data.values[0].each do |header|
return_hash[field_symbolizer.call(header)] = []
end
end
- keys_temp = return_hash.keys
+ headers = return_hash.keys
+ header_idx = headers.each_with_index.to_h
- google_val.values[1..-1].each do |rows|
- col_num = 0
-
- rows.each do |value|
- return_hash[keys_temp[col_num]] << value
- col_num +=1
+ gs_data.values[1..-1].each do |row|
+ headers.each do |header|
+ idx = header_idx[header]
+ return_hash[header] << (idx < row.size ? row[idx] : nil)
end
end
end
Remi::DataFrame.create(:daru, return_hash, order: return_hash.keys)
end