Class: OOXML::Excel::Sheet

Inherits:
Object
  • Object
show all
Includes:
Helper::List, Util
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

Constant Summary

Constants included from Util

Util::COLUMN_LETTERS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util

#letter_equivalent, #letter_index, #uniform_reference

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.



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

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.



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

def columns
  @columns
end

#commentsObject

Returns the value of attribute comments.



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

def comments
  @comments
end

#data_validationsObject (readonly)

Returns the value of attribute data_validations.



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

def data_validations
  @data_validations
end

#defined_namesObject

Returns the value of attribute defined_names.



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

def defined_names
  @defined_names
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#shared_stringsObject (readonly)

Returns the value of attribute shared_strings.



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

def shared_strings
  @shared_strings
end

#stylesObject

Returns the value of attribute styles.



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

def styles
  @styles
end

Class Method Details

.load_from_stream(xml_stream, shared_strings) ⇒ Object



103
104
105
# File 'lib/ooxml_excel/sheet.rb', line 103

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



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

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

#code_nameObject



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

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

#column(id) ⇒ Object



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

def column(id)
  uniformed_reference = uniform_reference(id)
  columns.find { |column| column.id_range.include?(uniformed_reference)}
end

#data_validation(cell_ref) ⇒ Object



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

def data_validation(cell_ref)
  data_validations.find { |data_validation| data_validation.in_sqref_range?(cell_ref)}
end

#each_rowObject



60
61
62
63
64
# File 'lib/ooxml_excel/sheet.rb', line 60

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

#each_row_as_objectObject



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

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

#fill(cell_reference) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/ooxml_excel/sheet.rb', line 86

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

def styles(cell_reference)

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

end



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

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

#row(index) ⇒ Object



46
47
48
# File 'lib/ooxml_excel/sheet.rb', line 46

def row(index)
  rows.find { |row| row.id == index.to_s}
end

#rowsObject



50
51
52
53
54
55
56
57
58
# File 'lib/ooxml_excel/sheet.rb', line 50

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, styles)
    end
  end
end