Method: Writeexcel::Format#get_xf

Defined in:
lib/writeexcel/format.rb

#get_xfObject

Generate an Excel BIFF XF record.



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
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
# File 'lib/writeexcel/format.rb', line 184

def get_xf  # :nodoc:

  # Local Variable
  #    record;     # Record identifier
  #    length;     # Number of bytes to follow
  #
  #    ifnt;       # Index to FONT record
  #    ifmt;       # Index to FORMAT record
  #    style;      # Style and other options
  #    align;      # Alignment
  #    indent;     #
  #    icv;        # fg and bg pattern colors
  #    border1;    # Border line options
  #    border2;    # Border line options
  #    border3;    # Border line options

  # Set the type of the XF record and some of the attributes.
  if @type == 0xFFF5 then
    style = 0xFFF5
  else
    style  = @locked
    style |= @hidden << 1
  end

  # Flags to indicate if attributes have been set.
  atr_num  = (@num_format   != 0) ? 1 : 0
  atr_fnt  = (@font_index   != 0) ? 1 : 0
  atr_alc  = (@text_h_align != 0 ||
              @text_v_align != 2 ||
              @shrink       != 0 ||
              @merge_range  != 0 ||
              @text_wrap    != 0 ||
              @indent       != 0) ? 1 : 0
  atr_bdr  = (@bottom       != 0 ||
              @top          != 0 ||
              @left         != 0 ||
              @right        != 0 ||
              @diag_type    != 0) ? 1 : 0
  atr_pat  = (@fg_color     != 0x40 ||
              @bg_color     != 0x41 ||
              @pattern      != 0x00) ? 1 : 0
  atr_prot = (@hidden       != 0 ||
              @locked       != 1) ? 1 : 0

  # Set attribute changed flags for the style formats.
  if @xf_index != 0 and @type == 0xFFF5
    if @xf_index >= 16
      atr_num    = 0
      atr_fnt    = 1
    else
      atr_num    = 1
      atr_fnt    = 0
    end
    atr_alc    = 1
    atr_bdr    = 1
    atr_pat    = 1
    atr_prot   = 1
  end

  # Set a default diagonal border style if none was specified.
  @diag_border = 1 if (@diag_border ==0 and @diag_type != 0)

  # Reset the default colours for the non-font properties
  @fg_color     = 0x40 if @fg_color     == 0x7FFF
  @bg_color     = 0x41 if @bg_color     == 0x7FFF
  @bottom_color = 0x40 if @bottom_color == 0x7FFF
  @top_color    = 0x40 if @top_color    == 0x7FFF
  @left_color   = 0x40 if @left_color   == 0x7FFF
  @right_color  = 0x40 if @right_color  == 0x7FFF
  @diag_color   = 0x40 if @diag_color   == 0x7FFF

  # Zero the default border colour if the border has not been set.
  @bottom_color = 0 if @bottom    == 0
  @top_color    = 0 if @top       == 0
  @right_color  = 0 if @right     == 0
  @left_color   = 0 if @left      == 0
  @diag_color   = 0 if @diag_type == 0

  # The following 2 logical statements take care of special cases in relation
  # to cell colours and patterns:
  # 1. For a solid fill (_pattern == 1) Excel reverses the role of foreground
  #    and background colours.
  # 2. If the user specifies a foreground or background colour without a
  #    pattern they probably wanted a solid fill, so we fill in the defaults.
  #
  if (@pattern  <= 0x01 && @bg_color != 0x41 && @fg_color == 0x40)
    @fg_color = @bg_color
    @bg_color = 0x40
    @pattern  = 1
  end

  if (@pattern <= 0x01 && @bg_color == 0x41 && @fg_color != 0x40)
    @bg_color = 0x40
    @pattern  = 1
  end

  # Set default alignment if indent is set.
  @text_h_align = 1 if @indent != 0 and @text_h_align == 0


  record         = 0x00E0
  length         = 0x0014

  ifnt           = @font_index
  ifmt           = @num_format


  align          = @text_h_align
  align         |= @text_wrap     << 3
  align         |= @text_v_align  << 4
  align         |= @text_justlast << 7
  align         |= @rotation      << 8

  indent         = @indent
  indent        |= @shrink        << 4
  indent        |= @merge_range   << 5
  indent        |= @reading_order << 6
  indent        |= atr_num        << 10
  indent        |= atr_fnt        << 11
  indent        |= atr_alc        << 12
  indent        |= atr_bdr        << 13
  indent        |= atr_pat        << 14
  indent        |= atr_prot       << 15


  border1        = @left
  border1       |= @right         << 4
  border1       |= @top           << 8
  border1       |= @bottom        << 12

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

  border3        = @top_color
  border3       |= @bottom_color  << 7
  border3       |= @diag_color    << 14
  border3       |= @diag_border   << 21
  border3       |= @pattern       << 26

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

  header = [record, length].pack("vv")
  data   = [ifnt, ifmt, style, align, indent,
            border1, border2, border3, icv].pack("vvvvvvvVv")

  header + data
end