Class: FplGsheet::Spreadsheet

Inherits:
Object
  • Object
show all
Defined in:
lib/fpl_gsheet/spreadsheet.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(spreadsheet_title) ⇒ Spreadsheet

Returns a new instance of Spreadsheet.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 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.("client_secret.json")
  else
    credentials = StringIO.new(ENV['GOOGLE_CLIENT_SECRET'])
    session = GoogleDrive::Session.(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

Instance Attribute Details

#spreadsheetObject (readonly)

Returns the value of attribute spreadsheet.



9
10
11
# File 'lib/fpl_gsheet/spreadsheet.rb', line 9

def spreadsheet
  @spreadsheet
end

Instance Method Details

#all_rowsObject



50
51
52
# File 'lib/fpl_gsheet/spreadsheet.rb', line 50

def all_rows
  @worksheet.rows
end

#insert_new_row(row_data) ⇒ Object



35
36
37
# 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) ⇒ Object

assumes array of arrays



39
40
41
# 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) ⇒ Object



27
28
29
30
31
32
33
# 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

#saveObject



43
44
45
46
47
48
# 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