Class: Rxls::Worksheet

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/rxls/worksheet.rb

Instance Method Summary collapse

Constructor Details

#initialize(o, n, mapping) ⇒ Worksheet

Returns a new instance of Worksheet.



19
20
21
22
23
24
# File 'lib/rxls/worksheet.rb', line 19

def initialize(o,n,mapping)
  @columns = to_row_numbers mapping
  @values = o.Worksheets(n).UsedRange.Value
  headers = @values.shift # skip 1st row
  @headers = @columns.map{|c| headers[c]} 
end

Instance Method Details

#dumpObject



33
34
35
36
37
38
39
40
41
# File 'lib/rxls/worksheet.rb', line 33

def dump
  vcs = @columns.map{|column| ValueCounter.new}
  each do |row_with_number|
    row_with_number[1..-1].each_with_index do |value,index|
      vcs[index] << dump_value(value)
    end
  end 
  vcs.each_with_index{|vc,index| puts "#{@headers[index]}:#{vc}"}
end

#dump_value(v) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/rxls/worksheet.rb', line 43

def dump_value(v)
  case v
  when String
    v == '' ? "String(empty)" : "String"
  else
    v.class
  end
end

#eachObject



26
27
28
29
30
31
# File 'lib/rxls/worksheet.rb', line 26

def each
  @values.each_with_index do |row,column|
    values = @columns ? @columns.map{|r| row[r]} : row 
    yield values.unshift(column) 
  end
end

#to_row_numbers(s) ⇒ Object

‘a’ => [0] ‘a b’ => [0,1]



8
9
10
11
12
13
14
15
16
17
# File 'lib/rxls/worksheet.rb', line 8

def to_row_numbers(s)
  s.split(' ').map do |r|
    r = 96.chr + r if r.length == 1
    s = nil
    r.each_byte do |b|
      s = s ? s + (b-97) : (b-96) * 26
    end
    s
  end
end