Class: SequencescapeExcel::Worksheet::Base

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model
Defined in:
app/sequencescape_excel/sequencescape_excel/worksheet/base.rb

Overview

Base class for worksheets

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Base

Returns a new instance of Base.



16
17
18
19
20
# File 'app/sequencescape_excel/sequencescape_excel/worksheet/base.rb', line 16

def initialize(attributes = {})
  super
  create_worksheet
  protect if password.present?
end

Instance Attribute Details

#axlsx_worksheetObject

Returns the value of attribute axlsx_worksheet.



13
14
15
# File 'app/sequencescape_excel/sequencescape_excel/worksheet/base.rb', line 13

def axlsx_worksheet
  @axlsx_worksheet
end

#columnsObject

Returns the value of attribute columns.



13
14
15
# File 'app/sequencescape_excel/sequencescape_excel/worksheet/base.rb', line 13

def columns
  @columns
end

#nameObject

Assigns name to a worksheet depending on axlsx worksheet name. Used to assign absolute references to ranges.



36
37
38
# File 'app/sequencescape_excel/sequencescape_excel/worksheet/base.rb', line 36

def name
  @name ||= axlsx_worksheet.name
end

#passwordObject

Returns the value of attribute password.



13
14
15
# File 'app/sequencescape_excel/sequencescape_excel/worksheet/base.rb', line 13

def password
  @password
end

#rangesObject

Returns the value of attribute ranges.



13
14
15
# File 'app/sequencescape_excel/sequencescape_excel/worksheet/base.rb', line 13

def ranges
  @ranges
end

#workbookObject

Returns the value of attribute workbook.



13
14
15
# File 'app/sequencescape_excel/sequencescape_excel/worksheet/base.rb', line 13

def workbook
  @workbook
end

Instance Method Details

#add_row(values = [], style = nil, types = nil) ⇒ Object

Adds row to a worksheet with particular value, style and type for each cell



24
25
26
# File 'app/sequencescape_excel/sequencescape_excel/worksheet/base.rb', line 24

def add_row(values = [], style = nil, types = nil)
  axlsx_worksheet.add_row values, types: types || ([:string] * values.length), style: style
end

#add_rows(num_rows) ⇒ Object

Adds n empty rows



29
30
31
# File 'app/sequencescape_excel/sequencescape_excel/worksheet/base.rb', line 29

def add_rows(num_rows)
  num_rows.times { |_i| add_row }
end

#create_worksheetObject

Creates a worksheet, empty one in this case



58
59
60
# File 'app/sequencescape_excel/sequencescape_excel/worksheet/base.rb', line 58

def create_worksheet
  insert_axlsx_worksheet(worksheet_name)
end

#insert_axlsx_worksheet(name, index = 0) ⇒ Object

Adds axlsx worksheet to a workbook, to a particular place.



52
53
54
# File 'app/sequencescape_excel/sequencescape_excel/worksheet/base.rb', line 52

def insert_axlsx_worksheet(name, index = 0)
  @axlsx_worksheet ||= workbook.insert_worksheet(index, name:) # rubocop:disable Naming/MemoizedInstanceVariableName
end

#protectObject

Protects worksheet, but sizes of rows and columns can be changed



42
43
44
45
46
47
48
# File 'app/sequencescape_excel/sequencescape_excel/worksheet/base.rb', line 42

def protect
  axlsx_worksheet.sheet_protection do |sheet_protection|
    sheet_protection.format_columns = false
    sheet_protection.format_rows = false
    sheet_protection.password = password
  end
end