Module: Entable::XlsExport

Defined in:
lib/entable/xls_export.rb

Instance Method Summary collapse

Instance Method Details

#array_for_xls(item) ⇒ Object



24
25
26
# File 'lib/entable/xls_export.rb', line 24

def array_for_xls item
  item.is_a?(Array) ? item : item.to_csv_array
end

#csv_date(date) ⇒ Object



6
7
8
# File 'lib/entable/xls_export.rb', line 6

def csv_date date
  return I18n.l(date, :format => :csv) if date
end

#double_quote(text) ⇒ Object



50
51
52
# File 'lib/entable/xls_export.rb', line 50

def double_quote text
  text.gsub /"/, '""'
end

#encObject



4
# File 'lib/entable/xls_export.rb', line 4

def enc; "UTF-8"; end

#items_as_xls_string(items) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/entable/xls_export.rb', line 28

def items_as_xls_string items
  prologue = xls_html_prologue
  epilogue = xls_html_epilogue
  items    = items.map { |item| array_for_xls(item) }
  items    = items.map { |item| yield item } if block_given?
  lines    = items.map { |item| "<tr><td>" + item.join("</td><td>") + "</td></tr>" }
  lines    = lines.map { |line| to_utf8 line }
  prologue + lines.join("\n") + epilogue
end

#make_filename(filename) ⇒ Object



58
59
60
61
# File 'lib/entable/xls_export.rb', line 58

def make_filename filename
  return filename.reject { |x| x.blank? }.join(" ").strip.gsub(/ +/, '-') if filename.is_a?(Array)
  filename
end

#needs_quoting?(text) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/entable/xls_export.rb', line 54

def needs_quoting? text
  text.is_a?(String) && (text.match(/^0\d+$/) || text.match(/^\d+[^\d]+/))
end

#quote_for_xls(text) ⇒ Object



46
47
48
# File 'lib/entable/xls_export.rb', line 46

def quote_for_xls text
  (text.is_a?(String) && needs_quoting?(text)) ? "=\"#{double_quote text}\"" : text
end

#set_xls_headers(filename) ⇒ Object



63
64
65
66
67
# File 'lib/entable/xls_export.rb', line 63

def set_xls_headers filename
  headers["Content-Type"] = "application/vnd.ms-excel; charset=#{enc}"
  headers["Content-disposition"] = "attachment; filename=\"#{make_filename filename}.xls\""
  headers["charset"] = enc
end

#to_utf8(str) ⇒ Object



18
19
20
21
22
# File 'lib/entable/xls_export.rb', line 18

def to_utf8 str
  (str || "").encode(enc)
rescue
  raise "unable to convert '#{str}' to #{enc}"
end

#write_items_as_xls(items, &block) ⇒ Object



38
39
40
# File 'lib/entable/xls_export.rb', line 38

def write_items_as_xls items, &block
  write_text_as_xls items_as_xls_string(items, &block)
end

#write_text_as_xls(text) ⇒ Object



42
43
44
# File 'lib/entable/xls_export.rb', line 42

def write_text_as_xls text
  render :text => text, :content_type => "application/vnd.ms-excel; charset=#{enc}"
end

#xls_html_epilogueObject



14
15
16
# File 'lib/entable/xls_export.rb', line 14

def xls_html_epilogue
  "</table></body></html>"
end

#xls_html_prologueObject



10
11
12
# File 'lib/entable/xls_export.rb', line 10

def xls_html_prologue
  "<html><head><meta content='application/vnd.ms-excel;charset=#{enc}' http-equiv='Content-Type'><meta content='#{enc}' http-equiv='Encoding'></head><body><table>"
end