Module: SheetsV4::GoogleExtensions::Spreadsheet

Included in:
Google::Apis::SheetsV4::Spreadsheet
Defined in:
lib/sheets_v4/google_extensions/spreadsheet.rb

Overview

The SheetsService class implements handling credentials on top of the Google::Apis::SheetsV4::SheetsService class.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#sheets_serviceGoogle::Apis::SheetsV4::SheetsService (readonly)

The sheets_service object used to create this spreadsheet

Examples:

sheets_service = spreadsheet.sheets_service

Returns:



20
21
22
# File 'lib/sheets_v4/google_extensions/spreadsheet.rb', line 20

def sheets_service
  @sheets_service
end

Instance Method Details

#each_sheet(ids_or_titles = sheets.map { |sheet| sheet.properties.sheet_id }) {|the| ... }

This method returns an undefined value.

Iterate over sheets in a spreadsheet

If ids_or_titles is not given or is nil, all sheets are enumerated. Otherwise, only the sheets whose IDs or titles are in sheets are enumerated.

Examples:

Enumerate all sheets

spreadsheet.each_sheet { |sheet| puts sheet.properties.title }

Enumerate sheets whose IDs are 123456 and 789012

sheets = [123456, 789012]
spreadsheet.each_sheet(sheets).with_index do |sheet, index|
  puts "#{index}: #{sheet.properties.title} (#{sheet.properties.sheet_id})"
end

Parameters:

  • ids_or_titles (Array<Integer, #to_s>) (defaults to: sheets.map { |sheet| sheet.properties.sheet_id })

    an array of sheet IDs and/or titles

    An Integer in this array is match to the sheet ID. Anything else is matched to the sheet title after calling #to_s on it.

Yields:

  • each matching sheet

Yield Parameters:

Raises:

  • (RuntimeError)

    if one of the sheets does not exist

  • (RuntimeError)

    if a block is not given



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/sheets_v4/google_extensions/spreadsheet.rb', line 90

def each_sheet(ids_or_titles = sheets.map { |sheet| sheet.properties.sheet_id }, &block)
  return enum_for(:each_sheet, ids_or_titles) unless block

  matching_sheets = ids_or_titles.map do |id_or_title|
    sheet(id_or_title) || raise("Could not find sheet '#{id_or_title}'")
  end

  matching_sheets.each { |sheet| block[sheet] }

  self
end

#sheet(id_or_title) ⇒ Google::Apis::SheetsV4::Sheet?

Return the matching sheet object or nil

If id_or_title is an Integer, it is assumed to be the sheet ID. Otherwise, it is assumed to be the sheet title.

Examples:

Get the sheet whose title is 'Sheet1'

sheet = spreadsheet.sheet('Sheet1')

Get the sheet whose title is '2023-03-15'

date = Date.new(2023, 3, 15)
sheet = spreadsheet.sheet(date)

Get the sheet whose ID is 123456

sheet = spreadsheet.sheet(123456)

Parameters:

  • id_or_title (Integer, #to_s)

    the ID or title of the sheet to return

Returns:



41
42
43
44
45
46
47
48
# File 'lib/sheets_v4/google_extensions/spreadsheet.rb', line 41

def sheet(id_or_title)
  if id_or_title.is_a?(Integer)
    sheets.find { |sheet| sheet.properties.sheet_id == id_or_title }
  else
    title = id_or_title.to_s
    sheets.find { |sheet| sheet.properties.title == title }
  end
end

#sheet_id(id_or_title) ⇒ Integer

Return the ID of the matching sheet

Examples:

id = spreadsheet.sheet_id('Sheet1')

Parameters:

  • id_or_title (Integer, #to_s)

    the ID or title of the sheet

Returns:

  • (Integer)


59
60
61
# File 'lib/sheets_v4/google_extensions/spreadsheet.rb', line 59

def sheet_id(id_or_title)
  sheet(id_or_title)&.properties&.sheet_id
end