Class: TableSetting::Sheet
- Inherits:
-
Object
- Object
- TableSetting::Sheet
- Defined in:
- lib/table_setting/sheet.rb
Constant Summary collapse
- @@counter =
0
Instance Attribute Summary collapse
-
#debug ⇒ Object
readonly
Returns the value of attribute debug.
-
#name ⇒ Object
Returns the value of attribute name.
-
#rows ⇒ Object
Returns the value of attribute rows.
-
#stack ⇒ Object
readonly
Returns the value of attribute stack.
Instance Method Summary collapse
- #cells ⇒ Object
- #css_styles ⇒ Object
- #index ⇒ Object
-
#initialize(parent_stack = nil, options = {}) ⇒ Sheet
constructor
A new instance of Sheet.
- #new_row(options = {}) ⇒ Object
- #num_columns ⇒ Object
- #spacer ⇒ Object
- #style_column(number, options) ⇒ Object
- #to_csv ⇒ Object
- #to_html ⇒ Object
- #to_xls(stack_context = false) ⇒ Object
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, = {}) @stack = parent_stack || TableSetting::Stack.new @stack.sheets.push(self) @rows = [] @debug = [:debug] @@counter += 1 @name = [:name] || "Sheet#{@@counter}" end |
Instance Attribute Details
#debug ⇒ Object (readonly)
Returns the value of attribute debug.
2 3 4 |
# File 'lib/table_setting/sheet.rb', line 2 def debug @debug end |
#name ⇒ Object
Returns the value of attribute name.
3 4 5 |
# File 'lib/table_setting/sheet.rb', line 3 def name @name end |
#rows ⇒ Object
Returns the value of attribute rows.
3 4 5 |
# File 'lib/table_setting/sheet.rb', line 3 def rows @rows end |
#stack ⇒ Object (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
#cells ⇒ Object
17 18 19 |
# File 'lib/table_setting/sheet.rb', line 17 def cells rows.map(&:cells).flatten end |
#css_styles ⇒ Object
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 |
#index ⇒ Object
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( = {}) TableSetting::Row.new(self, ) end |
#num_columns ⇒ Object
21 22 23 |
# File 'lib/table_setting/sheet.rb', line 21 def num_columns rows.map{|row| row.num_columns}.sort.last end |
#spacer ⇒ Object
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, ) rows.each do |row| if [:skip_row] and [:skip_row] == rows.index(row) + 1 next end row.fill row.cells.select{|c| c.in_column?(number)}.map{|c| c.set_style()} end end |
#to_csv ⇒ Object
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_html ⇒ Object
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 |