class BerkeleyLibrary::TIND::Export::ODSExporter

Exporter for OpenOffice/LibreOffice format

Constants

EIGHTH

Round column widths up to nearest quarter inch

LOCKED_CELL_COLOR
MAX_COLUMN_WIDTH_INCHES

Max column width before wrapping

Public Instance Methods

export(out = nil) click to toggle source

Exports {ExportBase#collection} as an OpenOffice/LibreOffice spreadsheet @overload export

Exports to a new string.
@return [String] a binary string containing the spreadsheet data.

@overload export(out)

Exports to the specified output stream.
@param out [IO] the output stream
@return[void]

@overload export(path)

Exports to the specified file. If `path` denotes a directory, the
spreadsheet will be written as exploded, pretty-printed XML.
@param path [String, Pathname] the path to the output file or directory
@return[void]
@see BerkeleyLibrary::Util::ODS::Spreadsheet#write_exploded_to
# File lib/berkeley_library/tind/export/ods_exporter.rb, line 34
def export(out = nil)
  populate_spreadsheet!
  spreadsheet.write_to(out)
end

Private Instance Methods

can_edit?(col_index) click to toggle source
# File lib/berkeley_library/tind/export/ods_exporter.rb, line 97
def can_edit?(col_index)
  raise ArgumentError, "Invalid column index: #{col_index.inspect}" unless (column = export_table.columns[col_index])

  column.can_edit?
end
color_for(col_index) click to toggle source
# File lib/berkeley_library/tind/export/ods_exporter.rb, line 64
def color_for(col_index)
  can_edit?(col_index) ? nil : LOCKED_CELL_COLOR
end
column_style_for(col_index) click to toggle source
# File lib/berkeley_library/tind/export/ods_exporter.rb, line 82
def column_style_for(col_index)
  column_width = table_metrics.formatted_width(col_index)
  @column_styles_by_width ||= {}
  @column_styles_by_width[column_width] ||= styles.find_or_create_column_style(column_width)
end
find_or_create_cell_style(color:, font_weight: nil, wrap: false) click to toggle source
# File lib/berkeley_library/tind/export/ods_exporter.rb, line 78
def find_or_create_cell_style(color:, font_weight: nil, wrap: false)
  styles.find_or_create_cell_style(color: color, font_weight: font_weight, wrap: wrap)
end
header_cell_style_for(col_index) click to toggle source
# File lib/berkeley_library/tind/export/ods_exporter.rb, line 68
def header_cell_style_for(col_index)
  @header_cell_styles ||= []
  @header_cell_styles[col_index] ||= begin
    find_or_create_cell_style(
      color: color_for(col_index),
      font_weight: 'bold'
    )
  end
end
populate_columns!() click to toggle source

@param table [BerkeleyLibrary::Util::ODS::XML::Table::Table] the table

# File lib/berkeley_library/tind/export/ods_exporter.rb, line 104
def populate_columns!
  export_table.columns.each_with_index do |export_column, col_index|
    table.add_column_with_styles(
      export_column.header,
      column_style: column_style_for(col_index),
      header_cell_style: header_cell_style_for(col_index)
    )
  end
end
populate_row(row_index) click to toggle source
# File lib/berkeley_library/tind/export/ods_exporter.rb, line 119
def populate_row(row_index)
  export_row = export_table.rows[row_index]
  row_metrics = table_metrics.row_metrics_for(row_index)
  row_height = row_metrics.formatted_row_height
  row = table.add_row(row_height)
  populate_values(export_row, row_metrics, row)
end
populate_rows!() click to toggle source

@param table [BerkeleyLibrary::Util::ODS::XML::Table::Table] the table

# File lib/berkeley_library/tind/export/ods_exporter.rb, line 115
def populate_rows!
  export_table.row_count.times(&method(:populate_row))
end
populate_spreadsheet!() click to toggle source

Private utility methods

# File lib/berkeley_library/tind/export/ods_exporter.rb, line 91
def populate_spreadsheet!
  logger.info("Populating spreadsheet for #{collection}")
  populate_columns!
  populate_rows!
end
populate_values(export_row, row_metrics, row) click to toggle source
# File lib/berkeley_library/tind/export/ods_exporter.rb, line 127
def populate_values(export_row, row_metrics, row)
  export_row.each_value.with_index do |v, col_index|
    wrap = row_metrics.wrap?(col_index)
    cell_style = find_or_create_cell_style(color: color_for(col_index), wrap: wrap)
    row.set_value_at(col_index, v, cell_style)
  end
end
spreadsheet() click to toggle source

Private accessors

# File lib/berkeley_library/tind/export/ods_exporter.rb, line 47
def spreadsheet
  @spreadsheet ||= BerkeleyLibrary::Util::ODS::Spreadsheet.new
end
styles() click to toggle source

@return [BerkeleyLibrary::Util::ODS::XML::Office::AutomaticStyles] the styles

# File lib/berkeley_library/tind/export/ods_exporter.rb, line 60
def styles
  spreadsheet.auto_styles
end
table() click to toggle source
# File lib/berkeley_library/tind/export/ods_exporter.rb, line 51
def table
  @table ||= spreadsheet.add_table(collection, protected: false)
end
table_metrics() click to toggle source
# File lib/berkeley_library/tind/export/ods_exporter.rb, line 55
def table_metrics
  @table_metrics ||= TableMetrics.new(export_table)
end