Class: Workbook::Format

Inherits:
Hash
  • Object
show all
Includes:
Modules::RawObjectsStorage
Defined in:
lib/workbook/format.rb

Overview

Format is an object used for maintinaing a cell’s formatting. It can belong to many cells. It maintains a relation to the raw template’s equivalent, to preserve attributes Workbook cannot modify/access. The keys in the Hash are intended to closely mimick the CSS-style options:

{
  background_color: '#ff000',
  color: '#ffff00',
  font_weight: :bold,
  text_decoration: :underline,
}

Note that as we speak, not all exporters support all properties properly. Consider it WIP.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Modules::RawObjectsStorage

#add_raw, #available_raws, #has_raw_for?, #raws, #remove_all_raws!, #return_raw_for

Constructor Details

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

Initializes Workbook::Format with a hash. The keys in the Hash are intended to closely mimick the CSS-style options (see above)

Parameters:

  • options (Workbook::Format, Hash) (defaults to: {})

    (e.g. :background, :color, :background_color, :font_weight (integer or css-type labels)



29
30
31
32
33
34
35
36
# File 'lib/workbook/format.rb', line 29

def initialize options={}, name=nil
  if options.is_a? String
    name = options
  else
    options.each {|k,v| self[k]=v}
  end
  self.name = name
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#parentObject

Returns the value of attribute parent.



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

def parent
  @parent
end

Instance Method Details

#all_namesArray<String>

returns an array of all format-names this style is inheriting from (and this style)

Returns:

  • (Array<String>)

    an array of Workbook::Formats



92
93
94
# File 'lib/workbook/format.rb', line 92

def all_names
  formats.collect{|a| a.name}
end

#derived_typeObject

Formatting is sometimes the only way to detect the cells’ type.



105
106
107
108
109
110
111
112
113
# File 'lib/workbook/format.rb', line 105

def derived_type
  if self[:numberformat]
    if self[:numberformat].to_s.match("h")
      :time
    elsif self[:numberformat].to_s.match(/y/i)
      :date
    end
  end
end

#flattenedWorkbook::Format

Applies the formatting options of self with its parents until no parent can be found

Returns:

  • (Workbook::Format)

    new Workbook::Format that is the result of merging current style with all its parent’s styles.



98
99
100
101
102
# File 'lib/workbook/format.rb', line 98

def flattened
  ff=Workbook::Format.new()
  formats.each{|a| ff.merge!(a) }
  return ff
end

#formatsArray<Workbook::Format>

returns an array of all formats this style is inheriting from (including itself)

Returns:



79
80
81
82
83
84
85
86
87
88
# File 'lib/workbook/format.rb', line 79

def formats
  formats=[]
  f = self
  formats << f
  while f.parent
    formats << f.parent
    f = f.parent
  end
  formats.reverse
end

#has_background_color?(color = :any) ⇒ Boolean

Does the current format feature a background color? (not black or white or transparant).

Returns:

  • (Boolean)


39
40
41
42
43
44
45
46
47
48
49
# File 'lib/workbook/format.rb', line 39

def has_background_color? color=:any
  bg_color = flattened[:background_color] ? flattened[:background_color].to_s.downcase : nil

  if color != :any and bg_color
    return bg_color == color.to_s.downcase
  elsif bg_color
    return !(flattened[:background_color].downcase=='#ffffff' or flattened[:background_color]=='#000000')
  else
    return false
  end
end

#merge(other_format) ⇒ Workbook::Format

Combines the formatting options of one with another, removes as a consequence the reference to the raw object’s equivalent.

Parameters:

Returns:



64
65
66
67
# File 'lib/workbook/format.rb', line 64

def merge(other_format)
  self.remove_all_raws!
  self.merge_hash(other_format)
end

#merge!(other_format) ⇒ Workbook::Format

Applies the formatting options of self with another, removes as a consequence the reference to the raw object’s equivalent.

Parameters:

Returns:



72
73
74
75
# File 'lib/workbook/format.rb', line 72

def merge!(other_format)
  self.remove_all_raws!
  self.merge_hash!(other_format)
end

#merge_hashObject



21
# File 'lib/workbook/format.rb', line 21

alias_method :merge_hash, :merge

#merge_hash!Object



22
# File 'lib/workbook/format.rb', line 22

alias_method :merge_hash!, :merge!

#to_cssObject

Returns a string that can be used as inline cell styling (e.g. ‘<td style=“<%=cell.format.to_css%>”><%=cell%></td>`)

Returns:

  • String very basic CSS styling string



53
54
55
56
57
58
59
# File 'lib/workbook/format.rb', line 53

def to_css
  css_parts = []
  background = [flattened[:background_color].to_s,flattened[:background].to_s].join(" ").strip
  css_parts.push("background: #{background}") if background and background != ""
  css_parts.push("color: #{flattened[:color].to_s}") if flattened[:color]
  css_parts.join("; ")
end