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

#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, #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



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/workbook/table.rb', line 20

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

#columnsObject

Returns the value of attribute columns



18
19
20
# File 'lib/workbook/table.rb', line 18

def columns
  @columns
end

#nameObject

Returns the value of attribute name



17
18
19
# File 'lib/workbook/table.rb', line 17

def name
  @name
end

#sheetWorkbook::Sheet

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

Returns:



120
121
122
# File 'lib/workbook/table.rb', line 120

def sheet
  @sheet
end

Instance Method Details

#<<(row) ⇒ Object

Add row

Parameters:



98
99
100
101
102
# File 'lib/workbook/table.rb', line 98

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:



157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/workbook/table.rb', line 157

def [](index_or_string)
  if index_or_string.is_a? String
    match = index_or_string.match(/([A-Z]+)([0-9]*)/i)
    col_index = 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:



181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/workbook/table.rb', line 181

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 = 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

#alpha_index_to_number_index(string) ⇒ Integer

Helps to convert from e.g. “AA” to 26

Parameters:

  • string (String)

    that typically identifies a column

Returns:

  • (Integer)


198
199
200
201
202
203
204
205
# File 'lib/workbook/table.rb', line 198

def alpha_index_to_number_index string
  string.upcase!
  sum = 0
  string.chars.each_with_index do | char, char_index|
    sum = sum * 26 + char.unpack('U')[0]-64
  end
  return sum-1
end

#cloneWorkbook::Table

clones itself and the rows it contains

Returns:



139
140
141
142
143
144
145
146
147
# File 'lib/workbook/table.rb', line 139

def clone
  t = self
  c = super
  header_row_index = t.index(t.header)
  c.delete_all
  t.each{|r| c << r.clone}
  c.header = c[header_row_index] if header_row_index
  return c
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)


112
113
114
115
# File 'lib/workbook/table.rb', line 112

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



71
72
73
74
75
76
77
78
# File 'lib/workbook/table.rb', line 71

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



59
60
61
62
63
# File 'lib/workbook/table.rb', line 59

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:



132
133
134
# File 'lib/workbook/table.rb', line 132

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

#has_contents?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/workbook/table.rb', line 104

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:



45
46
47
48
49
50
51
52
53
# File 'lib/workbook/table.rb', line 45

def header
  if defined?(@header) and @header == false
    false
  elsif defined?(@header) and @header
    @header
  else
    first
  end
end

#header=(h) ⇒ Object



55
56
57
# File 'lib/workbook/table.rb', line 55

def header= h
  @header = h
end

#new_row(cell_values = []) ⇒ Object

Generates a new row, with optionally predefined cell-values, that is already connected to this table.



66
67
68
69
# File 'lib/workbook/table.rb', line 66

def new_row cell_values=[]
  r = Workbook::Row.new(cell_values,self)
  return r
end

#push(row) ⇒ Object

Add row

Parameters:



90
91
92
93
94
# File 'lib/workbook/table.rb', line 90

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:



83
84
85
86
# File 'lib/workbook/table.rb', line 83

def remove_empty_lines!
  self.delete_if{|r| r.nil? or r.compact.empty?}
  self
end

#templateWorkbook::Template

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

Returns:



37
38
39
# File 'lib/workbook/table.rb', line 37

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:



211
212
213
# File 'lib/workbook/table.rb', line 211

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:



219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/workbook/table.rb', line 219

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