Class: Axlsx::Worksheet
- Inherits:
-
Object
- Object
- Axlsx::Worksheet
- Defined in:
- lib/axlsx/workbook/worksheet/worksheet.rb
Overview
The Worksheet class represents a worksheet in the workbook.
Instance Attribute Summary collapse
-
#auto_fit_data ⇒ Array
readonly
An array of content based calculated column widths.
-
#drawing ⇒ Drawing
readonly
The drawing associated with this worksheet.
-
#index ⇒ Integer
readonly
The index of this worksheet in the owning Workbook’s worksheets list.
-
#name ⇒ String
The name of the worksheet.
-
#pn ⇒ String
readonly
The part name of this worksheet.
-
#relationships ⇒ Object
readonly
The worksheet’s relationships.
-
#rels_pn ⇒ String
readonly
The relationship part name of this worksheet.
-
#rId ⇒ String
readonly
The relationship Id of thiw worksheet.
-
#rows ⇒ SimpleTypedList
readonly
The rows in this worksheet.
-
#workbook ⇒ Workbook
readonly
The workbook that owns this worksheet.
Instance Method Summary collapse
-
#add_chart(chart_type, options = {}) {|chart| ... } ⇒ Object
Adds a chart to this worksheets drawing.
-
#add_image(options = {}) {|image| ... } ⇒ Object
Adds a media item to the worksheets drawing.
-
#add_row(values = [], options = {}) {|@rows.last| ... } ⇒ Row
Adds a row to the worksheet and updates auto fit data.
-
#initialize(wb, options = {}) ⇒ Worksheet
constructor
Creates a new worksheet.
-
#to_xml ⇒ String
Serializes the worksheet document.
Constructor Details
#initialize(wb, options = {}) ⇒ Worksheet
the recommended way to manage worksheets is Workbook#add_worksheet
Creates a new worksheet.
59 60 61 62 63 64 65 66 |
# File 'lib/axlsx/workbook/worksheet/worksheet.rb', line 59 def initialize(wb, ={}) @rows = SimpleTypedList.new Row self.workbook = wb @workbook.worksheets << self @auto_fit_data = [] self.name = [:name] || "Sheet" + (index+1).to_s @magick_draw = Magick::Draw.new end |
Instance Attribute Details
#auto_fit_data ⇒ Array (readonly)
a single auto fit data item is a hash with :longest => [String] and :sz=> [Integer] members.
An array of content based calculated column widths.
34 35 36 |
# File 'lib/axlsx/workbook/worksheet/worksheet.rb', line 34 def auto_fit_data @auto_fit_data end |
#drawing ⇒ Drawing (readonly)
the recommended way to work with drawings and charts is Worksheet#add_chart
The drawing associated with this worksheet.
29 30 31 |
# File 'lib/axlsx/workbook/worksheet/worksheet.rb', line 29 def drawing @drawing end |
#index ⇒ Integer (readonly)
The index of this worksheet in the owning Workbook’s worksheets list.
50 51 52 |
# File 'lib/axlsx/workbook/worksheet/worksheet.rb', line 50 def index @index end |
#name ⇒ String
The name of the worksheet
9 10 11 |
# File 'lib/axlsx/workbook/worksheet/worksheet.rb', line 9 def name @name end |
#pn ⇒ String (readonly)
The part name of this worksheet
38 39 40 |
# File 'lib/axlsx/workbook/worksheet/worksheet.rb', line 38 def pn @pn end |
#relationships ⇒ Object (readonly)
The worksheet’s relationships.
17 18 19 |
# File 'lib/axlsx/workbook/worksheet/worksheet.rb', line 17 def relationships @relationships end |
#rels_pn ⇒ String (readonly)
The relationship part name of this worksheet
42 43 44 |
# File 'lib/axlsx/workbook/worksheet/worksheet.rb', line 42 def rels_pn @rels_pn end |
#rId ⇒ String (readonly)
The relationship Id of thiw worksheet
46 47 48 |
# File 'lib/axlsx/workbook/worksheet/worksheet.rb', line 46 def rId @rId end |
#rows ⇒ SimpleTypedList (readonly)
The recommended way to manage rows is Worksheet#add_row
The rows in this worksheet
23 24 25 |
# File 'lib/axlsx/workbook/worksheet/worksheet.rb', line 23 def rows @rows end |
#workbook ⇒ Workbook
The workbook that owns this worksheet
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
each chart type also specifies additional options
Adds a chart to this worksheets drawing. This is the recommended way to create charts for your worksheet. This method wraps the complexity of dealing with ooxml drawing, anchors, markers graphic frames chart objects and all the other dirty details.
116 117 118 119 120 |
# File 'lib/axlsx/workbook/worksheet/worksheet.rb', line 116 def add_chart(chart_type, ={}) chart = drawing.add_chart(chart_type, ) yield chart if block_given? chart end |
#add_image(options = {}) {|image| ... } ⇒ Object
Adds a media item to the worksheets drawing
125 126 127 128 129 |
# File 'lib/axlsx/workbook/worksheet/worksheet.rb', line 125 def add_image(={}) image = drawing.add_image() yield image if block_given? image end |
#add_row(values = [], options = {}) {|@rows.last| ... } ⇒ Row
Adds a row to the worksheet and updates auto fit data
96 97 98 99 100 101 |
# File 'lib/axlsx/workbook/worksheet/worksheet.rb', line 96 def add_row(values=[], ={}) Row.new(self, values, ) update_auto_fit_data @rows.last.cells yield @rows.last if block_given? @rows.last end |
#to_xml ⇒ String
Serializes the worksheet document
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/axlsx/workbook/worksheet/worksheet.rb', line 133 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 |