class Dullard::Workbook

Constants

FORMATS

Code borrowed from Roo (github.com/hmcgowan/roo/blob/master/lib/roo/excelx.rb) Some additional formats added by Paul Hendryx (phendryx@gmail.com) that are common in LibreOffice.

STANDARD_FORMATS

Public Class Methods

new(file, user_defined_formats = {}) click to toggle source
# File lib/dullard/reader.rb, line 84
def initialize(file, user_defined_formats = {})
  @file = file
  begin
    @zipfs = Zip::File.open(@file)
  rescue Zip::Error => e
    raise Dullard::Error, e.message
  end
  @user_defined_formats = user_defined_formats
  read_styles
end

Public Instance Methods

attribute2format(s) click to toggle source

Code borrowed from Roo (github.com/hmcgowan/roo/blob/master/lib/roo/excelx.rb) convert internal excelx attribute to a format

# File lib/dullard/reader.rb, line 163
def attribute2format(s)
  id = @cell_xfs[s.to_i].to_i
  result = @num_formats[id]

  if result == nil
    if STANDARD_FORMATS.has_key? id
      result = STANDARD_FORMATS[id]
    end
  end

  result.downcase
end
close() click to toggle source
# File lib/dullard/reader.rb, line 191
def close
  @zipfs.close
end
format2type(format) click to toggle source

Code borrowed from Roo (github.com/hmcgowan/roo/blob/master/lib/roo/excelx.rb)

# File lib/dullard/reader.rb, line 177
def format2type(format)
  if FORMATS.has_key? format
    FORMATS[format]
  elsif @user_defined_formats.has_key? format
    @user_defined_formats[format]
  else
    :float
  end
end
read_string_table() click to toggle source
# File lib/dullard/reader.rb, line 110
def read_string_table
  return [] unless @zipfs.file.exist? Dullard::SharedStringPath

  begin
    shared_string = @zipfs.file.open(Dullard::SharedStringPath)
  rescue Zip::Error
    raise Dullard::Error, 'Invalid file, could not open shared string file.'
  end

  entry = ''
  @string_table = []
  Nokogiri::XML::Reader(shared_string).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
read_styles() click to toggle source
# File lib/dullard/reader.rb, line 133
def read_styles
  @num_formats = {}
  @cell_xfs = []
  return unless @zipfs.file.exist? Dullard::StylesPath

  begin
    doc = Nokogiri::XML(@zipfs.file.open(Dullard::StylesPath))
  rescue Zip::Error
    raise Dullard::Error, 'Invalid file, could not open styles'
  end
  
  doc.css('/styleSheet/numFmts/numFmt').each do |numFmt|
    if numFmt.attributes['numFmtId'] && numFmt.attributes['formatCode']
      numFmtId = numFmt.attributes['numFmtId'].value.to_i
      formatCode = numFmt.attributes['formatCode'].value
      @num_formats[numFmtId] = formatCode
    end
  end

  doc.css('/styleSheet/cellXfs/xf').each do |xf|
    if xf.attributes['numFmtId']
      numFmtId = xf.attributes['numFmtId'].value.to_i
      @cell_xfs << numFmtId
    end
  end
end
sheets() click to toggle source
# File lib/dullard/reader.rb, line 95
def sheets
  begin
    workbook = Nokogiri::XML::Document.parse(@zipfs.file.open('xl/workbook.xml'))
  rescue Zip::Error
    raise Dullard::Error, 'Invalid file, could not open xl/workbook.xml'
  end
  @sheets = workbook.css('sheet').each_with_index.map do |n, i|
    Dullard::Sheet.new(self, n.attr('name'), n.attr('sheetId'), i+1)
  end
end
string_table() click to toggle source
# File lib/dullard/reader.rb, line 106
def string_table
  @string_table ||= read_string_table
end
zipfs() click to toggle source
# File lib/dullard/reader.rb, line 187
def zipfs
  @zipfs
end