Class: Sheety::Spreadsheet

Inherits:
Feed
  • Object
show all
Defined in:
lib/sheety/spreadsheet.rb

Constant Summary collapse

'http://schemas.google.com/spreadsheets/2006#worksheetsfeed'
LINK_WORKSHEETS

Constants inherited from Feed

Feed::LINK_ALT, Feed::LINK_EDIT, Feed::LINK_SELF

Instance Attribute Summary collapse

Attributes inherited from Feed

#content, #id, #parent, #title, #updated

Instance Method Summary collapse

Methods inherited from Feed

#as_xml, #initialize, #link

Methods included from Children

#_get_i_val, #_passes_constraint, included

Constructor Details

This class inherits a constructor from Sheety::Feed

Instance Attribute Details

#author_emailObject (readonly)

Returns the value of attribute author_email.



12
13
14
# File 'lib/sheety/spreadsheet.rb', line 12

def author_email
  @author_email
end

#author_nameObject (readonly)

Returns the value of attribute author_name.



12
13
14
# File 'lib/sheety/spreadsheet.rb', line 12

def author_name
  @author_name
end

Instance Method Details

#export_to_worksheet(data, options = {}) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/sheety/spreadsheet.rb', line 20

def export_to_worksheet(data, options={})
  if data.respond_to?(:as_json)
    data = data.as_json
  end

  unless !data.blank? && Array === data
    raise ArgumentError.new("data must be a non-blank array (or convertible by :as_json), got #{data}")
  end

  options = {title: 'worksheet', timestamp: true}.merge(options)

  headers = data[0].keys

  worksheet = new_worksheet
  worksheet.col_count = headers.length
  worksheet.row_count = 1 # we only need to have access to the header row for cells, other rows are inserted
  worksheet.title = if options[:timestamp] then
                      "#{options[:title]}_#{Time.now.to_i.to_s}"
                    else
                      "#{options[:title]}"
                    end
  worksheet.save # save that to create the ws

  worksheet.cells # we need to fetch the link to save new cells to

  headers.each_with_index do |key, index|
    cell = worksheet.new_cell
    cell.row = 1
    cell.col = index + 1
    cell.value = key.to_s
    cell.save
  end

  worksheet.rows # Fetches the link we can save rows to

  # We have to normalize the keys we display to a version without underscores or spaces or other bollocks
  header_keys = Hash[headers.map { |k| [k, Sheety::Row.normalize_key(k)] }]

  # TODO: Finish this if I end up caring....
  # dupes = (header_keys.values - header_keys.values.uniq)
  #
  # if dupes.length
  #
  # end

  data.each do |datum|
    row = worksheet.new_row
    datum.each do |kv|
      key, val = kv
      header_key = header_keys[key]
      row[header_key] = REXML::Text.normalize(val.to_s) unless val.blank?
    end
    row.save
  end
end

#inspectObject



84
85
86
# File 'lib/sheety/spreadsheet.rb', line 84

def inspect
  to_s
end

#parse(entry) ⇒ Object



14
15
16
17
18
# File 'lib/sheety/spreadsheet.rb', line 14

def parse(entry)
  super(entry)
  @author_name = entry['author'][0]['name'][0]
  @author_email = entry['author'][0]['email'][0]
end

#to_sObject

TODO: as_xml



80
81
82
# File 'lib/sheety/spreadsheet.rb', line 80

def to_s
  "<Spreadsheet::#{object_id} '#{title}' by #{author_name} w/ #{Sheety.length_s(@worksheets)} Worksheets>"
end