Class: OoxmlParser::Worksheet

Inherits:
OOXMLDocumentObject show all
Includes:
WorksheetHelper
Defined in:
lib/ooxml_parser/xlsx_parser/workbook/worksheet.rb

Overview

Properties of worksheet

Instance Attribute Summary collapse

Attributes inherited from OOXMLDocumentObject

#parent

Instance Method Summary collapse

Methods included from WorksheetHelper

#default_columns?

Methods inherited from OOXMLDocumentObject

#==, #boolean_attribute_value, #parse_xml

Methods included from OoxmlObjectAttributeHelper

#attribute_enabled?, #option_enabled?

Methods included from OoxmlDocumentObjectHelper

#to_hash

Constructor Details

#initialize(parent: nil) ⇒ Worksheet

Returns a new instance of Worksheet.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/ooxml_parser/xlsx_parser/workbook/worksheet.rb', line 45

def initialize(parent: nil)
  @columns = []
  @name = ''
  @rows = []
  @rows_raw = []
  @merge = []
  @charts = []
  @hyperlinks = []
  @drawings = []
  @sheet_views = []
  @table_parts = []
  @conditional_formattings = []
  @protected_ranges = []
  super
end

Instance Attribute Details

#autofilterObject

Returns the value of attribute autofilter.



20
21
22
# File 'lib/ooxml_parser/xlsx_parser/workbook/worksheet.rb', line 20

def autofilter
  @autofilter
end

#chartsObject

Returns the value of attribute charts.



20
21
22
# File 'lib/ooxml_parser/xlsx_parser/workbook/worksheet.rb', line 20

def charts
  @charts
end

#columnsObject

Returns the value of attribute columns.



20
21
22
# File 'lib/ooxml_parser/xlsx_parser/workbook/worksheet.rb', line 20

def columns
  @columns
end

#commentsObject

Returns the value of attribute comments.



20
21
22
# File 'lib/ooxml_parser/xlsx_parser/workbook/worksheet.rb', line 20

def comments
  @comments
end

#conditional_formattingsArray<ConditionalFormatting> (readonly)

Returns list of conditional formattings.

Returns:



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

def conditional_formattings
  @conditional_formattings
end

#drawingsObject

Returns the value of attribute drawings.



20
21
22
# File 'lib/ooxml_parser/xlsx_parser/workbook/worksheet.rb', line 20

def drawings
  @drawings
end

#extension_listExtensionList

Returns list of extensions.

Returns:



33
34
35
# File 'lib/ooxml_parser/xlsx_parser/workbook/worksheet.rb', line 33

def extension_list
  @extension_list
end

Returns header and footer.

Returns:



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

def header_footer
  @header_footer
end

Returns the value of attribute hyperlinks.



20
21
22
# File 'lib/ooxml_parser/xlsx_parser/workbook/worksheet.rb', line 20

def hyperlinks
  @hyperlinks
end

#mergeObject

Returns the value of attribute merge.



20
21
22
# File 'lib/ooxml_parser/xlsx_parser/workbook/worksheet.rb', line 20

def merge
  @merge
end

#nameObject

Returns the value of attribute name.



20
21
22
# File 'lib/ooxml_parser/xlsx_parser/workbook/worksheet.rb', line 20

def name
  @name
end

#ole_objectsRelationships

Returns array of ole objects.

Returns:



27
28
29
# File 'lib/ooxml_parser/xlsx_parser/workbook/worksheet.rb', line 27

def ole_objects
  @ole_objects
end

#page_marginsPageMargins (readonly)

Returns page margins settings.

Returns:



29
30
31
# File 'lib/ooxml_parser/xlsx_parser/workbook/worksheet.rb', line 29

def page_margins
  @page_margins
end

#page_setupPageSetup (readonly)

Returns page setup settings.

Returns:



31
32
33
# File 'lib/ooxml_parser/xlsx_parser/workbook/worksheet.rb', line 31

def page_setup
  @page_setup
end

#protected_rangesArray<ProtectedRange> (readonly)

Returns list of protected ranges.

Returns:



41
42
43
# File 'lib/ooxml_parser/xlsx_parser/workbook/worksheet.rb', line 41

def protected_ranges
  @protected_ranges
end

#relationshipsRelationships

Returns array of relationships.

Returns:



25
26
27
# File 'lib/ooxml_parser/xlsx_parser/workbook/worksheet.rb', line 25

def relationships
  @relationships
end

#rows_rawArray<Row> (readonly)

Returns rows in sheet, as in xml structure.

Returns:

  • (Array<Row>)

    rows in sheet, as in xml structure



43
44
45
# File 'lib/ooxml_parser/xlsx_parser/workbook/worksheet.rb', line 43

def rows_raw
  @rows_raw
end

#sheet_format_propertiesObject

Returns the value of attribute sheet_format_properties.



20
21
22
# File 'lib/ooxml_parser/xlsx_parser/workbook/worksheet.rb', line 20

def sheet_format_properties
  @sheet_format_properties
end

#sheet_protectionSheetProtection (readonly)

Returns protection of sheet.

Returns:



39
40
41
# File 'lib/ooxml_parser/xlsx_parser/workbook/worksheet.rb', line 39

def sheet_protection
  @sheet_protection
end

#sheet_viewsObject

Returns the value of attribute sheet_views.



20
21
22
# File 'lib/ooxml_parser/xlsx_parser/workbook/worksheet.rb', line 20

