Class: Axlsx::Worksheet

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

Overview

The Worksheet class represents a worksheet in the workbook.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(wb, options = {}) ⇒ Worksheet

Note:

the recommended way to manage worksheets is Workbook#add_worksheet

Creates a new worksheet.

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • name (String)

    The name of this sheet.

See Also:



59
60
61
62
63
64
65
66
# File 'lib/axlsx/workbook/worksheet/worksheet.rb', line 59

def initialize(wb, options={})
  @rows = SimpleTypedList.new Row
  self.workbook = wb
  @workbook.worksheets << self
  @auto_fit_data = []
  self.name = options[:name] || "Sheet" + (index+1).to_s
  @magick_draw = Magick::Draw.new
end

Instance Attribute Details

#auto_fit_dataArray (readonly)

Note:

a single auto fit data item is a hash with :longest => [String] and :sz=> [Integer] members.

An array of content based calculated column widths.

Returns:

  • (Array)

    of Hash



34
35
36
# File 'lib/axlsx/workbook/worksheet/worksheet.rb', line 34

def auto_fit_data
  @auto_fit_data
end

#drawingDrawing (readonly)

Note:

the recommended way to work with drawings and charts is Worksheet#add_chart

The drawing associated with this worksheet.

Returns:

See Also:



29
30
31
# File 'lib/axlsx/workbook/worksheet/worksheet.rb', line 29

def drawing
  @drawing
end

#indexInteger (readonly)

The index of this worksheet in the owning Workbook’s worksheets list.

Returns:

  • (Integer)


50
51
52
# File 'lib/axlsx/workbook/worksheet/worksheet.rb', line 50

def index
  @index
end

#nameString

The name of the worksheet

Returns:

  • (String)


9
10
11
# File 'lib/axlsx/workbook/worksheet/worksheet.rb', line 9

def name
  @name
end

#pnString (readonly)

The part name of this worksheet

Returns:

  • (String)


38
39
40
# File 'lib/axlsx/workbook/worksheet/worksheet.rb', line 38

def pn
  @pn
end

#relationshipsObject (readonly)

The worksheet’s relationships.



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

def relationships
  @relationships
end

#rels_pnString (readonly)

The relationship part name of this worksheet

Returns:

  • (String)


42
43
44
# File 'lib/axlsx/workbook/worksheet/worksheet.rb', line 42

def rels_pn
  @rels_pn
end

#rIdString (readonly)

The relationship Id of thiw worksheet

Returns:

  • (String)


46
47
48
# File 'lib/axlsx/workbook/worksheet/worksheet.rb', line 46

def rId
  @rId
end

#rowsSimpleTypedList (readonly)

Note:

The recommended way to manage rows is Worksheet#add_row

The rows in this worksheet

Returns:

See Also:



23
24
25
# File 'lib/axlsx/workbook/worksheet/worksheet.rb', line 23

def rows
  @rows
end

#workbookWorkbook

The workbook that owns this worksheet

Returns:



13
14
15
# File 'lib/axlsx/workbook/worksheet/worksheet.rb', line 13

def workbook
  @workbook
end

Instance Method Details

#add_chart(chart_type, options = {}) {|chart| ... } ⇒ Object

Adds a chart to this worksheets drawing.

Parameters:

  • chart_type (Class)
  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • start_at (Array)
  • end_at (Array)
  • title (Cell, String)

Yields:

  • (chart)


108
109
110
111
112
# File 'lib/axlsx/workbook/worksheet/worksheet.rb', line 108

def add_chart(chart_type, options={})
  chart = drawing.add_chart(chart_type, options)
  yield chart if block_given?
  chart
end

#add_row(values = [], options = {}) {|@rows.last| ... } ⇒ Row

Adds a row to the worksheet and updates auto fit data

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • values (Array)
  • types (Array, Symbol)
  • style (Array, Integer)

Yields:

Returns:



96
97
98
99
100
101
# File 'lib/axlsx/workbook/worksheet/worksheet.rb', line 96

def add_row(values=[], options={})
  Row.new(self, values, options)
  update_auto_fit_data @rows.last.cells
  yield @rows.last if block_given?
  @rows.last
end

#to_xmlString

Serializes the worksheet document

Returns:

  • (String)


116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/axlsx/workbook/worksheet/worksheet.rb', line 116

def to_xml
  builder = Nokogiri::XML::Builder.new(:encoding => ENCODING) do |xml|
    xml.worksheet(:xmlns => XML_NS, :'xmlns:r' => XML_NS_R) {
      if @auto_fit_data.size > 0
        xml.cols {
          @auto_fit_data.each_with_index do |col, index|
            min_max = index+1
            xml.col(:min=>min_max, :max=>min_max, :width => auto_width(col), :customWidth=>"true")
          end
        }
      end
      xml.sheetData {
        @rows.each do |row|
          row.to_xml(xml)
        end
      }
      xml.drawing :"r:id"=>"rId1" if @drawing          
    }
  end
  builder.to_xml(:indent=>0, :save_with=>0)
end