Class: OoxmlParser::Worksheet

Inherits:
OOXMLDocumentObject show all
Defined in:
lib/ooxml_parser/xlsx_parser/xlsx_data/view_model/workbook/worksheet.rb

Instance Attribute Summary collapse

Attributes inherited from OOXMLDocumentObject

#parent

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from OOXMLDocumentObject

#==, add_to_xmls_stack, copy_file_and_rename_to_zip, current_xml, dir, encrypted_file?, get_link_from_rels, unzip_file

Methods included from OoxmlDocumentObjectHelper

#to_hash

Constructor Details

#initializeWorksheet

Returns a new instance of Worksheet.



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ooxml_parser/xlsx_parser/xlsx_data/view_model/workbook/worksheet.rb', line 23

def initialize
  @columns = []
  @name = ''
  @rows = []
  @merge = []
  @charts = []
  @hyperlinks = []
  @drawings = []
  @sheet_views = []
  @table_parts = []
end

Instance Attribute Details

#autofilterObject

Returns the value of attribute autofilter.



12
13
14
# File 'lib/ooxml_parser/xlsx_parser/xlsx_data/view_model/workbook/worksheet.rb', line 12

def autofilter
  @autofilter
end

#chartsObject

Returns the value of attribute charts.



12
13
14
# File 'lib/ooxml_parser/xlsx_parser/xlsx_data/view_model/workbook/worksheet.rb', line 12

def charts
  @charts
end

#columnsObject

Returns the value of attribute columns.



12
13
14
# File 'lib/ooxml_parser/xlsx_parser/xlsx_data/view_model/workbook/worksheet.rb', line 12

def columns
  @columns
end

#commentsObject

Returns the value of attribute comments.



12
13
14
# File 'lib/ooxml_parser/xlsx_parser/xlsx_data/view_model/workbook/worksheet.rb', line 12

def comments
  @comments
end

#drawingsObject

Returns the value of attribute drawings.



12
13
14
# File 'lib/ooxml_parser/xlsx_parser/xlsx_data/view_model/workbook/worksheet.rb', line 12

def drawings
  @drawings
end

#extension_listExtensionList

Returns list of extensions.

Returns:



21
22
23
# File 'lib/ooxml_parser/xlsx_parser/xlsx_data/view_model/workbook/worksheet.rb', line 21

def extension_list
  @extension_list
end

Returns the value of attribute hyperlinks.



12
13
14
# File 'lib/ooxml_parser/xlsx_parser/xlsx_data/view_model/workbook/worksheet.rb', line 12

def hyperlinks
  @hyperlinks
end

#mergeObject

Returns the value of attribute merge.



12
13
14
# File 'lib/ooxml_parser/xlsx_parser/xlsx_data/view_model/workbook/worksheet.rb', line 12

def merge
  @merge
end

#nameObject

Returns the value of attribute name.



12
13
14
# File 'lib/ooxml_parser/xlsx_parser/xlsx_data/view_model/workbook/worksheet.rb', line 12

def name
  @name
end

#ole_objectsRelationships

Returns array of ole objects.

Returns:



19
20
21
# File 'lib/ooxml_parser/xlsx_parser/xlsx_data/view_model/workbook/worksheet.rb', line 19

def ole_objects
  @ole_objects
end

#relationshipsRelationships

Returns array of relationships.

Returns:



17
18
19
# File 'lib/ooxml_parser/xlsx_parser/xlsx_data/view_model/workbook/worksheet.rb', line 17

def relationships
  @relationships
end

#rowsObject

Returns the value of attribute rows.



12
13
14
# File 'lib/ooxml_parser/xlsx_parser/xlsx_data/view_model/workbook/worksheet.rb', line 12

def rows
  @rows
end

#sheet_format_propertiesObject

Returns the value of attribute sheet_format_properties.



12
13
14
# File 'lib/ooxml_parser/xlsx_parser/xlsx_data/view_model/workbook/worksheet.rb', line 12

def sheet_format_properties
  @sheet_format_properties
end

#sheet_viewsObject

Returns the value of attribute sheet_views.



12
13
14
# File 'lib/ooxml_parser/xlsx_parser/xlsx_data/view_model/workbook/worksheet.rb', line 12

def sheet_views
  @sheet_views
end

#table_partsObject

Returns the value of attribute table_parts.



12
13
14
# File 'lib/ooxml_parser/xlsx_parser/xlsx_data/view_model/workbook/worksheet.rb', line 12

def table_parts
  @table_parts
end

#xml_nameString

Returns xml name of sheet.

Returns:

  • (String)

    xml name of sheet



15
16
17
# File 'lib/ooxml_parser/xlsx_parser/xlsx_data/view_model/workbook/worksheet.rb', line 15

def xml_name
  @xml_name
end

Class Method Details

.parse(path_to_xml_file, parent: nil) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/ooxml_parser/xlsx_parser/xlsx_data/view_model/workbook/worksheet.rb', line 59

