Class: Shoji::ODS::Reader

Inherits:
Object
  • Object
show all
Defined in:
lib/shoji/ods/reader.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ Reader

Returns a new instance of Reader.



14
15
16
# File 'lib/shoji/ods/reader.rb', line 14

def initialize(filename)
  @filename = filename
end

Instance Attribute Details

#skip_empty_rowObject

Returns the value of attribute skip_empty_row.



6
7
8
# File 'lib/shoji/ods/reader.rb', line 6

def skip_empty_row
  @skip_empty_row
end

Class Method Details

.open(filename, sheet_name = nil, &block) ⇒ Object



8
9
10
11
12
# File 'lib/shoji/ods/reader.rb', line 8

def self.open(filename, sheet_name = nil, &block)
  reader = new(filename)
  reader.skip_empty_row = false
  reader.process_book(sheet_name, &block)
end

.valid_file?(filename) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/shoji/ods/reader.rb', line 44

def self.valid_file?(filename)
  new(filename).valid_file?
end

Instance Method Details

#process_book(sheet_name = nil, &block) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/shoji/ods/reader.rb', line 18

def process_book(sheet_name = nil, &block)
  docbytes = read_from_zip_content_xml(@filename)

  doc = Nokogiri::XML(docbytes)
  path = "//table:table"
  path += "[@table:name='#{sheet_name}']" if sheet_name
  ws = doc.at_xpath path
  process_sheet(ws, &block)
end

#process_row(rowreps, row, &block) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/shoji/ods/reader.rb', line 56

def process_row(rowreps, row, &block)
  cols = []
  index = 0
  has_value = false
  row.xpath('table:table-cell').each do |cell|
    tv = typed_value cell
    if tv && tv != ''
      cols[index] = tv
      has_value = true
    else
      cols[index] = ''
    end
    colreps = cell['number-columns-repeated']
    if colreps
      colreps.to_i.times do |num|
        cols[index + num] = cols[index]
      end
      index = index + colreps.to_i
    else
      index = index + 1
    end
  end
  cols = regulate_trailing_blank_cols(cols)
  rowreps.times do |num|
    if has_value
      block.call(cols)
    elsif !skip_empty_row
      block.call(cols)
    end
  end
end

#process_sheet(sheet, &block) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/shoji/ods/reader.rb', line 48

def process_sheet(sheet, &block)
  sheet.xpath('table:table-row').each do |row|
    rowreps = row['table:number-rows-repeated'] || '1'
    rowreps = rowreps.to_i
    process_row(rowreps, row, &block)
  end
end

#rows(opts = {}) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/shoji/ods/reader.rb', line 37

def rows(opts = {})
  result = []
  process_book(opts[:sheet]) do |row|
    result << row
  end
  result
end

#valid_file?Boolean

Returns:

  • (Boolean)


28
29
30
31
32
33
34
35
36
# File 'lib/shoji/ods/reader.rb', line 28

def valid_file?
  valid = true
  begin
    read_from_zip_content_xml(@filename, true)
  rescue
    valid = false
  end
  valid
end