Class: Format

Inherits:
Object
  • Object
show all
Defined in:
lib/spreadsheet/format.rb

Constant Summary collapse

COLORS =
{
   'aqua'    => 0x0F,
   'black'   => 0x08,
   'blue'    => 0x0C,
   'brown'   => 0x10,
   'cyan'    => 0x0F,
   'fuchsia' => 0x0E,
   'gray'    => 0x17,
   'grey'    => 0x17,
   'green'   => 0x11,
   'lime'    => 0x0B,
   'magenta' => 0x0E,
   'navy'    => 0x12,
   'orange'  => 0x1D,
   'purple'  => 0x24,
   'red'     => 0x0A,
   'silver'  => 0x16,
   'white'   => 0x09,
   'yellow'  => 0x0D
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}, xf_index = 0) {|_self| ... } ⇒ Format

Returns a new instance of Format.

Yields:

  • (_self)

Yield Parameters:

  • _self (Format)

    the object that the method was called on



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/spreadsheet/format.rb', line 26

def initialize(args={}, xf_index=0)
   defaults = {}

   defaults.update(:color     => 0x7FFF, :bold   => 0x0190)
   defaults.update(:fg_color  => 0x40, :pattern  => 0,  :size => 10)
   defaults.update(:bg_color  => 0x41, :rotation => 0,  :font => "Arial")
   defaults.update(:underline => 0,    :italic   => 0,  :top  => 0)
   defaults.update(:bottom    => 0,    :right    => 0,  :left => 0)

   defaults.update(:font_index     => 0, :font_family  => 0)
   defaults.update(:font_strikeout => 0, :font_script  => 0)
   defaults.update(:font_outline   => 0, :left_color   => 0)
   defaults.update(:font_charset   => 0, :right_color  => 0)
   defaults.update(:font_shadow    => 0, :top_color    => 0x40)
   defaults.update(:text_v_align   => 2, :bottom_color => 0x40)
   defaults.update(:text_h_align   => 0, :num_format   => 0)
   defaults.update(:text_justlast  => 0, :text_wrap    => 0)

   ## convenience methods
   defaults.update(:border => 0, :align => 'left')

   ########################################################################
   # We must manually create accessors for these so that they can handle
   # both 0/1 and true/false.
   ########################################################################
   no_acc = [:bold,:italic,:underline,:strikeout,:text_wrap,:text_justlast]
   no_acc.push(:fg_color,:bg_color,:color,:font_outline,:font_shadow)
   no_acc.push(:align,:border)

   args.each{|key,val|
      key = key.to_s.downcase.intern
      val = 1 if val == true
      val = 0 if val == false
      defaults.fetch(key)
      defaults.update(key=>val)
   }

   defaults.each{|key,val|
      unless no_acc.member?(key)
         self.class.send(:attr_accessor,"#{key}")
      end
      send("#{key}=",val)
   }

   @xf_index = xf_index

   yield self if block_given?
end

Instance Attribute Details

#xf_indexObject

Returns the value of attribute xf_index.



24
25
26
# File 'lib/spreadsheet/format.rb', line 24

def xf_index
  @xf_index
end

Instance Method Details

#alignObject



335
336
337
# File 'lib/spreadsheet/format.rb', line 335

def align
   [@text_h_align,@text_v_align]
end

#align=(location = nil) ⇒ Object



306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# File 'lib/spreadsheet/format.rb', line 306

def align=(location = nil)
   return if location.nil?
   return if location.kind_of?(Fixnum)
   location.downcase!

   case location
   when 'left'
     @text_h_align = 1
   when 'center', 'centre'
     @text_h_align = 2
   when 'right'
     @text_h_align = 3
   when 'fill'
     @text_h_align = 4 
   when 'justify'
     @text_h_align = 5 
   when 'merge'
     @text_h_align = 6
   when 'top'
     @text_v_align = 0
   when 'vcentre', 'vcenter'
     @text_v_align = 1
   when 'bottom'
     @text_v_align = 2
   when 'vjustify'
     @text_v_align = 3
   end
end

#bg_colorObject



118
119
120
# File 'lib/spreadsheet/format.rb', line 118

def bg_color
   @bg_color
end

#bg_color=(colour) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/spreadsheet/format.rb', line 88

