Module: ActiveRecordToGoogleSpreadsheet::Converter::ActiveRecordRelationConverter

Extended by:
Utils
Includes:
Utils
Included in:
ActiveRecord::Relation
Defined in:
lib/activerecord_to_google_spreadsheet/converter.rb

Instance Method Summary collapse

Methods included from Utils

get_worksheet_by_name

Instance Method Details

#from_google_spreadsheetObject



54
55
# File 'lib/activerecord_to_google_spreadsheet/converter.rb', line 54

def from_google_spreadsheet
end

#to_google_spreadsheet(session, spreadsheet_key, name: self.table_name, worksheet_title: false, row_offset: 1) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/activerecord_to_google_spreadsheet/converter.rb', line 34

def to_google_spreadsheet(session, spreadsheet_key, name: self.table_name, worksheet_title: false, row_offset: 1)
  spreadsheet = session.spreadsheet_by_key(spreadsheet_key)
  ws = get_worksheet_by_name(spreadsheet, name)

  if worksheet_title
    self.column_names.each_with_index do |name, column_index|
      ws[row_offset, column_index+1] = name
    end
    row_offset += 1
  end

  self.each_with_index do |item, index|
    self.column_names.each_with_index do |name, column_index|
      ws[index + row_offset, column_index + 1] = item[name]
    end
  end
  ws.save
  ws.reload
end