Class: RubyFromExcel::Worksheet

Inherits:
Object
  • Object
show all
Defined in:
lib/excelfile/worksheet.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(xml, name = nil, workbook = nil) ⇒ Worksheet

Returns a new instance of Worksheet.



22
23
24
25
26
27
28
29
# File 'lib/excelfile/worksheet.rb', line 22

def initialize(xml,name = nil,workbook = nil)
  self.name = name
  self.workbook = workbook
  self.cells = {}
  @named_references = {}
  load_cells_from xml
  GC.start
end

Instance Attribute Details

#cellsObject

Returns the value of attribute cells.



17
18
19
# File 'lib/excelfile/worksheet.rb', line 17

def cells
  @cells
end

#nameObject

Returns the value of attribute name.



18
19
20
# File 'lib/excelfile/worksheet.rb', line 18

def name
  @name
end

#named_referencesObject (readonly)

Returns the value of attribute named_references.



19
20
21
# File 'lib/excelfile/worksheet.rb', line 19

def named_references
  @named_references
end

#workbookObject

Returns the value of attribute workbook.



20
21
22
# File 'lib/excelfile/worksheet.rb', line 20

def workbook
  @workbook
end

Class Method Details

.from_file(filename, workbook = nil) ⇒ Object



6
7
8
9
10
11
12
13
14
15
# File 'lib/excelfile/worksheet.rb', line 6

def self.from_file(filename,workbook = nil)
  xml = File.open(filename) { |f| Nokogiri::XML(f).root }
  worksheet = Worksheet.new(xml,File.basename(filename,'.xml'),workbook)
  relationships = Relationships.for_file(filename)
  xml.css('tablePart').each do |table_reference_xml|
    table_xml = File.open(relationships[table_reference_xml['id']]) {|f| Nokogiri::XML(f).root }
    Table.from_xml(worksheet,table_xml)
  end
  worksheet
end

Instance Method Details

#cell(reference) ⇒ Object



69
70
71
# File 'lib/excelfile/worksheet.rb', line 69

def cell(reference)
  cells[reference]
end

#class_nameObject



112
113
114
# File 'lib/excelfile/worksheet.rb', line 112

def class_name
  name.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
end

#create_cell_from(xml) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/excelfile/worksheet.rb', line 41

def create_cell_from(xml)
  formula = xml.at_css("f")
  if formula
    formula_type = formula['t']
    ref = formula['ref']
    return SimpleFormulaCell.new(self,xml) unless formula_type
    return SharingFormulaCell.new(self,xml) if formula_type == 'shared' && ref
    return SharedFormulaCell.new(self,xml) if formula_type == 'shared'
    return ArrayingFormulaCell.new(self,xml) if formula_type == 'array' && ref =~ /:/
    return SingleCellArrayFormulaCell.new(self,xml) if formula_type == 'array'
  end
  return ValueCell.new(self,xml) if xml.at_css("v")
  nil
end

#let_cells_alter_other_cells_if_requiredObject



56
57
58
59
60
# File 'lib/excelfile/worksheet.rb', line 56

def let_cells_alter_other_cells_if_required
  cells.each do |reference,cell|
    cell.alter_other_cells_if_required
  end
end

#load_cells_from(xml) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/excelfile/worksheet.rb', line 31

def load_cells_from(xml)
  xml.css("c").each do |cell_xml|
     new_cell = create_cell_from cell_xml
     next unless new_cell
     self.cells[new_cell.reference.to_s] = new_cell
  end
  xml = nil
  let_cells_alter_other_cells_if_required
end

#replace_cell(reference, new_cell) ⇒ Object



73
74
75
# File 'lib/excelfile/worksheet.rb', line 73

def replace_cell(reference,new_cell)
  cells[reference] = new_cell
end

#to_ruby(r = RubyScriptWriter.new) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/excelfile/worksheet.rb', line 77

def to_ruby(r = RubyScriptWriter.new)
  r.put_coding
  r.comment SheetNames.instance.key(variable_name)
  r.put_class class_name, 'Spreadsheet' do
    cells.each do |reference,cell|
      begin
        cell.to_ruby(r)
      rescue Exception => e
        puts "Error in #{cell.inspect}"
        raise
      end
    end
    if workbook.indirects_used
      named_references.each do |name,reference|
        r.put_simple_method name.downcase.gsub(/[^\p{word}]/,'_'), reference
      end
    end
  end
  r.to_s
end

#to_test(r = RubySpecWriter.new) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/excelfile/worksheet.rb', line 98

def to_test(r = RubySpecWriter.new)
  r.put_coding
  r.puts "require_relative '../spreadsheet'"
  r.comment SheetNames.instance.key(variable_name)
  r.put_description "'#{class_name}'" do
    r.put_simple_method variable_name, "$spreadsheet ||= Spreadsheet.new; $spreadsheet.#{variable_name}"
    r.puts
    cells.each do |reference,cell|
      cell.to_test(r)
    end
  end
  r.to_s
end

#variable_nameObject Also known as: to_s



116
117
118
# File 'lib/excelfile/worksheet.rb', line 116

def variable_name
  name.gsub(/([a-z])([A-Z])/,'\1_\2').downcase.gsub(/[^a-z0-9_]/,'_')
end

#work_out_dependenciesObject



62
63
64
65
66
67
# File 'lib/excelfile/worksheet.rb', line 62

def work_out_dependencies
  cells.each do |reference,cell|
    cell.work_out_dependencies
    RubyFromExcel.debug(:dependences,"#{name}.#{cell.reference} -> #{cell.dependencies.inspect}")
  end
end

#worksheet_file_nameObject



122
123
124
# File 'lib/excelfile/worksheet.rb', line 122

def worksheet_file_name
  variable_name
end