Class: Spreadsheet::Worksheet

Inherits:
Object
  • Object
show all
Includes:
DataShift::ExcelBase
Defined in:
lib/datashift/applications/spreadsheet_extensions.rb

Instance Attribute Summary collapse

Attributes included from DataShift::ExcelBase

#excel, #sheet

Instance Method Summary collapse

Methods included from DataShift::ExcelBase

#ar_to_xls, #ar_to_xls_cell, #ar_to_xls_row, #exportable?, max_columns, #open_excel, #parse_headers, #sanitize_sheet_name, #start_excel

Methods included from DataShift::Logging

#logdir, #logdir=, #logger, #verbose

Instance Attribute Details

#header_formatObject

See Spreadsheet::Format



20
21
22
# File 'lib/datashift/applications/spreadsheet_extensions.rb', line 20

def header_format
  @header_format
end

Instance Method Details

#auto_fit_columnsObject



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/datashift/applications/spreadsheet_extensions.rb', line 35

def auto_fit_columns
  (0...column_count).each do |col_idx|
    column = column(col_idx)

    column.width = column.each_with_index.map do |cell, row|
      chars = cell.present? ? cell.to_s.strip.split('').count + 3 : 1
      ratio = row(row).format(col_idx).font.size / 10.0
      (chars * ratio).round
    end.max
  end
  self
end

#auto_fit_rowsObject



48
49
50
51
52
53
54
55
56
57
# File 'lib/datashift/applications/spreadsheet_extensions.rb', line 48

def auto_fit_rows
  (0...row_count).each do |row_idx|
    row = row(row_idx)
    row.height = row.each_with_index.map do |cell, col_idx|
      lines = cell.present? ? cell.to_s.strip.split("\n").count + 1 : 1
      lines * row.format(col_idx).font.size
    end.max.round
  end
  self
end

#num_rowsObject



59
60
61
# File 'lib/datashift/applications/spreadsheet_extensions.rb', line 59

def num_rows
  rows.size
end

#set_headers(headers, _apply_style = nil) ⇒ Object

Convert array into a header row



24
25
26
27
28
29
30
31
32
33
# File 'lib/datashift/applications/spreadsheet_extensions.rb', line 24

def set_headers(headers, _apply_style = nil)
  return if headers.empty?

  format = header_format || Spreadsheet::Format.new(color: :blue, weight: :bold, size: 12)
  row(0).default_format = format

  headers.each_with_index do |header, i|
    self[0, i] = header.to_s
  end
end