class FplGsheet::Spreadsheet

Attributes

spreadsheet[R]

Public Class Methods

new(spreadsheet_title) click to toggle source
# File lib/fpl_gsheet/spreadsheet.rb, line 11
def initialize(spreadsheet_title)
  # Authenticate a session with your Service Account
  if File.file?("client_secret.json")
    session = GoogleDrive::Session.from_service_account_key("client_secret.json")
  else
    credentials = StringIO.new(ENV['GOOGLE_CLIENT_SECRET'])
    session = GoogleDrive::Session.from_service_account_key(credentials)
  end

  # Get the spreadsheet by its title
  @spreadsheet = session.spreadsheet_by_title(spreadsheet_title)
  @worksheet = @spreadsheet.worksheets.first # the default
  @max_row = @worksheet.num_rows
  @rows_to_add = Array.new
end

Public Instance Methods

all_rows() click to toggle source
# File lib/fpl_gsheet/spreadsheet.rb, line 50
def all_rows
  @worksheet.rows
end
insert_new_row(row_data) click to toggle source
# File lib/fpl_gsheet/spreadsheet.rb, line 35
def insert_new_row(row_data)
  @rows_to_add << row_data
end
insert_new_rows(row_data) click to toggle source
# File lib/fpl_gsheet/spreadsheet.rb, line 39
def insert_new_rows(row_data) # assumes array of arrays
  @rows_to_add += row_data
end
new_sheet(sheet_name) click to toggle source
# File lib/fpl_gsheet/spreadsheet.rb, line 27
def new_sheet(sheet_name)
  doomed_ws = @spreadsheet.worksheet_by_title(sheet_name)
  doomed_ws.delete if !doomed_ws.nil?
  @worksheet = @spreadsheet.add_worksheet(sheet_name)
  @max_row = 1
  save
end
save() click to toggle source
# File lib/fpl_gsheet/spreadsheet.rb, line 43
def save
  @worksheet.insert_rows(@max_row +1, @rows_to_add) if !@rows_to_add.empty?
  @worksheet.save
  @rows_to_add.clear # empty the array
  @max_row = @worksheet.num_rows
end