Class: CSVPlusPlus::Reader::GoogleSheets

Inherits:
Reader
  • Object
show all
Extended by:
T::Generic, T::Sig
Includes:
GoogleApiClient
Defined in:
lib/csv_plus_plus/reader/google_sheets.rb

Overview

A class that can read an existing Google Sheets spreadsheet

Constant Summary collapse

CellValue =
type_member { { fixed: ::String } }

Instance Method Summary collapse

Methods included from GoogleApiClient

#drive_client, #sheets_client

Constructor Details

#initialize(options) ⇒ GoogleSheets

Open a CSV outputter to the output_filename specified by the Options

Parameters:



19
20
21
22
23
24
# File 'lib/csv_plus_plus/reader/google_sheets.rb', line 19

def initialize(options)
  super()

  @options = options
  @cell_values = ::T.let(nil, ::T.nilable(::T::Array[::T::Array[::T.nilable(::String)]]))
end

Instance Method Details

#sheetGoogle::Apis::SheetsV4::Sheet?

Returns:

  • (Google::Apis::SheetsV4::Sheet, nil)


40
41
42
# File 'lib/csv_plus_plus/reader/google_sheets.rb', line 40

def sheet
  spreadsheet.sheets.find { |s| s.properties.title.strip == @options.sheet_name.strip }
end

#spreadsheetGoogle::Apis::SheetsV4::Spreadsheet

Returns:

  • (Google::Apis::SheetsV4::Spreadsheet)


46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/csv_plus_plus/reader/google_sheets.rb', line 46

def spreadsheet
  @spreadsheet ||= ::T.let(
    sheets_client.get_spreadsheet(@options.sheet_id),
    ::T.nilable(::Google::Apis::SheetsV4::Spreadsheet)
  )

  unless @spreadsheet
    raise(::CSVPlusPlus::Error::WriterError, "Unable to connect to google spreadsheet #{@options.sheet_id}")
  end

  @spreadsheet
end

#value_at(cell) ⇒ CellValue?

Get the current value at the cell‘s location.

Parameters:

Returns:



34
35
36
# File 'lib/csv_plus_plus/reader/google_sheets.rb', line 34

def value_at(cell)
  cell_values[cell.row_index]&.[](cell.index)
end