Class: Workbook::Table
- Inherits:
-
Array
- Object
- Array
- Workbook::Table
- Includes:
- Modules::TableDiffSort, Writers::CsvTableWriter, Writers::HtmlTableWriter, Writers::JsonTableWriter
- Defined in:
- lib/workbook/table.rb
Overview
A table is a container of rows and keeps track of the sheet it belongs to and which row is its header. Additionally suport for CSV writing and diffing with another table is included.
Instance Attribute Summary collapse
-
#name ⇒ Object
Returns the value of attribute name.
Instance Method Summary collapse
-
#<<(row) ⇒ Object
Add row.
-
#[](index_or_string) ⇒ Workbook::Row, ...
Overrides normal Array’s []-function with support for symbols that identify a column based on the header-values.
-
#[]=(index_or_string, new_value) ⇒ Workbook::Cell?
Overrides normal Row’s []=-function; automatically converting to row and setting with the label correctly.
-
#clone ⇒ Workbook::Table
clones itself and the rows it contains.
-
#columns ⇒ Array<Column>
Returns an array of Column-classes describing the columns of this table.
-
#columns=(columns) ⇒ Array<Column>
Returns an array of Column-classes describing the columns of this table.
-
#contains_row?(row) ⇒ Boolean
Returns true if the row exists in this table.
- #create_or_open_row_at(index) ⇒ Object
- #define_columns_with_row(r) ⇒ Object
-
#delete_all ⇒ Workbook::Table
Removes all lines from this table.
-
#dimensions ⇒ Array
Returns The dimensions of this sheet based on longest row.
- #has_contents? ⇒ Boolean
-
#header ⇒ Workbook::Row
Returns the header of this table (typically the first row, but can be a different row).
-
#header=(h) ⇒ Workbook::Row
Set the header of this table (typically the first row, but can be a different row).
-
#header_row_index ⇒ Integer
Returns the index of the header row.
-
#initialize(row_cel_values = [], sheet = nil, options = {}) ⇒ Table
constructor
A new instance of Table.
-
#new_row(cell_values = []) ⇒ Workbook::Row
Generates a new row, with optionally predefined cell-values, that is already connected to this table.
-
#push(row) ⇒ Object
Add row.
-
#remove_empty_lines! ⇒ Workbook::Table
Removes all empty lines.
-
#sheet ⇒ Workbook::Sheet
Returns the sheet this table belongs to, creates a new sheet if none exists.
-
#sheet=(sheet) ⇒ Workbook::Sheet
Returns the sheet this table belongs to, creates a new sheet if none exists.
-
#template ⇒ Workbook::Template
Quick assessor to the book’s template, if it exists.
-
#trim(desired_row_length = nil) ⇒ Workbook::Row
remove all the trailing empty-rows (returning a trimmed clone).
-
#trim!(desired_row_length = nil) ⇒ Workbook::Row
remove all the trailing empty-rows (returning a trimmed self).
Methods included from Writers::HtmlTableWriter
Methods included from Writers::JsonTableWriter
#to_array_of_hashes_with_values, #to_json, #write_to_json
Methods included from Writers::CsvTableWriter
Methods included from Modules::TableDiffSort
#align, #align_row, #create_diff_cell, #diff, #diff_template, #diff_template=, #insert_placeholder?, #placeholder_row
Constructor Details
#initialize(row_cel_values = [], sheet = nil, options = {}) ⇒ Table
Returns a new instance of Table.
21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/workbook/table.rb', line 21 def initialize row_cel_values=[], sheet=nil, ={} row_cel_values = [] if row_cel_values == nil row_cel_values.each_with_index do |r,ri| if r.is_a? Workbook::Row r.table = self else r = Workbook::Row.new(r,self, ) end define_columns_with_row(r) if ri == 0 end self.sheet = sheet # Column data is considered as a 'row' with 'cells' that contain 'formatting' end |
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
19 20 21 |
# File 'lib/workbook/table.rb', line 19 def name @name end |
Instance Method Details
#<<(row) ⇒ Object
Add row
118 119 120 121 122 |
# File 'lib/workbook/table.rb', line 118 def <<(row) row = Workbook::Row.new(row) if row.class == Array super(row) row.set_table(self) end |
#[](index_or_string) ⇒ Workbook::Row, ...
Overrides normal Array’s []-function with support for symbols that identify a column based on the header-values
180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/workbook/table.rb', line 180 def [] index_or_string if index_or_string.is_a? String match = index_or_string.match(/([A-Z]+)([0-9]*)/i) col_index = Workbook::Column.alpha_index_to_number_index(match[1]) row_index = match[2].to_i - 1 return self[row_index][col_index] elsif index_or_string.is_a? Range collection = to_a[index_or_string].collect{|a| a.clone} return Workbook::Table.new collection elsif index_or_string.is_a? Integer return to_a[index_or_string] end end |
#[]=(index_or_string, new_value) ⇒ Workbook::Cell?
Overrides normal Row’s []=-function; automatically converting to row and setting with the label correctly
204 205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/workbook/table.rb', line 204 def []= index_or_string, new_value if index_or_string.is_a? String match = index_or_string.upcase.match(/([A-Z]*)([0-9]*)/) cell_index = Workbook::Column.alpha_index_to_number_index(match[1]) row_index = match[2].to_i - 1 self[row_index][cell_index].value = new_value else row = new_value row = Workbook::Row.new(row) unless row.is_a? Workbook::Row super(index_or_string,row) row.set_table(self) end end |
#clone ⇒ Workbook::Table
clones itself and the rows it contains
163 164 165 166 167 168 169 170 |
# File 'lib/workbook/table.rb', line 163 def clone t = self c = super c.delete_all t.each{|r| c << r.clone} c.header = c[header_row_index] if header_row_index return c end |
#columns ⇒ Array<Column>
Returns an array of Column-classes describing the columns of this table
255 256 257 258 259 |
# File 'lib/workbook/table.rb', line 255 def columns @columns ||= header.collect do |header_cell| Column.new(self) end end |
#columns=(columns) ⇒ Array<Column>
Returns an array of Column-classes describing the columns of this table
264 265 266 267 |
# File 'lib/workbook/table.rb', line 264 def columns= columns columns.each{|c| c.table=self} @columns = columns end |
#contains_row?(row) ⇒ Boolean
Returns true if the row exists in this table
132 133 134 135 |
# File 'lib/workbook/table.rb', line 132 def contains_row? row raise ArgumentError, "table should be a Workbook::Row (you passed a #{t.class})" unless row.is_a?(Workbook::Row) self.collect{|r| r.object_id}.include? row.object_id end |
#create_or_open_row_at(index) ⇒ Object
91 92 93 94 95 96 97 98 |
# File 'lib/workbook/table.rb', line 91 def create_or_open_row_at index r = self[index] if r == nil r = Workbook::Row.new r.table=(self) end r end |
#define_columns_with_row(r) ⇒ Object
76 77 78 79 80 |
# File 'lib/workbook/table.rb', line 76 def define_columns_with_row(r) self.columns = r.collect do |column| Workbook::Column.new self, {} end end |
#delete_all ⇒ Workbook::Table
Removes all lines from this table
156 157 158 |
# File 'lib/workbook/table.rb', line 156 def delete_all self.delete_if{|b| true} end |
#dimensions ⇒ Array
Returns The dimensions of this sheet based on longest row
247 248 249 250 251 |
# File 'lib/workbook/table.rb', line 247 def dimensions height = self.count width = self.collect{|a| a.length}.max [width,height] end |
#has_contents? ⇒ Boolean
124 125 126 |
# File 'lib/workbook/table.rb', line 124 def has_contents? self.clone.remove_empty_lines!.count != 0 end |
#header ⇒ Workbook::Row
Returns the header of this table (typically the first row, but can be a different row). The header row is also used for finding values in a aribrary row.
46 47 48 49 50 51 52 53 54 |
# File 'lib/workbook/table.rb', line 46 def header if defined?(@header) and @header == false false elsif defined?(@header) and @header @header else first end end |
#header=(h) ⇒ Workbook::Row
Set the header of this table (typically the first row, but can be a different row). The header row is also used for finding values in a aribrary row.
61 62 63 64 65 66 67 |
# File 'lib/workbook/table.rb', line 61 def header= h if h.is_a? Numeric @header = self[h] else @header = h end end |
#header_row_index ⇒ Integer
Returns the index of the header row
72 73 74 |
# File 'lib/workbook/table.rb', line 72 def header_row_index self.index(self.header) end |
#new_row(cell_values = []) ⇒ Workbook::Row
Generates a new row, with optionally predefined cell-values, that is already connected to this table.
86 87 88 89 |
# File 'lib/workbook/table.rb', line 86 def new_row cell_values=[] r = Workbook::Row.new(cell_values,self) return r end |
#push(row) ⇒ Object
Add row
110 111 112 113 114 |
# File 'lib/workbook/table.rb', line 110 def push(row) row = Workbook::Row.new(row) if row.class == Array super(row) row.set_table(self) end |
#remove_empty_lines! ⇒ Workbook::Table
Removes all empty lines. This function is particularly useful if you typically add lines to the end of a template-table, which sometimes has unremovable empty lines.
103 104 105 106 |
# File 'lib/workbook/table.rb', line 103 def remove_empty_lines! self.delete_if{|r| r.nil? or r.compact.empty?} self end |
#sheet ⇒ Workbook::Sheet
Returns the sheet this table belongs to, creates a new sheet if none exists
140 141 142 143 |
# File 'lib/workbook/table.rb', line 140 def sheet return @sheet if defined?(@sheet) and !@sheet.nil? self.sheet= Workbook::Sheet.new(self) end |
#sheet=(sheet) ⇒ Workbook::Sheet
Returns the sheet this table belongs to, creates a new sheet if none exists
149 150 151 |
# File 'lib/workbook/table.rb', line 149 def sheet= sheet @sheet = sheet end |
#template ⇒ Workbook::Template
Quick assessor to the book’s template, if it exists
38 39 40 |
# File 'lib/workbook/table.rb', line 38 def template sheet.book.template end |
#trim(desired_row_length = nil) ⇒ Workbook::Row
remove all the trailing empty-rows (returning a trimmed clone)
222 223 224 |
# File 'lib/workbook/table.rb', line 222 def trim desired_row_length=nil self.clone.trim!(desired_row_length) end |
#trim!(desired_row_length = nil) ⇒ Workbook::Row
remove all the trailing empty-rows (returning a trimmed self)
230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/workbook/table.rb', line 230 def trim! desired_row_length=nil max_length = self.collect{|a| a.trim.length }.max self_count = self.count-1 self.count.times do |index| index = self_count - index if self[index].trim.empty? self.delete_at(index) else break end end self.each{|a| a.trim!(max_length)} self end |