class Rspreadsheet::CellFormat

Represents a format of a cell. This object is returned by `@cell.format` method and allows syntax like

@cell.format.bold = true
@cell.format.italic = false
@cell.format.color = '#45AC00'

Also handles all logic for formats. @private

Public Class Methods

new(cell) click to toggle source
# File lib/rspreadsheet/cell_format.rb, line 18
def initialize(cell)
  @cell = cell
end

Public Instance Methods

automatic_styles_node() click to toggle source
# File lib/rspreadsheet/cell_format.rb, line 123
def automatic_styles_node; style_node_with_partial_xpath('') end
background_color() click to toggle source
# File lib/rspreadsheet/cell_format.rb, line 29
def background_color; get_cell_style_node_attribute('background-color') end
background_color=(value) click to toggle source
# File lib/rspreadsheet/cell_format.rb, line 39
def background_color=(value); set_cell_style_node_attribute('background-color',  value) end
bold() click to toggle source

@!group text style attribute readers

# File lib/rspreadsheet/cell_format.rb, line 24
def bold;      get_text_style_node_attribute('font-weight') == 'bold' end
Also aliased as: bold?
bold=(value) click to toggle source

@!group text style attribute writers

# File lib/rspreadsheet/cell_format.rb, line 35
def bold=(value);     set_text_style_node_attribute('font-weight', value ? 'bold' : 'normal') end
bold?()
Alias for: bold
border_bottom()
Alias for: bottom
border_left()
Alias for: left
border_right()
Alias for: right
border_top()
Alias for: top
bottom() click to toggle source
# File lib/rspreadsheet/cell_format.rb, line 44
def bottom; @bottom ||= Border.new(self,:bottom) end
Also aliased as: border_bottom
cell_style_node() click to toggle source
# File lib/rspreadsheet/cell_format.rb, line 127
def cell_style_node; style_node_with_partial_xpath("/style:style[@style:name=\"#{style_name}\"]/style:table-cell-properties") end
cellnode() click to toggle source
# File lib/rspreadsheet/cell_format.rb, line 21
def cellnode; @cell.xmlnode end
color() click to toggle source
# File lib/rspreadsheet/cell_format.rb, line 27
def color;     get_text_style_node_attribute('color') end
color=(value) click to toggle source
# File lib/rspreadsheet/cell_format.rb, line 37
def color=(value);    set_text_style_node_attribute('color',  value) end
create_cell_style_node() click to toggle source
# File lib/rspreadsheet/cell_format.rb, line 96
def create_cell_style_node
  create_style_node if style_name.nil? or style_node.nil?
  raise 'cell_style_node already exists' unless cell_style_node.nil?
  style_node << Tools.prepare_ns_node('style','table-cell-properties')
end
create_style_node() click to toggle source
# File lib/rspreadsheet/cell_format.rb, line 101
def create_style_node
  if style_name.nil?
    proposed_style_name = unused_cell_style_name
    Tools.set_ns_attribute(cellnode,'table','style-name',proposed_style_name)
    raise 'Style name was not correctly initialized' if style_name!=proposed_style_name
  end
  anode =  Tools.prepare_ns_node('style','style')
  Tools.set_ns_attribute(anode, 'style', 'name', style_name)
  Tools.set_ns_attribute(anode, 'style', 'family', 'table-cell')
  Tools.set_ns_attribute(anode, 'style', 'parent-style-name', 'Default')
  automatic_styles_node << anode
  raise 'Style node was not correctly initialized' if style_node.nil?
end
create_text_style_node() click to toggle source

@!group initialization of style related nodes, if they do not exist

# File lib/rspreadsheet/cell_format.rb, line 91
def create_text_style_node
  create_style_node if style_name.nil? or style_node.nil?
  raise 'text_style_node already exists' unless text_style_node.nil?
  style_node << Tools.prepare_ns_node('style','text-properties')
end
currency() click to toggle source
# File lib/rspreadsheet/cell_format.rb, line 30
def currency
  Tools.get_ns_attribute_value(cellnode,'office','currency',nil) 
end
font_size() click to toggle source
# File lib/rspreadsheet/cell_format.rb, line 28
def font_size; get_text_style_node_attribute('font-size') end
font_size=(value) click to toggle source
# File lib/rspreadsheet/cell_format.rb, line 38
def font_size=(value);set_text_style_node_attribute('font-size',  value) end
get_cell_style_node_attribute(attribute_name) click to toggle source
# File lib/rspreadsheet/cell_format.rb, line 56
def get_cell_style_node_attribute(attribute_name)
  cell_style_node.nil? ? nil : Tools.get_ns_attribute_value(cell_style_node,'fo',attribute_name)
end
get_text_style_node_attribute(attribute_name) click to toggle source

@!group manipulation of style related nodes

# File lib/rspreadsheet/cell_format.rb, line 53
def get_text_style_node_attribute(attribute_name)
  text_style_node.nil? ? nil : Tools.get_ns_attribute_value(text_style_node,'fo',attribute_name)
