Class: Workbook::Table

Inherits:
Array
  • Object
show all
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

Instance Method Summary collapse

Methods included from Writers::HtmlTableWriter

#build_cell_options, #to_html

Methods included from Writers::JsonTableWriter

#to_array_of_hashes_with_values, #to_json, #write_to_json

Methods included from Writers::CsvTableWriter

#to_csv, #write_to_csv

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, options={}
  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, options)
    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

#nameObject

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

Parameters:



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

Examples:

Lookup using fixnum or header value encoded as symbol

table[0] #=> <Row [a,2,3,4]> (first row)
table["A1"] #=> <Cell value="a"> (first cell of first row)

Parameters:

  • index_or_string (Fixnum, String)

    to reference to either the row, or the cell

Returns:



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

Examples:

Lookup using fixnum or header value encoded as symbol

`table[0] = <Row [a,2,3,4]>` (set first row)
`table["A1"] = 2` (set first cell of first row to 2)

Parameters:

  • index_or_string (Fixnum, String)

    to reference to either the row, or the cell

  • new_value (Workbook::Table, Array)

    to set

Returns:



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

#cloneWorkbook::Table

clones itself and the rows it contains

Returns:



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

#columnsArray<Column>

Returns an array of Column-classes describing the columns of this table

Returns:



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

Parameters:

Returns:



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

Parameters:

Returns:

  • (Boolean)

    whether the row exist in this table

Raises:

  • (ArgumentError)


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_allWorkbook::Table

Removes all lines from this table

Returns:



156
157
158
# File 'lib/workbook/table.rb', line 156

def delete_all
  self.delete_if{|b| true}
end

#dimensionsArray

Returns The dimensions of this sheet based on longest row

Returns:

  • (Array)

    x-width, y-height



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

Returns:

  • (Boolean)


124
125
126
# File 'lib/workbook/table.rb', line 124

def has_contents?
  self.clone.remove_empty_lines!.count != 0
end

#headerWorkbook::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.

Returns:



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.

Parameters:

  • h (Workbook::Row, Integer)

    should be the row or the index of this table’s row

Returns:



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_indexInteger

Returns the index of the header row

Returns:

  • (Integer)

    The index of the header row (typically 0)



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.

Parameters:

  • cell_values (Array, Workbook::Row) (defaults to: [])

    is an array or row of cell values

Returns:



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

Parameters:



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.

Returns:



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

#sheetWorkbook::Sheet

Returns the sheet this table belongs to, creates a new sheet if none exists

Returns:



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

Parameters:

Returns:



149
150
151
# File 'lib/workbook/table.rb', line 149

def sheet= sheet
  @sheet = sheet
end

#templateWorkbook::Template

Quick assessor to the book’s template, if it exists

Returns:



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)

Parameters:

  • desired_row_length (Integer) (defaults to: nil)

    of the rows

Returns:



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)

Parameters:

  • desired_row_length (Integer) (defaults to: nil)

    of the new row

Returns:



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