class BerkeleyLibrary::Util::ODS::XML::Table::Table

Constants

MIN_COLUMNS

Constants

MIN_ROWS

Attributes

column_count[W]

Private writers

row_count[W]
styles[R]

@return [XML::Office::AutomaticStyles] the document styles

table_name[R]

Accessors

table_style[R]

Public Class Methods

new(table_name, table_style = nil, styles:, protected: true) click to toggle source

Initializes a new table

@param name [String] the table name @param style [XML::Style::TableStyle] the table style, if other than default @param styles [XML::Office::AutomaticStyles] the document styles @param protected [Boolean] whether the table is protected

# File lib/berkeley_library/util/ods/xml/table/table.rb, line 38
def initialize(table_name, table_style = nil, styles:, protected: true)
  super(:table, 'table', doc: styles.doc)

  @table_name = table_name
  @table_style = table_style || styles.default_style(:table)
  @styles = styles

  set_attribute('name', self.table_name)
  set_attribute('style-name', self.table_style.style_name)

  protect! if protected
end

Public Instance Methods

add_child(child) click to toggle source

Public XML::ElementNode overrides

# File lib/berkeley_library/util/ods/xml/table/table.rb, line 108
def add_child(child)
  return child.tap { |column| columns << column } if child.is_a?(TableColumn)
  return child.tap { |row| rows << row } if child.is_a?(TableRow)

  child.tap { |c| other_children << c }
end
add_column(header, width = nil, protected: false) click to toggle source
# File lib/berkeley_library/util/ods/xml/table/table.rb, line 66
def add_column(header, width = nil, protected: false)
  add_column_with_styles(
    header,
    column_style: styles.find_or_create_column_style(width),
    default_cell_style: styles.find_or_create_cell_style(protected)
  )
end
add_column_with_styles(header, column_style:, default_cell_style: nil, header_cell_style: nil) click to toggle source
# File lib/berkeley_library/util/ods/xml/table/table.rb, line 74
def add_column_with_styles(header, column_style:, default_cell_style: nil, header_cell_style: nil)
  cell_style = default_cell_style || styles.find_or_create_cell_style
  add_or_repeat_column(column_style, cell_style).tap do
    header_row = rows[0] || add_row
    header_row.set_value_at(column_count, header, header_cell_style)
    self.column_count += 1
  end
end
add_empty_columns(number_repeated, width = nil, protected: false) click to toggle source
# File lib/berkeley_library/util/ods/xml/table/table.rb, line 83
def add_empty_columns(number_repeated, width = nil, protected: false)
  column_style = styles.find_or_create_column_style(width)
  default_cell_style = styles.find_or_create_cell_style(protected)

  TableColumn.new(column_style, default_cell_style, number_repeated, table: self).tap do |col|
    columns << col
    self.column_count += number_repeated
  end
end
add_row(height = nil, number_repeated = 1) click to toggle source

Adds a new row with the specified height. @param height [String] the row height. Defaults to {XML::Style::RowStyle::DEFAULT_HEIGHT}. @param number_repeated [Integer] the number of identical rows to repeat @return [TableRow] the new row

# File lib/berkeley_library/util/ods/xml/table/table.rb, line 97
def add_row(height = nil, number_repeated = 1)
  row_style = styles.find_or_create_row_style(height)
  TableRow.new(row_style, number_repeated, table: self).tap do |row|
    rows << row
    self.row_count += number_repeated
  end
end
column_count() click to toggle source

Accessors and utility methods

# File lib/berkeley_library/util/ods/xml/table/table.rb, line 54
def column_count
  @column_count ||= 0
end
get_value_at(row_index, column_index) click to toggle source
# File lib/berkeley_library/util/ods/xml/table/table.rb, line 62
def get_value_at(row_index, column_index)
  (row = rows[row_index]) && row.get_value_at(column_index)
end
row_count() click to toggle source
# File lib/berkeley_library/util/ods/xml/table/table.rb, line 58
def row_count
  @row_count ||= 0
end

Protected Instance Methods

children() click to toggle source

Protected XML::ElementNode overrides

# File lib/berkeley_library/util/ods/xml/table/table.rb, line 123
def children
  [other_children, columns, rows].flatten
end
create_element() click to toggle source
# File lib/berkeley_library/util/ods/xml/table/table.rb, line 127
def create_element
  ensure_empty_columns!
  ensure_empty_rows!

  super
end

Private Instance Methods

add_or_repeat_column(column_style, default_cell_style) click to toggle source
# File lib/berkeley_library/util/ods/xml/table/table.rb, line 169
def add_or_repeat_column(column_style, default_cell_style)
  if (last_column = columns.last).nil? || !last_column.has_styles?(column_style, default_cell_style)
    TableColumn.new(column_style, default_cell_style, table: self).tap { |c| columns << c }
  else
    last_column.tap(&:increment_repeats!)
  end
end
columns() click to toggle source

Private readers

# File lib/berkeley_library/util/ods/xml/table/table.rb, line 149
def columns
  @columns ||= []
end
ensure_empty_columns!() click to toggle source

TODO: do we really need to use the LibreOffice default width/height?

# File lib/berkeley_library/util/ods/xml/table/table.rb, line 179
def ensure_empty_columns!
  empty_required = MIN_COLUMNS - column_count
  add_empty_columns(empty_required, '0.889in') if empty_required > 0
end
ensure_empty_rows!() click to toggle source
# File lib/berkeley_library/util/ods/xml/table/table.rb, line 184
def ensure_empty_rows!
  empty_required = MIN_ROWS - row_count
  add_row('0.178in', empty_required) if empty_required > 0
end
other_children() click to toggle source
# File lib/berkeley_library/util/ods/xml/table/table.rb, line 157
def other_children
  @other_children ||= []
end
protect!() click to toggle source

Private utility methods

# File lib/berkeley_library/util/ods/xml/table/table.rb, line 164
def protect!
  set_attribute('protected', 'true')
  add_child(LOExt::TableProtection.new(doc: doc))
end
rows() click to toggle source
# File lib/berkeley_library/util/ods/xml/table/table.rb, line 153
def rows
  @rows ||= []
end