end
inspect() click to toggle source

@!group other routines

# File lib/rspreadsheet/cell_format.rb, line 142
def inspect
  "#<Rspreadsheet::CellFormat bold:#{bold?.inspect}, borders: #{top.get_border_string || 'none'}, #{right.get_border_string || 'none'}, #{bottom.get_border_string || 'none'}, #{left.get_border_string || 'none'}>"
end
italic() click to toggle source
# File lib/rspreadsheet/cell_format.rb, line 26
def italic;    get_text_style_node_attribute('font-style') == 'italic' end
italic=(value) click to toggle source
# File lib/rspreadsheet/cell_format.rb, line 36
def italic=(value);   set_text_style_node_attribute('font-style',  value ? 'italic' : 'normal') end
left() click to toggle source
# File lib/rspreadsheet/cell_format.rb, line 45
def left;   @left   ||= Border.new(self,:left) end
Also aliased as: border_left
make_sure_styles_are_not_shared() click to toggle source
# File lib/rspreadsheet/cell_format.rb, line 75
def make_sure_styles_are_not_shared  # with another cell (prevent issue #40)
  # if cell is reapeated, we must detach it first
  @cell.detach if @cell.mode != :regular   
  # if style node is shared, we must create our own first
  if style_shared?
    new_style_name = unused_cell_style_name
    
    new_style_node = style_node.copy(true)
    Tools.set_ns_attribute(new_style_node,'style','name',new_style_name)
    style_node.next = new_style_node
    self.style_name = new_style_name
    raise 'Unsucessfull unsharing of styles' if style_shared?
  end
end
right() click to toggle source
# File lib/rspreadsheet/cell_format.rb, line 46
def right;  @right  ||= Border.new(self,:right) end
Also aliased as: border_right
set_cell_style_node_attribute(attribute_name,value) click to toggle source
# File lib/rspreadsheet/cell_format.rb, line 67
def set_cell_style_node_attribute(attribute_name,value)
  make_sure_styles_are_not_shared
  if cell_style_node.nil?
    self.create_cell_style_node
    raise 'Cell style node was not correctly initialized' if cell_style_node.nil?
  end
  Tools.set_ns_attribute(cell_style_node,'fo',attribute_name,value)
end
set_text_style_node_attribute(attribute_name,value) click to toggle source
# File lib/rspreadsheet/cell_format.rb, line 59
def set_text_style_node_attribute(attribute_name,value)
  make_sure_styles_are_not_shared
  if text_style_node.nil?
    self.create_text_style_node
    raise 'Text style node was not correctly initialized' if text_style_node.nil?
  end
  Tools.set_ns_attribute(text_style_node,'fo',attribute_name,value)
end
style_name() click to toggle source
# File lib/rspreadsheet/cell_format.rb, line 124
def style_name; Tools.get_ns_attribute_value(cellnode,'table','style-name',nil) end
style_name=(value) click to toggle source

experimental. Allows to assign a style to cell. Currenty used only for automatic styles.

# File lib/rspreadsheet/cell_format.rb, line 147
def style_name=(value)
  Tools.set_ns_attribute(cellnode,'table','style-name',value)
end
style_node() click to toggle source
# File lib/rspreadsheet/cell_format.rb, line 125
def style_node; style_node_with_partial_xpath("/style:style[@style:name=\"#{style_name}\"]") end
style_node_with_partial_xpath(xpath) click to toggle source
# File lib/rspreadsheet/cell_format.rb, line 128
def style_node_with_partial_xpath(xpath)
  return nil if cellnode.nil?
  cellnode.doc.root.find("./office:automatic-styles#{xpath}").first 
end
style_shared?() click to toggle source
# File lib/rspreadsheet/cell_format.rb, line 137
def style_shared?
  style_shared_count>1
end
style_shared_count() click to toggle source

how many cells are using this style. repeated cells are counted only once

# File lib/rspreadsheet/cell_format.rb, line 134
def style_shared_count
  cellnode.doc.root.find("//table:table-cell[@table:style-name=\"#{style_name}\"]").size
end
text_style_node() click to toggle source
# File lib/rspreadsheet/cell_format.rb, line 126
def text_style_node; style_node_with_partial_xpath("/style:style[@style:name=\"#{style_name}\"]/style:text-properties") end
top() click to toggle source

@!group border related

# File lib/rspreadsheet/cell_format.rb, line 43
def top;    @top    ||= Border.new(self,:top)  end
Also aliased as: border_top
unused_cell_style_name() click to toggle source

@!group other style node related routines

# File lib/rspreadsheet/cell_format.rb, line 116
def unused_cell_style_name
  last = (cellnode.nil? ? [] : cellnode.doc.root.find('./office:automatic-styles/style:style')).
    collect {|node| node['name']}.
    collect{ |name| /^ce(\d*)$/.match(name); $1.andand.to_i}.
    compact.max || 0
  "ce#{last+1}"
end