Class: OOXML::Excel::Sheet

Inherits:
Object
  • Object
show all
Includes:
Helper::List
Defined in:
lib/ooxml_excel/sheet.rb,
lib/ooxml_excel/sheet.rb,
lib/ooxml_excel/sheet.rb,
lib/ooxml_excel/sheet.rb,
lib/ooxml_excel/sheet.rb

Defined Under Namespace

Classes: Column, DataValidation, Row

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helper::List

#list_value_formula, #list_values, #list_values_from_formula

Constructor Details

#initialize(xml, shared_strings) ⇒ Sheet

Returns a new instance of Sheet.



8
9
10
11
12
13
14
# File 'lib/ooxml_excel/sheet.rb', line 8

def initialize(xml, shared_strings)
  @xml = xml
  @shared_strings = shared_strings
  @comments = {}
  @defined_names = {}
  @styles = []
end

Instance Attribute Details

#columnsObject (readonly)

Returns the value of attribute columns.



5
6
7
# File 'lib/ooxml_excel/sheet.rb', line 5

def columns
  @columns
end

#commentsObject

Returns the value of attribute comments.



6
7
8
# File 'lib/ooxml_excel/sheet.rb', line 6

def comments
  @comments
end

#data_validationsObject (readonly)

Returns the value of attribute data_validations.



5
6
7
# File 'lib/ooxml_excel/sheet.rb', line 5

def data_validations
  @data_validations
end

#defined_namesObject

Returns the value of attribute defined_names.



6
7
8
# File 'lib/ooxml_excel/sheet.rb', line 6

def defined_names
  @defined_names
end

#nameObject

Returns the value of attribute name.



6
7
8
# File 'lib/ooxml_excel/sheet.rb', line 6

def name
  @name
end

#shared_stringsObject (readonly)

Returns the value of attribute shared_strings.



5
6
7
# File 'lib/ooxml_excel/sheet.rb', line 5

def shared_strings
  @shared_strings
end

#stylesObject

Returns the value of attribute styles.



6
7
8
# File 'lib/ooxml_excel/sheet.rb', line 6

def styles
  @styles
end

Class Method Details

.load_from_stream(xml_stream, shared_strings) ⇒ Object



92
93
94
# File 'lib/ooxml_excel/sheet.rb', line 92

def self.load_from_stream(xml_stream, shared_strings)
  self.new(Nokogiri.XML(xml_stream).remove_namespaces!, shared_strings)
end

Instance Method Details

#[](id) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/ooxml_excel/sheet.rb', line 37

def [](id)
  if id.is_a?(String)
    rows.find { |row| row.id == id}
  else
    rows[id]
  end
end

#code_nameObject



16
17
18
# File 'lib/ooxml_excel/sheet.rb', line 16

def code_name
  @code_name ||= @xml.xpath('//sheetPr').attribute('codeName').try(:value)
end

#column(id) ⇒ Object



25
26
27
# File 'lib/ooxml_excel/sheet.rb', line 25

def column(id)
  columns.select { |column| column.id_range.include?(id)}
end

#data_validation_for_cell(cell_ref) ⇒ Object



20
21
22
# File 'lib/ooxml_excel/sheet.rb', line 20

def data_validation_for_cell(cell_ref)
  data_validations.find { |data_validation| data_validation.sqref_range.include?(cell_ref)}
end

#each_rowObject



72
73
74
75
76
# File 'lib/ooxml_excel/sheet.rb', line 72

def each_row
  rows.each_with_index do |row, row_index|
    yield row.cells.map(&:value), row_index
  end
end

#each_row_as_objectObject



78
79
80
81
82
# File 'lib/ooxml_excel/sheet.rb', line 78

def each_row_as_object
  0.upto(rows.size).each do |row_index|
    yield rows[row_index]
  end
end

#fill(cell_reference) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/ooxml_excel/sheet.rb', line 64

def fill(cell_reference)
  style_id = fetch_style_style_id(cell_reference)
  if style_id.present?
    style = @styles.by_id(style_id.to_i)
    (style.present?) ? style[:fill] : nil
  end
end

#font(cell_reference) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/ooxml_excel/sheet.rb', line 55

def font(cell_reference)
  style_id = fetch_style_style_id(cell_reference)
  if style_id.present?
    style = @styles.by_id(style_id.to_i)

    (style.present?) ? style[:font] : nil
  end
end

#rowsObject



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

def rows
  @rows ||= begin
    # TODO: get the value of merged cells
    # merged_cells = @xml.xpath('//mergeCells/mergeCell').map { |merged_cell| merged_cell.attributes["ref"].try(:value) }
    @xml.xpath('//sheetData/row').map do |row_node|
      Excel::Sheet::Row.load_from_node(row_node, shared_strings)
    end
  end
end