Class: RubyXL::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/rubyXL/parser.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#data_onlyObject (readonly)

Returns the value of attribute data_only.



9
10
11
# File 'lib/rubyXL/parser.rb', line 9

def data_only
  @data_only
end

#num_sheetsObject (readonly)

Returns the value of attribute num_sheets.



9
10
11
# File 'lib/rubyXL/parser.rb', line 9

def num_sheets
  @num_sheets
end

Class Method Details

.convert_to_index(cell_string) ⇒ Object

converts cell string (such as “AA1”) to matrix indices



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rubyXL/parser.rb', line 12

def Parser.convert_to_index(cell_string)
  index = Array.new(2)
  index[0]=-1
  index[1]=-1
  if(cell_string =~ /^([A-Z]+)(\d+)$/)
    one = $1.to_s
    row = Integer($2) - 1 #-1 for 0 indexing
    col = 0
    i = 0
    one = one.reverse #because of 26^i calculation
    one.each_byte do |c|
      int_val = c - 64 #converts A to 1
      col += int_val * 26**(i)
      i=i+1
    end
    col -= 1 #zer0 index
    index[0] = row
    index[1] = col
  end
  return index
end

.parse(file_path, data_only = false) ⇒ Object

data_only allows only the sheet data to be parsed, so as to speed up parsing However, using this option will result in date-formatted cells being interpreted as numbers



37
38
39
40
41
42
43
44
45
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
77
78
79
80
81
82
83
84
85
# File 'lib/rubyXL/parser.rb', line 37

def Parser.parse(file_path, data_only=false)
  @data_only = data_only
  files = Parser.decompress(file_path)
  wb = Parser.fill_workbook(file_path, files)

  if(files['sharedString'] != nil)
    wb.num_strings = Integer(files['sharedString'].css('sst').attribute('count').value())
    wb.size = Integer(files['sharedString'].css('sst').attribute('uniqueCount').value())

    files['sharedString'].css('si').each do |node|
      unless node.css('r').empty?
        text = node.css('r t').children.to_a.join
        node.children.remove
        node << "<t xml:space=\"preserve\">#{text}</t>"
      end
    end

    string_nodes = files['sharedString'].css('si t')
    wb.shared_strings = {}
    string_nodes.each_with_index do |node,i|
      string = node.children.to_s
      wb.shared_strings[i] = string
      wb.shared_strings[string] = i
    end
  end

  unless @data_only
    styles = files['styles'].css('cellXfs xf')
    style_hash = Hash.xml_node_to_hash(files['styles'].root)
    fill_styles(wb,style_hash)

    #will be nil if these files do not exist
    wb.external_links = files['externalLinks']
    wb.drawings = files['drawings']
    wb.printer_settings = files['printerSettings']
    wb.worksheet_rels = files['worksheetRels']
    wb.macros = files['vbaProject']
  end

  #for each worksheet:
  #1. find the dimensions of the data matrix
  #2. Fill in the matrix with data from worksheet/shared_string files
  #3. Apply styles
  wb.worksheets.each_index do |i|
    Parser.fill_worksheet(wb,i,files,wb.shared_strings)
  end

  return wb
end