def self.parse(path_to_xml_file, parent: nil)
  worksheet = Worksheet.new
  worksheet.xml_name = File.basename path_to_xml_file
  worksheet.parse_relationships
  worksheet.parent = parent
  OOXMLDocumentObject.add_to_xmls_stack("#{OOXMLDocumentObject.root_subfolder}/worksheets/#{File.basename(path_to_xml_file)}")
  doc = Nokogiri::XML(File.open(OOXMLDocumentObject.current_xml))
  sheet = doc.search('//xmlns:worksheet').first
  sheet.xpath('*').each do |worksheet_node_child|
    case worksheet_node_child.name
    when 'sheetData'
      worksheet_node_child.xpath('xmlns:row').each do |row_node|
        worksheet.rows[row_node.attribute('r').value.to_i - 1] = XlsxRow.new(parent: worksheet).parse(row_node)
        worksheet.rows[row_node.attribute('r').value.to_i - 1].style = CellStyle.new(parent: worksheet).parse(row_node.attribute('s').value) unless row_node.attribute('s').nil?
      end
    when 'sheetFormatPr'
      if !worksheet_node_child.attribute('defaultColWidth').nil? && !worksheet_node_child.attribute('defaultRowHeight').nil?
        worksheet.sheet_format_properties = SheetFormatProperties.new(parent: worksheet).parse(worksheet_node_child)
      end
    when 'mergeCells'
      worksheet_node_child.xpath('xmlns:mergeCell').each do |merge_node|
        worksheet.merge << merge_node.attribute('ref').value.to_s
      end
    when 'drawing'
      path_to_drawing = OOXMLDocumentObject.get_link_from_rels(worksheet_node_child.attribute('id').value)
      unless path_to_drawing.nil?
        OOXMLDocumentObject.add_to_xmls_stack(path_to_drawing)
        worksheet.parse_drawing
        OOXMLDocumentObject.xmls_stack.pop
      end
    when 'hyperlinks'
      worksheet_node_child.xpath('xmlns:hyperlink').each do |hyperlink_node|
        worksheet.hyperlinks << Hyperlink.new(parent: worksheet).parse(hyperlink_node).dup
      end
    when 'cols'
      worksheet.columns = XlsxColumnProperties.parse_list(worksheet_node_child, parent: worksheet)
    when 'autoFilter'
      worksheet.autofilter = Autofilter.new(parent: self).parse(worksheet_node_child)
    when 'tableParts'
      worksheet_node_child.xpath('*').each do |part_node|
        worksheet.table_parts << TablePart.new(parent: worksheet).parse(part_node)
      end
    when 'sheetViews'
      worksheet_node_child.xpath('*').each do |view_child|
        worksheet.sheet_views << SheetView.new(parent: worksheet).parse(view_child)
      end
    when 'oleObjects'
      worksheet.ole_objects = OleObjects.new(parent: worksheet).parse(worksheet_node_child)
    when 'extLst'
      worksheet.extension_list = ExtensionList.new(parent: self).parse(worksheet_node_child)
    end
  end
  worksheet.comments = ExcelComments.parse_file(File.basename(path_to_xml_file), OOXMLDocumentObject.path_to_folder)
  OOXMLDocumentObject.xmls_stack.pop
  worksheet
end

Instance Method Details

#parse_drawingObject

Parse list of drawings in file



52
53
54
55
56
57
# File 'lib/ooxml_parser/xlsx_parser/xlsx_data/view_model/workbook/worksheet.rb', line 52

def parse_drawing
  drawing_node = Nokogiri::XML(File.open(OOXMLDocumentObject.current_xml))
  drawing_node.xpath('xdr:wsDr/*').each do |drawing_node_child|
    @drawings << XlsxDrawing.new(parent: self).parse(drawing_node_child)
  end
end

#parse_relationshipsObject



35
36
37
38
39
# File 'lib/ooxml_parser/xlsx_parser/xlsx_data/view_model/workbook/worksheet.rb', line 35

def parse_relationships
  OOXMLDocumentObject.add_to_xmls_stack("#{OOXMLDocumentObject.root_subfolder}/worksheets/_rels/#{@xml_name}.rels")
  @relationships = Relationships.parse_rels(OOXMLDocumentObject.current_xml) if File.exist?(OOXMLDocumentObject.current_xml)
  OOXMLDocumentObject.xmls_stack.pop
end

#with_data?True, false

Returns if structure contain any user data.

Returns:

  • (True, false)

    if structure contain any user data



42
43
44
45
46
47
48
49
# File 'lib/ooxml_parser/xlsx_parser/xlsx_data/view_model/workbook/worksheet.rb', line 42

def with_data?
  return true unless @rows.empty?
  return true unless @columns.empty?
  return true unless @drawings.empty?
  return true unless @charts.empty?
  return true unless @hyperlinks.empty?
  false
end