Method: SheetsV4::GoogleExtensions::Spreadsheet#each_sheet

Defined in:
lib/sheets_v4/google_extensions/spreadsheet.rb

#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