Class: Scio::Excel::SimpleStyle

Inherits:
Object
  • Object
show all
Defined in:
lib/dm_core/scio_excel.rb

Overview

SimpleStyle tries to simplify the styles for an individual cell.

Current options are:

  • text alignment (vertical, horizontal)

  • text wrapping

  • font style (bold, italics, color)

  • background color for the cell

  • borders

Example

# simple center-align
st1 = Scio::Excel::SimpleStyle.new(:text => {:halign => "Center"})
# wrap text to the cell
st2 = Scio::Excel::SimpleStyle.new(:text => {:wrap => true})
# set background color
st2.bgcolor = "#666699"
# set the borders
st2.borders = Scio::Excel::BORDER_ALL
# set only the left & right border
st1.borders = Scio::Excel::BORDER_LEFT | Scio::Excel::BORDER_RIGHT
# italics
st1.font[:italic] = true

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ SimpleStyle

Returns a new instance of SimpleStyle.



437
438
439
440
441
442
443
# File 'lib/dm_core/scio_excel.rb', line 437

def initialize(opts = {})
  @text         = opts[:text] || {}
  @font         = opts[:font] || {}
  @borders      = opts[:borders].nil? ? 0 : opts[:borders]
  @bgcolor      = opts[:bgcolor]
  @numberformat = opts[:numberformat]
end

Instance Attribute Details

#bgcolorObject

Returns the value of attribute bgcolor.



430
431
432
# File 'lib/dm_core/scio_excel.rb', line 430

def bgcolor
  @bgcolor
end

#bordersObject

Returns the value of attribute borders.



430
431
432
# File 'lib/dm_core/scio_excel.rb', line 430

def borders
  @borders
end

#excel_idObject

Returns the value of attribute excel_id.



430
431
432
# File 'lib/dm_core/scio_excel.rb', line 430

def excel_id
  @excel_id
end

#fontObject

Returns the value of attribute font.



430
431
432
# File 'lib/dm_core/scio_excel.rb', line 430

def font
  @font
end

#numberformatObject

Returns the value of attribute numberformat.



430
431
432
# File 'lib/dm_core/scio_excel.rb', line 430

def numberformat
  @numberformat
end

#textObject

Returns the value of attribute text.



430
431
432
# File 'lib/dm_core/scio_excel.rb', line 430

def text
  @text
end

Instance Method Details

#create(workbook) ⇒ Object

Creates the xml for the style. You should not call this directly.



446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
# File 'lib/dm_core/scio_excel.rb', line 446

def create(workbook)
  buffer = ""
  xml = Builder::XmlMarkup.new(:target => buffer, :indent => 2)
  @excel_id = workbook.next_style_id
  xml.Style 'ss:ID' => @excel_id do
    unless @text.empty?
      alignment_opts = {}
      alignment_opts["ss:Vertical"] = @text[:valign] if !@text[:valign].nil?
      alignment_opts["ss:Horizontal"] = @text[:halign] if !@text[:halign].nil?
      alignment_opts["ss:WrapText"] = "1" if @text[:wrap]
      xml.Alignment alignment_opts unless alignment_opts.empty?
    end
    unless @font.empty?
      font_opts = {}
      font_opts["ss:Bold"] = "1" if @font[:bold]
      font_opts["ss:Italic"] = "1" if @font[:italic]
      font_opts["ss:Color"] = @font[:color] unless @font[:color].nil?
      xml.Font font_opts unless font_opts.empty?
    end
    if @borders > 0
      xml.Borders do
        xml.Border "ss:Position" => "Bottom",
                   "ss:LineStyle" => "Continuous",
                   "ss:Weight" => "1" if @borders & BORDER_BOTTOM > 0
        xml.Border "ss:Position" => "Left",
                   "ss:LineStyle" => "Continuous",
                   "ss:Weight" => "1" if @borders & BORDER_LEFT > 0
        xml.Border "ss:Position" => "Right",
                   "ss:LineStyle" => "Continuous",
                   "ss:Weight" => "1" if @borders & BORDER_RIGHT > 0
        xml.Border "ss:Position" => "Top",
                   "ss:LineStyle" => "Continuous",
                   "ss:Weight" => "1" if @borders & BORDER_TOP > 0
      end
    end
    xml.Interior "ss:Color" => @bgcolor, "ss:Pattern" => "Solid" unless @bgcolor.nil?
    xml.NumberFormat "ss:Format" => @numberformat unless @numberformat.nil?
  end
  xml.target!
end