def bg_color=(colour)
   if COLORS.has_key?(colour)
      @bg_color = COLORS[colour]
   else
      if colour.kind_of?(String)
         raise ArgumentError, "unknown color"
      else
         @bg_color = colour
      end
   end
   @bg_color
end

#boldObject



351
352
353
354
# File 'lib/spreadsheet/format.rb', line 351

def bold
   return true if @bold >= 1
   return false
end

#bold=(weight) ⇒ Object



339
340
341
342
343
344
345
346
347
348
349
# File 'lib/spreadsheet/format.rb', line 339

def bold=(weight)
   weight = 1 if weight == true
   weight = 0 if weight == false
   weight = 0x2BC if weight.nil?
   weight = 0x2BC if weight == 1
   weight = 0x190 if weight == 0
   weight = 0x190 if weight < 0x064
   weight = 0x190 if weight > 0x3E8
   @bold = weight
   @bold
end

#borderObject



360
361
362
# File 'lib/spreadsheet/format.rb', line 360

def border
   [@bottom,@top,@right,@left]
end

#border=(style) ⇒ Object



356
357
358
# File 'lib/spreadsheet/format.rb', line 356

def border=(style)
   [@bottom,@top,@right,@left].each{ |attr| attr = style }
end

#border_colorObject



368
369
370
# File 'lib/spreadsheet/format.rb', line 368

def border_color
   [@bottom_color,@top_color,@left_color,@right_color]
end

#border_color=(color) ⇒ Object



364
365
366
# File 'lib/spreadsheet/format.rb', line 364

def border_color=(color)
   [@bottom_color,@top_color,@left_color,@right_color].each{ |a| a = color }
end

#colorObject

Should I return the stringified version of the color if applicable?



123
124
125
126
# File 'lib/spreadsheet/format.rb', line 123

def color
   #COLORS.invert.fetch(@color)
   @color
end

#color=(colour) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/spreadsheet/format.rb', line 75

def color=(colour)
   if COLORS.has_key?(colour)
      @color = COLORS[colour]
   else
      if colour.kind_of?(String)
         raise ArgumentError, "unknown color"
      else
         @color = colour
      end
   end
   @color
end

#fg_colorObject



114
115
116
# File 'lib/spreadsheet/format.rb', line 114

def fg_color
   @fg_color
end

#fg_color=(colour) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/spreadsheet/format.rb', line 101

def fg_color=(colour)
   if COLORS.has_key?(colour)
      @fg_color = COLORS[colour]
   else
      if colour.kind_of?(String)
         raise ArgumentError, "unknown color"
      else
         @fg_color = colour
      end
   end
   @fg_color
end

#font_biffObject



275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/spreadsheet/format.rb', line 275

def font_biff
   dyheight = @size * 20
   cch      = @font.length
   record   = 0x31
   length   = 0x0F + cch
   reserved = 0x00

   grbit = 0x00
   grbit |= 0x02 if @italic > 0
   grbit |= 0x08 if @font_strikeout > 0
   grbit |= 0x10 if @font_outline > 0
   grbit |= 0x20 if @font_shadow > 0

   header = [record,length].pack("vv")
   fields = [dyheight,grbit,@color,@bold,@font_script,@underline,@font_family]
   fields.push(@font_charset,reserved,cch)

   data = fields.pack("vvvvvCCCCC")
   rv = header + data + @font

   return rv
end

#font_keyObject



298
299
300
301
302
303
304
# File 'lib/spreadsheet/format.rb', line 298

def font_key
   key = @font.to_s + @size.to_s + @font_script.to_s + @underline.to_s
   key += @font_strikeout.to_s + @bold.to_s + @font_outline.to_s
   key += @font_family.to_s + @font_charset.to_s + @font_shadow.to_s
   key += @color.to_s + @italic.to_s
   return key
end

#font_outlineObject



150
151
152
153
# File 'lib/spreadsheet/format.rb', line 150

def font_outline
   return true if @font_outline == 1
   return false
end

#font_outline=(val) ⇒ Object



155
156
157
158
159
# File 'lib/spreadsheet/format.rb', line 155

def font_outline=(val)
   val = 1 if val == true
   val = 0 if val == false
   @font_outline = val
end

#font_shadowObject



139
140
141
142
# File 'lib/spreadsheet/format.rb', line 139

def font_shadow
   return true if @font_shadow == 1
   return false
end

#font_shadow=(val) ⇒ Object



