Class: Dullard::Workbook

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

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ Workbook

Returns a new instance of Workbook.



7
8
9
10
# File 'lib/dullard/reader.rb', line 7

def initialize(file)
  @file = file
  @zipfs = Zip::ZipFile.open(@file)
end

Instance Method Details

#closeObject



40
41
42
# File 'lib/dullard/reader.rb', line 40

def close
  @zipfs.close
end

#read_string_tableObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/dullard/reader.rb', line 21

def read_string_table
  @string_table = []
  entry = ''
  Nokogiri::XML::Reader(@zipfs.file.open("xl/sharedStrings.xml")).each do |node|
    if node.name == "si" and node.node_type == Nokogiri::XML::Reader::TYPE_ELEMENT
      entry = ''
    elsif node.name == "si" and node.node_type == Nokogiri::XML::Reader::TYPE_END_ELEMENT
      @string_table << entry
    elsif node.value?
      entry << node.value
    end
  end
  @string_table
end

#sheetsObject



12
13
14
15
# File 'lib/dullard/reader.rb', line 12

def sheets
  workbook = Nokogiri::XML::Document.parse(@zipfs.file.open("xl/workbook.xml"))
  @sheets = workbook.css("sheet").each_with_index.map {|n,i| Dullard::Sheet.new(self, n.attr("name"), n.attr("sheetId"), i+1) }
end

#string_tableObject



17
18
19
# File 'lib/dullard/reader.rb', line 17

def string_table
  @string_tabe ||= read_string_table
end

#zipfsObject



36
37
38
# File 'lib/dullard/reader.rb', line 36

def zipfs
  @zipfs
end