Method: Spreadsheet::Excel::Reader#read_formula

Defined in:
lib/spreadsheet/excel/reader.rb

#read_formula(worksheet, addr, work) ⇒ Object



313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
# File 'lib/spreadsheet/excel/reader.rb', line 313

def read_formula worksheet, addr, work
  # Offset  Size  Contents
  #      0     2  Index to row
  #      2     2  Index to column
  #      4     2  Index to XF record (➜ 6.115)
  #      6     8  Result of the formula. See below for details.
  #     14     2  Option flags:
  #               Bit  Mask    Contents
  #                 0  0x0001  1 = Recalculate always
  #                 1  0x0002  1 = Calculate on open
  #                 3  0x0008  1 = Part of a shared formula
  #     16     4  Not used
  #     20  var.  Formula data (RPN token array, ➜ 4)
  #               Offset  Size  Contents
  #                    0     2  Size of the following formula data (sz)
  #                    2    sz  Formula data (RPN token array)
  #               [2+sz]  var.  (optional) Additional data for specific tokens
  #                             (➜ 4.1.6, for example tArray token, ➜ 4.8.7)
  #
  # Result of the Formula
  # Dependent on the type of value the formula returns, the result field has
  # the following format:
  #
  # Result is a numeric value:
  # Offset  Size  Contents
  #      0     8  IEEE 754 floating-point value (64-bit double precision)
  #
  # Result is a string (the string follows in a STRING record, ➜ 6.98):
  # Offset  Size  Contents
  #      0     1  0x00 (identifier for a string value)
  #      1     5  Not used
  #      6     2  0xffff
  # Note: In BIFF8 the string must not be empty. For empty cells there is a
  # special identifier defined (see below).
  #
  # Result is a Boolean value:
  # Offset  Size  Contents
  #      0     1  0x01 (identifier for a Boolean value)
  #      1     1  Not used
  #      2     1  0 = FALSE, 1 = TRUE
  #      3     3  Not used
  #      6     2  0xffff
  #
  # Result is an error value:
  # Offset  Size  Contents
  #      0     1  0x02 (identifier for an error value)
  #      1     1  Not used
  #      2     1  Error code (➜ 3.7)
  #      3     3  Not used
  #      6     2  0xffff
  #
  # Result is an empty cell (BIFF8), for example an empty string:
  # Offset  Size  Contents
  #      0     1  0x03 (identifier for an empty cell)
  #      1     5  Not used
  #      6     2  0xffff
  row, column, xf, rtype, rval, rcheck, opts = work.unpack 'v3CxCx3v2'
  formula = Formula.new
  formula.shared = (opts & 0x08) > 0
  formula.data = work[20..-1]
  if rcheck != 0xffff || rtype > 3
    value, = work.unpack 'x6E'
    unless value
      # on architectures where sizeof(double) > 8
      value, = work.unpack 'x6e'
    end
    formula.value = value
  elsif rtype == 0
    pos, op, len, work = get_next_chunk
    if op == :sharedfmla
      ## TODO: formula-support in 0.8.0
      pos, op, len, work = get_next_chunk
    end
    if op == :string
      formula.value = client read_string(work, 2), @workbook.encoding
    else
      warn "String Value expected after Formula, but got #{op}"
      formula.value = Error.new 0x2a
      @pos = pos
    end
  elsif rtype == 1
    formula.value = rval > 0
  elsif rtype == 2
    formula.value = Error.new rval
  else
    # leave the Formula value blank
  end
  set_cell worksheet, row, column, xf, formula
end