144
145
146
147
148
# File 'lib/spreadsheet/format.rb', line 144

def font_shadow=(val)
   val = 1 if val == true
   val = 0 if val == false
   @font_shadow = val
end

#italicObject



128
129
130
131
# File 'lib/spreadsheet/format.rb', line 128

def italic
   return true if @italic >= 1
   return false
end

#italic=(val) ⇒ Object



133
134
135
136
137
# File 'lib/spreadsheet/format.rb', line 133

def italic=(val)
   val = 1 if val == true
   val = 0 if val == false
   @italic = val
end

#strikeoutObject



183
184
185
186
# File 'lib/spreadsheet/format.rb', line 183

def strikeout
   return true if @strikeout == 1
   return false
end

#strikeout=(val) ⇒ Object



188
189
190
191
192
# File 'lib/spreadsheet/format.rb', line 188

def strikeout=(val)
   val = 1 if val == true
   val = 0 if val == false
   @strikeout = val
end

#text_justlastObject



161
162
163
164
# File 'lib/spreadsheet/format.rb', line 161

def text_justlast
   return true if @text_justlast == 1
   return false
end

#text_justlast=(val) ⇒ Object



166
167
168
169
170
# File 'lib/spreadsheet/format.rb', line 166

def text_justlast=(val)
   val = 1 if val == true
   val = 0 if val == false
   @text_justlast = val
end

#text_wrapObject



172
173
174
175
# File 'lib/spreadsheet/format.rb', line 172

def text_wrap
   return true if @text_wrap == 1
   return false
end

#text_wrap=(val) ⇒ Object



177
178
179
180
181
# File 'lib/spreadsheet/format.rb', line 177

def text_wrap=(val)
   val = 1 if val == true
   val = 0 if val == false
   @text_wrap = val
end

#underlineObject



194
195
196
197
# File 'lib/spreadsheet/format.rb', line 194

def underline
   return true if @underline == 1
   return false
end

#underline=(val) ⇒ Object



199
200
201
202
203
# File 'lib/spreadsheet/format.rb', line 199

def underline=(val)
   val = 1 if val == true
   val = 0 if val == false
   @underline = val
end

#xf_biff(style = 0) ⇒ Object



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/spreadsheet/format.rb', line 206

def xf_biff(style=0)
   atr_num = 0
   atr_num = 1 if @num_format != 0

   atr_fnt = 0
   atr_fnt = 1 if @font_index != 0

   atr_alc = @text_wrap
   atr_bdr = [@bottom,@top,@left,@right].find{ |n| n > 0 } || 0
   atr_pat = [@fg_color,@bg_color,@pattern].find{ |n| n > 0 } || 0

   atr_prot    = 0

   @bottom_color = 0 if @bottom == 0
   @top_color    = 0 if @top    == 0
   @right_color  = 0 if @right  == 0
   @left_color   = 0 if @left   == 0

   record         = 0x00E0
   length         = 0x0010

   align  = @text_h_align
   align  |= @text_wrap     << 3
   align  |= @text_v_align  << 4
   align  |= @text_justlast << 7
   align  |= @rotation      << 8
   align  |= atr_num        << 10
   align  |= atr_fnt        << 11
   align  |= atr_alc        << 12
   align  |= atr_bdr        << 13
   align  |= atr_pat        << 14
   align  |= atr_prot       << 15

   # Assume a solid fill color if the bg_color or fg_color are set but
   # the pattern value is not set
   if @pattern <= 0x01 and @bg_color != 0x41 and @fg_color == 0x40
      @fg_color = @bg_color
      @bg_color = 0x40
      @pattern  = 1
   end

   if @pattern < 0x01 and @bg_color == 0x41 and @fg_color != 0x40
      @bg_color = 0x40
      @pattern  = 1
   end

   icv   = @fg_color
   icv  |= @bg_color << 7

   fill = @pattern
   fill |= @bottom << 6
   fill |= @bottom_color << 9

   border1 = @top
   border1 |= @left      << 3
   border1 |= @right     << 6
   border1 |= @top_color << 9

   border2 = @left_color
   border2 |= @right_color << 7

   header = [record,length].pack("vv")
   fields = [@font_index,@num_format,style,align,icv,fill,border1,border2]
   data = fields.pack("vvvvvvvv")

   rv = header + data
   return rv
end