class OoxmlParser::Worksheet

Properties of worksheet

Attributes

autofilter[RW]
charts[RW]
columns[RW]
comments[RW]
conditional_formattings[R]

@return [Array<ConditionalFormatting>] list of conditional formattings

drawings[RW]
extension_list[RW]

@return [ExtensionList] list of extensions

merge[RW]
name[RW]
ole_objects[RW]

@return [Relationships] array of ole objects

page_margins[R]

@return [PageMargins] page margins settings

page_setup[R]

@return [PageSetup] page setup settings

protected_ranges[R]

@return [Array<ProtectedRange>] list of protected ranges

relationships[RW]

@return [Relationships] array of relationships

rows_raw[R]

@return [Array<Row>] rows in sheet, as in xml structure

sheet_format_properties[RW]
sheet_protection[R]

@return [SheetProtection] protection of sheet

sheet_views[RW]
table_parts[RW]
xml_name[RW]

@return [String] xml name of sheet

Public Class Methods

new(parent: nil) click to toggle source
Calls superclass method OoxmlParser::OOXMLDocumentObject::new
# 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

Public Instance Methods

parse(path_to_xml_file) click to toggle source

Parse data of Worksheet @param path_to_xml_file [String] path to file to parse @return [Worksheet] parsed worksheet

# 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_drawing() click to toggle source

Parse list of drawings in file

# 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_relationships() click to toggle source

Perform parsing of relationships @return [nil]

# 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
rows() click to toggle source

@return [Array<XlsxRow, nil>] list of rows, with nil,

if row data is not stored in xml
# 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?() click to toggle source

@return [True, false] if structure contain any user data

# 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

Private Instance Methods

parse_comments() click to toggle source

Do work for parsing shared comments file

# File lib/ooxml_parser/xlsx_parser/workbook/worksheet.rb, line 173
def parse_comments
  return unless relationships

  comments_target = relationships.target_by_type('comment')
  return if comments_target.empty?

  comments_file = "#{root_object.unpacked_folder}/#{root_object.root_subfolder}/#{comments_target.first.gsub('..', '')}"
  @comments = ExcelComments.new(parent: self).parse(comments_file)
end