Class: RubyXL::Worksheet

Inherits:
OOXMLTopLevelObject show all
Includes:
LegacyWorksheet, RelationshipSupport
Defined in:
lib/rubyXL/objects/worksheet.rb

Overview

Constant Summary collapse

CONTENT_TYPE =
'application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml'.freeze
REL_TYPE =
'http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet'.freeze

Constants included from LegacyWorksheet

LegacyWorksheet::TEXT_LENGTH_LIMIT_IN_CELL

Constants inherited from OOXMLTopLevelObject

OOXMLTopLevelObject::ROOT, OOXMLTopLevelObject::SAVE_ORDER

Instance Attribute Summary collapse

Attributes included from RelationshipSupport

#generic_storage, #relationship_container

Attributes inherited from OOXMLTopLevelObject

#root

Attributes included from OOXMLObjectInstanceMethods

#local_namespaces

Class Method Summary collapse

Instance Method Summary collapse

Methods included from LegacyWorksheet

#add_cell, #initialize

Methods included from RelationshipSupport

#attach_relationship, #collect_related_objects, included, #load_relationships, #store_relationship

Methods inherited from OOXMLTopLevelObject

#add_to_zip, #file_index, parse_file, set_namespaces

Methods included from OOXMLObjectInstanceMethods

#==, included, #index_in_collection, #init_child_nodes, #initialize, #write_xml

Instance Attribute Details

#rels ⇒ Object

Returns the value of attribute rels.



691
692
693
# File 'lib/rubyXL/objects/worksheet.rb', line 691

def rels
  @rels
end

#sheet_id ⇒ Object

Returns the value of attribute sheet_id.



691
692
693
# File 'lib/rubyXL/objects/worksheet.rb', line 691

def sheet_id
  @sheet_id
end

#sheet_name ⇒ Object

Returns the value of attribute sheet_name.



691
692
693
# File 'lib/rubyXL/objects/worksheet.rb', line 691

def sheet_name
  @sheet_name
end

#state ⇒ Object

Returns the value of attribute state.



691
692
693
# File 'lib/rubyXL/objects/worksheet.rb', line 691

def state
  @state
end

#workbook ⇒ Object

Returns the value of attribute workbook.



691
692
693
# File 'lib/rubyXL/objects/worksheet.rb', line 691

def workbook
  @workbook
end

Class Method Details

.parse(param) ⇒ Object



742
743
744
745
746
747
748
749
750
# File 'lib/rubyXL/objects/worksheet.rb', line 742

def self.parse(param)
  sheet_obj = super
  sheet_obj.sheet_data.rows.each { |r|
    next if r.nil?
    r.worksheet = sheet_obj
    r.cells.each { |c| c.worksheet = sheet_obj unless c.nil? }
  }
  sheet_obj
end

Instance Method Details

#[](row = 0) ⇒ Object

Allows for easier access to sheet_data



767
768
769
# File 'lib/rubyXL/objects/worksheet.rb', line 767

def [](row = 0)
  sheet_data[row]
end

#add_row(row_index = 0, params = {}) ⇒ Object



775
776
777
778
779
# File 'lib/rubyXL/objects/worksheet.rb', line 775

def add_row(row_index = 0, params = {})
  new_row = RubyXL::Row.new(params)
  new_row.worksheet = self
  sheet_data.rows[row_index] = new_row
end

#before_write_xml ⇒ Object

This method may need to be moved higher in the hierarchy



693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
# File 'lib/rubyXL/objects/worksheet.rb', line 693

def before_write_xml # This method may need to be moved higher in the hierarchy
  first_nonempty_row = nil
  last_nonempty_row = 0
  first_nonempty_column = nil
  last_nonempty_column = 0

  if sheet_data then
    sheet_data.rows.each_with_index { |row, row_index|
      next if row.nil? || row.cells.empty?

      first_nonempty_cell = nil
      last_nonempty_cell = 0

      row.cells.each_with_index { |cell, col_index|
        next if cell.nil?
        cell.r = RubyXL::Reference.new(row_index, col_index)

        first_nonempty_cell ||= col_index
        last_nonempty_cell = col_index
      }

      if first_nonempty_cell then # If there's nothing in this row, then +first_nonempty_cell+ will be +nil+.
        last_nonempty_row = row_index
        first_nonempty_row ||= row_index

        first_nonempty_column ||= first_nonempty_cell
        last_nonempty_column = last_nonempty_cell if last_nonempty_cell > last_nonempty_column
      end

      row.r = row_index + 1
      row.spans = "#{first_nonempty_cell + 1}:#{last_nonempty_cell + 1}" unless first_nonempty_cell.nil?
      row.custom_format = (row.style_index.to_i != 0)
    }

    if first_nonempty_row then
      self.dimension ||= RubyXL::WorksheetDimensions.new
      self.dimension.ref = RubyXL::Reference.new(first_nonempty_row, last_nonempty_row,
                                                 first_nonempty_column, last_nonempty_column)
    end

  end

  true
end

#cell_at(ref) ⇒ Object

Raises:

  • (ArgumentError)


760
761
762
763
764
# File 'lib/rubyXL/objects/worksheet.rb', line 760

def cell_at(ref)
  reference = RubyXL::Reference.new(ref)
  raise(ArgumentError, "Invalid reference: #{ref}") unless reference.valid? && reference.single_cell?
  sheet_data&.rows&.[](reference.first_row)&.cells&.[](reference.first_col)
end

#each ⇒ Object



771
772
773
# File 'lib/rubyXL/objects/worksheet.rb', line 771

def each
  sheet_data.rows.each { |row| yield(row) }
end

#get_col_xf(column_index) ⇒ Object



752
753
754
# File 'lib/rubyXL/objects/worksheet.rb', line 752

def get_col_xf(column_index)
  workbook.stylesheet.cell_xfs[get_col_style(column_index)]
end

#get_row_xf(row) ⇒ Object



756
757
758
# File 'lib/rubyXL/objects/worksheet.rb', line 756

def get_row_xf(row)
  workbook.stylesheet.cell_xfs[get_row_style(row)]
end


627
628
629
# File 'lib/rubyXL/objects/worksheet.rb', line 627

def related_objects
  comments + printer_settings
end

#xlsx_path ⇒ Object



738
739
740
# File 'lib/rubyXL/objects/worksheet.rb', line 738

def xlsx_path
  ROOT.join('xl', 'worksheets', "sheet#{file_index}.xml")
end