Class: Excel::Style

Inherits:
Object
  • Object
show all
Defined in:
lib/rexcel/style.rb

Overview

Style definition for Excel. The styles can be assigned to the Cell and Row.

You can define:

  • Font characteristic: bold/italic

  • Color and background color (Based on ColorIndex)

You can (yet) not define:

  • Font type

Constant Summary collapse

ColorIndex =

Some colors.

Colors are adminstrated by a ColorIndex. This Hash is a reconversion.

See also www.mvps.org/dmcritchie/excel/colors.htm

{
    1=> '#000000',
    2=> '#FFFFFF',
    3=> '#FF0000',
    4=> '#00FF00',
    5=> '#0000FF',
    6=> '#FFFF00',
    7=> '#FF00FF',
    8=> '#00FFFF',
    9=> '#800000',
    10=> '#008000',
    11=> '#000080',
    12=> '#808000',
    13=> '#800080',
    14=> '#008080',
    15=> '#C0C0C0', #gray
    16=> '#808080',
    17=> '#9999FF',
    18=> '#993366',
    19=> '#FFFFCC',
    20=> '#CCFFFF',
    21=> '#660066',
    22=> '#FF8080',
    23=> '#0066CC',
    24=> '#CCCCFF',
    25=> '#000080',
    26=> '#FF00FF',
    27=> '#FFFF00',
    28=> '#00FFFF',
    29=> '#800080',
    30=> '#800000',
    31=> '#008080',
    32=> '#0000FF',
    33=> '#00CCFF',
    34=> '#CCFFFF',
    35=> '#CCFFCC',
    36=> '#FFFF99',
    37=> '#99CCFF',
    38=> '#FF99CC',
    39=> '#CC99FF',
    40=> '#FFCC99',
    41=> '#3366FF',
    42=> '#33CCCC',
    43=> '#99CC00',
    44=> '#FFCC00',
    45=> '#FF9900',
    46=> '#FF6600',
    47=> '#666699',
    48=> '#969696',
    49=> '#003366',
    50=> '#339966',
    51=> '#003300',
    52=> '#333300',
    53=> '#993300',
    54=> '#993366',
    55=> '#333399',
    56=> '#333333',
}
@@xls_warning_made =

to_xls

false

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ Style

Create a new style.

Options:

  • bold

  • italic

  • color (not supported yet)

  • backgroundcolor (not supported yet)

Not implemented (yet)

  • Fontsize

  • Font

Raises:

  • (ArgumentError)


93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/rexcel/style.rb', line 93

def initialize(name, options = {})
  
  @name = name
  #Catch, if name was forgotten and options are first parameter.
  raise ArgumentError, "Style requries key" if name.is_a?(Hash)
  
  @log = options[:log] || LOGGER    
  @log.debug( "Create Style #{name}")
  
  options.each{|key,value|
    case key
      when :log
      when :bold
        self.bold = value
        @log.debug( "Style #{name}: Set #{key}")
      when :italic
        self.italic = value
        @log.debug( "Style #{name}: Set #{key}")          
      when :color, :colour
        if ColorIndex[value]
          self.color = value
          @log.debug( "Style #{name}: Set color #{key}")
        else
          @log.error( "Style #{name}: Usage of undefined color #{value}")
        end
      when :backgroundcolor, :backgroundcolour
        if ColorIndex[value]
          self.backgroundcolor = value
          @log.debug( "Style #{name}: Set backgroundcolor #{key}")
        else
          @log.error( "Style #{name}: Usage of undefined color #{value}")
        end
      else
        @log.warn("Excel::Style: undefined option #{option}")
    end
  }
    
  @style_id = nil #Set by Workbook
end

Instance Attribute Details

#backgroundcolorObject

Background Color. Must be defined in ColorIndex



156
157
158
# File 'lib/rexcel/style.rb', line 156

def backgroundcolor
  @backgroundcolor
end

#boldObject

Bold



150
151
152
# File 'lib/rexcel/style.rb', line 150

def bold
  @bold
end

#colorObject

Color. Must be defined in ColorIndex



154
155
156
# File 'lib/rexcel/style.rb', line 154

def color
  @color
end

#italicObject

Italic



152
153
154
# File 'lib/rexcel/style.rb', line 152

def italic
  @italic
end

#nameObject (readonly)

Name of the Sytle. for usage inside ruby



133
134
135
# File 'lib/rexcel/style.rb', line 133

def name
  @name
end

#style_idObject

Style ID. For usage in XML/XLS.



147
148
149
# File 'lib/rexcel/style.rb', line 147

def style_id
  @style_id
end

Instance Method Details

#to_xls(wb) ⇒ Object

Define Excel style.

Receives a OLE-work book

How to do it??

Unless this function is working, use #to_xls_direct



197
198
199
200
201
# File 'lib/rexcel/style.rb', line 197

def to_xls(wb)
  #~ @log.fatal("#{self.class}##{__method__} not supported.") unless @@xls_warning_made
  @@xls_warning_made = true  #only once
  @log.info("Cells include format options directly")
end

#to_xls_direct(cell) ⇒ Object

Attach style information directly to the OLE-Object (row/cell)

Receives a OLE-object

This method is only a interim solution until I found the way to use styles with ole. (#to_xls).



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/rexcel/style.rb', line 211

def to_xls_direct(cell)

    #if the cell has bold=false, the row-setting is overwritten
    if bold 
      cell.Font.Bold = true
    end
    if italic
      cell.Font.Italic = true
    end
    if color
      cell.Font.ColorIndex  = color
    end 
    if backgroundcolor
      cell.Interior.ColorIndex = backgroundcolor
    end
end

#to_xml(xmlbuilder, ns) ⇒ Object

Build the xml a work sheet style definition,

ns must be a method-object to implement the namespace definitions.



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/rexcel/style.rb', line 162

def to_xml(xmlbuilder, ns)
  xmlbuilder[ns.call].Style( ns.call(:ID) => @style_id ){
    fontoption = {}
    fontoption[ns.call(:Bold)]  = "1"  if bold
    fontoption[ns.call(:Italic)] = "1"  if italic
    if color  #Set font color
      if ColorIndex[color]
        fontoption[ns.call(:Color)] = ColorIndex[color] 
      else
        @log.error("Color #{color.inspect} not defined in ColorIndex")
      end
    end      
    xmlbuilder[ns.call].Font(fontoption) unless fontoption.empty?

    if backgroundcolor
      if ColorIndex[backgroundcolor]
        xmlbuilder[ns.call].Interior(
          ns.call(:Color) => ColorIndex[backgroundcolor],
          ns.call(:Pattern) => "Solid"
        )
      else
        @log.error("ColorIndex #{backgroundcolor.inspect} not defined")
      end
    end
  }
end