class DataPitcher::Spreadsheet

Constants

BATCH_SIZE

Public Class Methods

new(spreadsheet_key, worksheet_title = nil) click to toggle source
# File lib/data_pitcher/spreadsheet.rb, line 7
def initialize(spreadsheet_key, worksheet_title = nil)
  @spreadsheet_key = spreadsheet_key
  @worksheet_title = worksheet_title
end

Public Instance Methods

clear_sheet() click to toggle source
# File lib/data_pitcher/spreadsheet.rb, line 25
def clear_sheet
  worksheet.reload
  worksheet.delete_rows(1, worksheet.num_rows)
  worksheet.save
end
fill_sheet(sql_query) click to toggle source
# File lib/data_pitcher/spreadsheet.rb, line 31
def fill_sheet(sql_query)
  result = DataPitcher::Executor.new(sql_query).execute
  worksheet.reload
  # fill header
  worksheet.insert_rows(1, [result.header])
  # fill rows
  result.rows.each_slice(BATCH_SIZE).with_index do |array, batch_index|
    worksheet.insert_rows(2 + batch_index * BATCH_SIZE, array)
    worksheet.save
  end
end
replace_worksheet_with_query(sql_query) click to toggle source
# File lib/data_pitcher/spreadsheet.rb, line 43
def replace_worksheet_with_query(sql_query)
  clear_sheet
  fill_sheet(sql_query)
end
session() click to toggle source
# File lib/data_pitcher/spreadsheet.rb, line 55
def session
  @session ||= ::GoogleDrive::Session.from_service_account_key(DataPitcher.configuration.google_service_account_json_path)
end
spreadsheet() click to toggle source
# File lib/data_pitcher/spreadsheet.rb, line 12
def spreadsheet
  @spreadsheet ||= session.spreadsheet_by_key(@spreadsheet_key)
end
valid?() click to toggle source
# File lib/data_pitcher/spreadsheet.rb, line 48
def valid?
  worksheet
  true
rescue => e
  false
end
worksheet() click to toggle source
# File lib/data_pitcher/spreadsheet.rb, line 16
def worksheet
  @worksheet ||=
    if @worksheet_title
      spreadsheet.worksheet_by_title(@worksheet_title)
    else
      spreadsheet.worksheets.first
    end
end