Class: Rusk::Sheet

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/rusk/sheet.rb

Instance Method Summary collapse

Constructor Details

#initialize(content) ⇒ Sheet

Returns a new instance of Sheet.



7
8
9
10
11
12
13
14
# File 'lib/rusk/sheet.rb', line 7

def initialize(content)
  @content = content
  @cells = []
  rows = @content.xpath('.//table:table-row')
  @row_size = rows.select { |row| row["table:number-rows-repeated"] }.inject(0) { |sum, item| sum + item["table:number-rows-repeated"].to_i} + rows.size
  columns = rows[0].xpath(".//table:table-cell|.//table:covered-table-cell")
  @column_size = columns.select{ |cell| cell["table:number-columns-repeated"] }.inject(0){ |sum, item| sum + item["table:number-columns-repeated"].to_i } + columns.size
end

Instance Method Details

#[](row, column) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rusk/sheet.rb', line 24

def [](row, column)
  return nil if @row_size < row || @column_size < column
  return @cells[row][column] if @cells[row]

  row_index = 0
  each_row(force: true) do |row_range|
    break if row_index > row
    row_index += 1
  end

  @cells[row][column]
end

#each(&block) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/rusk/sheet.rb', line 37

def each(&block)
  return Enumerator.new(self) unless block
  each_row do |row_range|
    row_range.each do |cell|
      yield cell
    end
  end
end

#each_column(&block) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/rusk/sheet.rb', line 78

def each_column(&block)
  return Enumerator.new(self, :each_column) unless block
  self.each_row{|i| i}
  @cells.transpose.each do |columns|
    yield columns
  end
end

#each_row(options = {force: false}, &block) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/rusk/sheet.rb', line 46

def each_row(options = {force: false}, &block)
  return Enumerator.new(self, :each_row) unless block
  row_index = 0
  @content.xpath('.//table:table-row').each_with_index do |row_range, index|
    if @cells[row_index]
      yield @cells[row_index]
      row_index += 1
      next
    end
    rows_repeated = row_range["table:number-rows-repeated"].to_i
    break if rows_repeated + row_index + 1 >= 1048576 && options[:force] == false

    cells = row_cells(row_range)
    yield cells
    @cells << cells
    row_index += 1

    if rows_repeated > 1
      base_row_range = row_range
      (rows_repeated - 1).times do |i|
        base_row_range.remove_attribute("number-rows-repeated")
        base_row_range = base_row_range.add_next_sibling(row_range.dup)
        base_row_range["table:number-rows-repeated"] = (rows_repeated - i)
        cells = row_cells(base_row_range)
        yield cells
        @cells << cells
        row_index += 1
      end
    end
  end
end

#nameObject



16
17
18
# File 'lib/rusk/sheet.rb', line 16

def name
  @content["table:name"]
end

#name=(name) ⇒ Object



20
21
22
# File 'lib/rusk/sheet.rb', line 20

def name= name
  @content["table:name"] = name
end