Class: SimpleSheets

Inherits:
Object
  • Object
show all
Defined in:
lib/solis/shape/reader/simple_sheets.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, spreadsheet_id) ⇒ SimpleSheets

Returns a new instance of SimpleSheets.



9
10
11
12
13
14
# File 'lib/solis/shape/reader/simple_sheets.rb', line 9

def initialize(key, spreadsheet_id)
  @id = spreadsheet_id
  @key = key
  @sheets_service = Google::Apis::SheetsV4::SheetsService.new
  @sheets_service.key = @key    
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



7
8
9
# File 'lib/solis/shape/reader/simple_sheets.rb', line 7

def id
  @id
end

#keyObject

Returns the value of attribute key.



6
7
8
# File 'lib/solis/shape/reader/simple_sheets.rb', line 6

def key
  @key
end

#sheets_serviceObject (readonly)

Returns the value of attribute sheets_service.



7
8
9
# File 'lib/solis/shape/reader/simple_sheets.rb', line 7

def sheets_service
  @sheets_service
end

Instance Method Details

#worksheet_by_sheet_id(sheet_id) ⇒ Object



35
36
37
38
# File 'lib/solis/shape/reader/simple_sheets.rb', line 35

def worksheet_by_sheet_id(sheet_id)
  sheet_id = sheet_id.to_i
  worksheets.find { |ws| ws.sheet_id == sheet_id }
end

#worksheet_by_title(title) ⇒ Object



31
32
33
# File 'lib/solis/shape/reader/simple_sheets.rb', line 31

def worksheet_by_title(title)
  worksheets.find { |ws| ws.title == title }
end

#worksheetsObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/solis/shape/reader/simple_sheets.rb', line 16

def worksheets
  spreadsheet_api = @sheets_service.get_spreadsheet(@id, fields: 'sheets.properties')
  spreadsheet_api.sheets.map { |s| Worksheet.new(self, s.properties) }
  #TODO: catch not found
rescue Google::Apis::ClientError => e
  case e.status_code
  when 404
    raise "Sheet with id #{@id} NOT FOUND"
  else
    raise "An error occured reading sheet with id #{@id}. HTTP status code = #{e.status_code}, reason = '#{e.header.reason_phrase}'"
  end
rescue Exception  => e
  raise e
end