Class: TableSetting::Sheet

Inherits:
Object
  • Object
show all
Defined in:
lib/table_setting/sheet.rb

Constant Summary collapse

@@counter =
0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent_stack = nil, options = {}) ⇒ Sheet

Returns a new instance of Sheet.



7
8
9
10
11
12
13
14
15
# File 'lib/table_setting/sheet.rb', line 7

def initialize(parent_stack = nil, options = {})
  @stack = parent_stack || TableSetting::Stack.new
  @stack.sheets.push(self)
  @rows = []
  @debug = options[:debug]

  @@counter += 1
  @name = options[:name] || "Sheet#{@@counter}"
end

Instance Attribute Details

#debugObject (readonly)

Returns the value of attribute debug.



2
3
4
# File 'lib/table_setting/sheet.rb', line 2

def debug
  @debug
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/table_setting/sheet.rb', line 3

def name
  @name
end

#rowsObject

Returns the value of attribute rows.



3
4
5
# File 'lib/table_setting/sheet.rb', line 3

def rows
  @rows
end

#stackObject (readonly)

Returns the value of attribute stack.



2
3
4
# File 'lib/table_setting/sheet.rb', line 2

def stack
  @stack
end

Instance Method Details

#cellsObject



17
18
19
# File 'lib/table_setting/sheet.rb', line 17

def cells
  rows.map(&:cells).flatten
end

#css_stylesObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/table_setting/sheet.rb', line 67

def css_styles
  signatures = {}
  cells.each do |cell|
    next if signatures[cell.style_name]
    signatures[cell.style_name] = cell.style_css
  end
  # .grid-sheet td {background-color: #fff;}
  # .grid-sheet tr:nth-child(odd) td {background-color: #eee;}
  # .grid-sheet .bold {font-weight: bold;}
  css = <<-CSS
    .grid-sheet {background-color: #ddd; border-collapse: separate; border-spacing: 1px;}
    .grid-sheet td {background-color: #fff;}
  CSS
  signatures.each do |signature_class, signature_css|
    next if signature_css.empty?
    css += "\n.grid-sheet td.#{signature_class} {#{signature_css}}"
  end
  css
end

#indexObject



52
53
54
# File 'lib/table_setting/sheet.rb', line 52

def index
  @index ||= sheet.rows.index(self)
end

#new_row(options = {}) ⇒ Object



39
40
41
# File 'lib/table_setting/sheet.rb', line 39

def new_row(options = {})
  TableSetting::Row.new(self, options)
end

#num_columnsObject



21
22
23
# File 'lib/table_setting/sheet.rb', line 21

def num_columns
  rows.map{|row| row.num_columns}.sort.last
end

#spacerObject



35
36
37
# File 'lib/table_setting/sheet.rb', line 35

def spacer
  self.new_row.new_cell('', span: 'all')
end

#style_column(number, options) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/table_setting/sheet.rb', line 25

def style_column(number, options)
  rows.each do |row|
    if options[:skip_row] and options[:skip_row] == rows.index(row) + 1
      next
    end
    row.fill
    row.cells.select{|c| c.in_column?(number)}.map{|c| c.set_style(options)}
  end
end

#to_csvObject



43
44
45
46
47
48
49
50
# File 'lib/table_setting/sheet.rb', line 43

def to_csv
  csv_string = CSV.generate do |csv|
    rows.each do |row|
      csv << row.to_a
    end
  end
  csv_string
end

#to_htmlObject



56
57
58
59
60
61
62
63
64
65
# File 'lib/table_setting/sheet.rb', line 56

def to_html
  <<-HTML
    <style type="text/css">
      #{css_styles}
    </style>
    <table class="grid-sheet">
      #{rows.map(&:to_html).join("\n")}
    </table>
  HTML
end

#to_xls(stack_context = false) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/table_setting/sheet.rb', line 87

def to_xls(stack_context = false)
  if stack_context
    <<-XML
      <Worksheet ss:Name="#{self.name}">
        <Table>
          #{rows.map(&:to_xls).join}
        </Table>
      </Worksheet>
    XML
  else
    stack.to_xls
  end
end