Class: TableSetting::Sheet
- Inherits:
-
Object
- Object
- TableSetting::Sheet
- Defined in:
- lib/table_setting/sheet.rb
Constant Summary collapse
- @@counter =
0
Instance Attribute Summary collapse
-
#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
-
#initialize(parent_stack = TableSetting::Stack.new, 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 = TableSetting::Stack.new, options = {}) ⇒ Sheet
Returns a new instance of Sheet.
7 8 9 10 11 12 13 14 |
# File 'lib/table_setting/sheet.rb', line 7 def initialize(parent_stack = TableSetting::Stack.new, = {}) @stack = parent_stack @stack.sheets.push(self) @rows = [] @@counter += 1 @name = [:name] || "Sheet#{@@counter}" end |
Instance Attribute Details
#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
16 17 18 19 20 21 22 |
# File 'lib/table_setting/sheet.rb', line 16 def cells list = [] rows.each do |row| list.concat(row.cells) end list end |
#css_styles ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/table_setting/sheet.rb', line 69 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 |
#new_row(options = {}) ⇒ Object
45 46 47 |
# File 'lib/table_setting/sheet.rb', line 45 def new_row( = {}) TableSetting::Row.new(self, ) end |
#num_columns ⇒ Object
24 25 26 27 28 29 30 31 32 |
# File 'lib/table_setting/sheet.rb', line 24 def num_columns max = 0 rows.each do |row| if row.num_columns > max max = row.num_columns end end max end |
#spacer ⇒ Object
41 42 43 |
# File 'lib/table_setting/sheet.rb', line 41 def spacer self.new_row.new_cell('', span: 'all') end |
#style_column(number, options) ⇒ Object
34 35 36 37 38 39 |
# File 'lib/table_setting/sheet.rb', line 34 def style_column(number, ) rows.each do |row| row.fill row.cells.select{|c| c.in_column?(number)}.map{|c| c.set_style()} end end |
#to_csv ⇒ Object
49 50 51 52 53 54 55 56 |
# File 'lib/table_setting/sheet.rb', line 49 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
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/table_setting/sheet.rb', line 58 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
89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/table_setting/sheet.rb', line 89 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 |