Class: Excel::Worksheet

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

Overview

Implements a worksheet.

Worksheets are part of a Workbook and contain Row

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = nil, options = {}) ⇒ Worksheet

Returns a new instance of Worksheet.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/rexcel/worksheet.rb', line 9

def initialize( name = nil, options = {})
  @log = options[:log] || LOGGER
  @name = name || 'Sheet'
  #Array with columns sequence. Needed to insert Hashes.

  @columns = {}
  @rows = []
  options.each{|key,value|
    case key
      when :log
      else
        @log.warn("Excel::Worksheet: undefined option #{option}")
    end
  }
end

Instance Attribute Details

#nameObject (readonly)

Name of the sheet



24
25
26
# File 'lib/rexcel/worksheet.rb', line 24

def name
  @name
end

#rowsObject (readonly)

Array with rows



26
27
28
# File 'lib/rexcel/worksheet.rb', line 26

def rows
  @rows
end

Instance Method Details

#<<(insertion) ⇒ Object

Add content to the worksheet.

  • Array: Added as Row to the actual worksheet.

  • Hash: Added as Row to the actual worksheet. Requires the definition of columns

  • Row: Added the actual worksheet.

If no actual worksheet is available, a new worksheet i created.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/rexcel/worksheet.rb', line 35

def << (insertion)
  case insertion
    when Column
      @columns[insertion.id] = insertion
    when Array
      @rows << row = Row.new()
      row << insertion
    when Hash
      raise ArgumentError, "#{Worksheet}: Hash without heading data" if @columns.empty?
      @rows << row = Row.new()
      #Hashes keep the order of entry. -> no internal order necessary

      @columns.each{|id, column|
        row << insertion[id]
      }
    when Row
      @rows << insertion
    else
      raise ArgumentError, "#{Worksheet}: Wrong insertion type #{insertion.inspect}"
  end
end

#add_title_row(columns = @columns, style = Style.new("Title_row#{object_id}", :bold => true)) ⇒ Object

Add a title line from columns data.

Raises:

  • (ArgumentError)


60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/rexcel/worksheet.rb', line 60

def add_title_row(columns = @columns, style = Style.new("Title_row#{object_id}", :bold => true))
  #~ raise ArgumentError, "#{Worksheet}##{__method__}: No columns list found" unless columns.respond_to?(:each)

  raise ArgumentError, "#{Worksheet}##{__method__}: No columns list found" unless columns.is_a?(Hash)
  raise ArgumentError, "#{Worksheet}##{__method__}: Columns list empty" if columns.empty?
  
  @rows << row = Row.new(:style => style )
  #Hashes keep the order of entry. -> no internal order necessary

  columns.each{|id, column|
    row << Cell.new(column.title)
  }
  row
end

#to_csv(options = {}) ⇒ Object

Build a csv for the work sheet.

The csv is optimized to be used by Excel. The field content are converted for this target.

Probably the csv expects a German Excel. Details see Rexcel::Cell#to_csv.

Options:

  • :sep cell separator. default: ; (as expected from Excel)

  • :filename: Store the data to a file (optional).

  • :encoding: Used for storage in filename. Default: UTF-16LE

fixme
  • :filehandle



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/rexcel/worksheet.rb', line 127

def to_csv( options = {} )
  @log.debug("Prepare Worksheet #{@name} for csv-output")
  (options.keys - [:sep, :filename, :encoding] ).each{|option|
    @log.warn("Undefined option #{option} in #{self.class}##{__method__}")
  }
  #Set options defaults

  options[:encoding] ||= "UTF-16LE"
  #~ options[:filename] ||= 'test.csv'  #no default 


  csv = []
  
  @rows.each_with_index{|row, rownum|        
    @log.debug("Prepare row #{rownum} for csv-output") if @log.debug?
    csv << row.to_csv(options)
  }#rows

  
  #Save to file if requested

  if options[:filename]
    File.open(options[:filename], 'w', :encoding => options[:encoding] ){|csv_file|
      csv_file << csv.join("\n")
    }
  end #if options[:filename]

  
  csv.join("\n")
end

#to_xls(ws) ⇒ Object

Build the xls for the work sheet.

Receives a OLE-Worksheet.



95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/rexcel/worksheet.rb', line 95

def to_xls(ws)
  @log.debug("Prepare Worksheet #{@name}")
  ws.Name = @name
  
  #fixme: Add styles

  #~ @log.warn("XLS contains no styles")

  
  @rows.each_with_index{|row, rownum|
    row.columns.each_with_index{|col, colnum|
      @log.debug("Prepare cell #{rownum+1},#{colnum+1}")
      col.to_xls( ws.Cells(rownum+1, colnum+1), row )
    }#columns in row

  }#rows

end

#to_xml(xmlbuilder, ns) ⇒ Object

Build the xml for the work sheet.

ns must be a method-object to implement the namespace definitions.

Raises:



77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/rexcel/worksheet.rb', line 77

def to_xml(xmlbuilder, ns)
  raise EmptyError, "Worksheet #{@name} without content" if @rows.empty?

  @log.debug("Prepare XML for worksheet #{@name}")
  xmlbuilder[ns.call].Worksheet(ns.call('Name') => @name){
    xmlbuilder[ns.call].Table(){
      @columns.each{|id,column| column.to_xml(xmlbuilder, ns) }
      @rows.each{|row|
        row.to_xml(xmlbuilder, ns)
      } #row

    }
  }
end