Class: OpenXML::SpreadsheetML::Sheet

Inherits:
Struct
  • Object
show all
Defined in:
lib/xlsx/sheet.rb,
lib/xlsx/sheet.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#dimensionObject

Returns the value of attribute dimension

Returns:

  • (Object)

    the current value of dimension



3
4
5
# File 'lib/xlsx/sheet.rb', line 3

def dimension
  @dimension
end

#merge_cellsObject

Returns the value of attribute merge_cells

Returns:

  • (Object)

    the current value of merge_cells



3
4
5
# File 'lib/xlsx/sheet.rb', line 3

def merge_cells
  @merge_cells
end

#nameObject

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



3
4
5
# File 'lib/xlsx/sheet.rb', line 3

def name
  @name
end

#ridObject

Returns the value of attribute rid

Returns:

  • (Object)

    the current value of rid



3
4
5
# File 'lib/xlsx/sheet.rb', line 3

def rid
  @rid
end

#sheet_dataObject

Returns the value of attribute sheet_data

Returns:

  • (Object)

    the current value of sheet_data



3
4
5
# File 'lib/xlsx/sheet.rb', line 3

def sheet_data
  @sheet_data
end

#sheet_viewsObject

Returns the value of attribute sheet_views

Returns:

  • (Object)

    the current value of sheet_views



3
4
5
# File 'lib/xlsx/sheet.rb', line 3

def sheet_views
  @sheet_views
end

#sheetIdObject

Returns the value of attribute sheetId

Returns:

  • (Object)

    the current value of sheetId



3
4
5
# File 'lib/xlsx/sheet.rb', line 3

def sheetId
  @sheetId
end

Class Method Details

.parser(content) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/xlsx/sheet.rb', line 41

def self.parser content
  doc = Nokogiri::XML content

  # sheetData
  sheet_data = {}
  sheet_data_tags = doc.css('sheetData')
  cell_tags = sheet_data_tags.css('c')
  cell_tags.each do |cell|
    t = cell[:t] 
    v = cell.at_css('v')
    v = v.text if v
    f = cell.at_css('f')
    f = f.text if f
    sheet_data[cell[:r]] = Cell.new(t, v, f)
  end

  # mergeCells
  merge_cells = []
  merge_cells_tags = doc.css('mergeCell')
  merge_cells_tags.each do |mc|
    merge_cells << MergeCell.new(mc[:ref])
  end

  # dimension
  dimension_tag = doc.at_css('dimension')
  dimension = dimension_tag[:ref]

  Sheet.new(dimension, sheet_data, merge_cells)
end

Instance Method Details

#+(sheet) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/xlsx/sheet.rb', line 10

def + sheet
  self.dimension = self.dimension || sheet.dimension
  self.sheet_data = self.sheet_data || sheet.sheet_data
  self.merge_cells = self.merge_cells || sheet.merge_cells
  self.name = self.name || sheet.name
  self.sheetId = self.sheetId || sheet.sheetId
  self.rid = self.rid || sheet.rid
  self
end

#[](index) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/xlsx/sheet.rb', line 20

def [] index
  case index
  when /^[A-Z]+\d+$/
    retrieve_one_cell index
  when /^\d+$/
    retrieve_one_row index
  when /^[A-Z]+$/
    retrieve_one_column index
  when /^(\d+)[\-:](\d+)$/
    retrieve_rows($1, $2)
  when /^([A-Z]+)[\-:]([A-Z]+)$/
    retrieve_columns($1, $2)
  else
    raise IndexError, 'Invalid index'
  end
end

#at_cell(index) ⇒ Object



37
38
39
# File 'lib/xlsx/sheet.rb', line 37

def at_cell index
  retrieve_one_cell index
end

#max_columnObject



71
72
73
# File 'lib/xlsx/sheet.rb', line 71

def max_column
  /([A-Z]+)\d+$/.match(dimension)[1]
end

#max_rowObject



75
76
77
# File 'lib/xlsx/sheet.rb', line 75

def max_row
  /[A-Z]+(\d+)$/.match(dimension)[1]
end