mirror of
https://github.com/ToolJet/ToolJet
synced 2026-05-23 17:08:34 +00:00
Backend for read google sheets operation
This commit is contained in:
parent
a068915812
commit
a7c9140ba2
1 changed files with 42 additions and 0 deletions
42
app/services/googlesheets_query_service.rb
Normal file
42
app/services/googlesheets_query_service.rb
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
class GooglesheetsQueryService
|
||||
|
||||
attr_accessor :query, :ource, :options, :source_options, :current_user
|
||||
|
||||
def initialize(data_query, options, source_options, current_user)
|
||||
@query = data_query
|
||||
@source = query.data_source
|
||||
@options = options
|
||||
@source_options = source_options
|
||||
@current_user = current_user
|
||||
end
|
||||
|
||||
def process
|
||||
|
||||
operation = query.options["operation"]
|
||||
access_token = source_options["access_token"]
|
||||
|
||||
if operation === "read"
|
||||
spreadsheet_id = query.options["spreadsheet_id"]
|
||||
sheet = query.options["sheet"]
|
||||
|
||||
result = HTTParty.get("https://sheets.googleapis.com/v4/spreadsheets/#{spreadsheet_id}/values/#{sheet}!A1:V101",
|
||||
headers: { 'Content-Type':
|
||||
'application/json', "Authorization": "Bearer #{access_token}"})
|
||||
|
||||
headers = result["values"][0]
|
||||
values = result["values"][1..]
|
||||
|
||||
parsed_values = []
|
||||
values.each do |value|
|
||||
row = { }
|
||||
headers.each_with_index do |header, index|
|
||||
row[header] = value[index]
|
||||
end
|
||||
parsed_values << row
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
{ status: 'success', data: parsed_values }
|
||||
end
|
||||
end
|
||||
Loading…
Reference in a new issue