class TableSetting::Sheet

Attributes

debug[R]
name[RW]
rows[RW]
stack[R]

Public Class Methods

new(parent_stack = nil, options = {}) click to toggle source
# 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

Public Instance Methods

cells() click to toggle source
# File lib/table_setting/sheet.rb, line 17
def cells
  rows.map(&:cells).flatten
end
css_styles() click to toggle source
# 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
index() click to toggle source
# File lib/table_setting/sheet.rb, line 52
def index
  @index ||= sheet.rows.index(self)
end
new_row(options = {}) click to toggle source
# File lib/table_setting/sheet.rb, line 39
def new_row(options = {})
  TableSetting::Row.new(self, options)
end
num_columns() click to toggle source
# File lib/table_setting/sheet.rb, line 21
def num_columns
  rows.map{|row| row.num_columns}.sort.last
end
spacer() click to toggle source
# File lib/table_setting/sheet.rb, line 35
def spacer
  self.new_row.new_cell('', span: 'all')
end
style_column(number, options) click to toggle source
# 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_csv() click to toggle source
# 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_html() click to toggle source
# 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) click to toggle source
# 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