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
# 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)

   ########################################################################
   # 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)

   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



322
323
324
# File 'lib/spreadsheet/format.rb', line 322

def align
   [@text_h_align,@text_v_align]
end

#align=(location = nil) ⇒ Object



302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# File 'lib/spreadsheet/format.rb', line 302

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

   @text_h_align = 1 if location == 'left'
   @text_h_align = 2 if location == 'centre'
   @text_h_align = 2 if location == 'center'
   @text_h_align = 3 if location == 'right'
   @text_h_align = 4 if location == 'fill'
   @text_h_align = 5 if location == 'justify'
   @text_h_align = 6 if location == 'merge'

   @text_v_align = 0 if location == 'top'
   @text_v_align = 1 if location == 'vcentre'
   @text_v_align = 1 if location == 'vcenter'
   @text_v_align = 2 if location == 'bottom'
   @text_v_align = 3 if location == 'vjustify'
end

#bg_colorObject



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

def bg_color
   @bg_color
end

#bg_color=(colour) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/spreadsheet/format.rb', line 84

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



338
339
340
341
# File 'lib/spreadsheet/format.rb', line 338

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

#bold=(weight) ⇒ Object



326
327
328
329
330
331
332
333
334
335
336
# File 'lib/spreadsheet/format.rb', line 326

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



347
348
349
# File 'lib/spreadsheet/format.rb', line 347

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

#border=(style) ⇒ Object



343
344
345
# File 'lib/spreadsheet/format.rb', line 343

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

#border_colorObject



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

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

#border_color=(color) ⇒ Object



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

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?



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

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

#color=(colour) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/spreadsheet/format.rb', line 71

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



110
111
112
# File 'lib/spreadsheet/format.rb', line 110

def fg_color
   @fg_color
end

#fg_color=(colour) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/spreadsheet/format.rb', line 97

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



271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/spreadsheet/format.rb', line 271

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



294
295
296
297
298
299
300
# File 'lib/spreadsheet/format.rb', line 294

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



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

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

#font_outline=(val) ⇒ Object



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

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

#font_shadowObject



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

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

#font_shadow=(val) ⇒ Object



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

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

#italicObject



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

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

#italic=(val) ⇒ Object



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

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

#strikeoutObject



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

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

#strikeout=(val) ⇒ Object



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

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

#text_justlastObject



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

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

#text_justlast=(val) ⇒ Object



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

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

#text_wrapObject



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

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

#text_wrap=(val) ⇒ Object



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

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

#underlineObject



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

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

#underline=(val) ⇒ Object



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

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

#xf_biff(style = 0) ⇒ Object



202
203
204
205
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
# File 'lib/spreadsheet/format.rb', line 202

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