def sheet_views
  @sheet_views
end

#table_partsObject

Returns the value of attribute table_parts.



20
21
22
# File 'lib/ooxml_parser/xlsx_parser/workbook/worksheet.rb', line 20

def table_parts
  @table_parts
end

#xml_nameString

Returns xml name of sheet.

Returns:

  • (String)

    xml name of sheet



23
24
25
# File 'lib/ooxml_parser/xlsx_parser/workbook/worksheet.rb', line 23

def xml_name
  @xml_name
end

Instance Method Details

#parse(path_to_xml_file) ⇒ Worksheet

Parse data of Worksheet

Parameters:

  • path_to_xml_file (String)

    path to file to parse

Returns:



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/ooxml_parser/xlsx_parser/workbook/worksheet.rb', line 91

def parse(path_to_xml_file)
  @xml_name = File.basename path_to_xml_file
  parse_relationships
  root_object.add_to_xmls_stack("#{root_object.root_subfolder}/worksheets/#{File.basename(path_to_xml_file)}")
  doc = parse_xml(root_object.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|
        @rows_raw << XlsxRow.new(parent: self).parse(row_node)
      end
    when 'sheetFormatPr'
      @sheet_format_properties = SheetFormatProperties.new(parent: self).parse(worksheet_node_child)
    when 'mergeCells'
      worksheet_node_child.xpath('xmlns:mergeCell').each do |merge_node|
        @merge << merge_node.attribute('ref').value.to_s
      end
    when 'drawing'
      @drawing = DocxDrawing.new(parent: self).parse(worksheet_node_child)
      path_to_drawing = root_object.get_link_from_rels(@drawing.id)
      unless path_to_drawing.nil?
        root_object.add_to_xmls_stack(path_to_drawing)
        parse_drawing
        root_object.xmls_stack.pop
      end
    when 'hyperlinks'
      worksheet_node_child.xpath('xmlns:hyperlink').each do |hyperlink_node|
        @hyperlinks << Hyperlink.new(parent: self).parse(hyperlink_node).dup
      end
    when 'cols'
      @columns = XlsxColumns.new(parent: self).parse(worksheet_node_child).elements
    when 'autoFilter'
      @autofilter = Autofilter.new(parent: self).parse(worksheet_node_child)
    when 'tableParts'
      worksheet_node_child.xpath('*').each do |part_node|
        @table_parts << TablePart.new(parent: self).parse(part_node)
      end
    when 'sheetViews'
      worksheet_node_child.xpath('*').each do |view_child|
        @sheet_views << SheetView.new(parent: self).parse(view_child)
      end
    when 'oleObjects'
      @ole_objects = OleObjects.new(parent: self).parse(worksheet_node_child)
    when 'pageMargins'
      @page_margins = PageMargins.new(parent: self).parse(worksheet_node_child, :inch)
    when 'pageSetup'
      @page_setup = PageSetup.new(parent: self).parse(worksheet_node_child)
    when 'extLst'
      @extension_list = ExtensionList.new(parent: self).parse(worksheet_node_child)
    when 'headerFooter'
      @header_footer = XlsxHeaderFooter.new(parent: self).parse(worksheet_node_child)
    when 'conditionalFormatting'
      @conditional_formattings << ConditionalFormatting.new(parent: self).parse(worksheet_node_child)
    when 'sheetProtection'
      @sheet_protection = SheetProtection.new(parent: self).parse(worksheet_node_child)
    when 'protectedRanges'
      worksheet_node_child.xpath('*').each do |protected_range_node|
        @protected_ranges << ProtectedRange.new(parent: self).parse(protected_range_node)
      end
    end
  end
  parse_comments
  root_object.xmls_stack.pop
  self
end

#parse_drawingObject

Parse list of drawings in file



81
82
83
84
85
86
# File 'lib/ooxml_parser/xlsx_parser/workbook/worksheet.rb', line 81

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

#parse_relationshipsnil

Perform parsing of relationships

Returns:

  • (nil)


63
64
65
66
67
# File 'lib/ooxml_parser/xlsx_parser/workbook/worksheet.rb', line 63

def parse_relationships
  root_object.add_to_xmls_stack("#{root_object.root_subfolder}/worksheets/_rels/#{@xml_name}.rels")
  @relationships = Relationships.new(parent: self).parse_file(root_object.current_xml) if File.exist?(root_object.current_xml)
  root_object.xmls_stack.pop
end

#rowsArray<XlsxRow, nil>

Returns list of rows, with nil, if row data is not stored in xml.

Returns:

  • (Array<XlsxRow, nil>)

    list of rows, with nil, if row data is not stored in xml



160
161
162
163
164
165
166
167
168
# File 'lib/ooxml_parser/xlsx_parser/workbook/worksheet.rb', line 160

def rows
  return @rows if @rows.any?

  rows_raw.each do |row|
    @rows[row.index - 1] = row
  end

  @rows
end

#with_data?True, false

Returns if structure contain any user data.

Returns:

  • (True, false)

    if structure contain any user data



70
71
72
73
74
75
76
77
78
# File 'lib/ooxml_parser/xlsx_parser/workbook/worksheet.rb', line 70

def with_data?
  return true unless @rows_raw.empty?
  return true unless default_columns?
  return true unless @drawings.empty?
  return true unless @charts.empty?
  return true unless @hyperlinks.empty?

  false
end