Module: FastExcel::WorksheetExt

Includes:
AttributeHelper
Defined in:
lib/fast_excel.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from AttributeHelper

#fields_hash, #pretty_print, #set

Instance Attribute Details

#workbookObject

Returns the value of attribute workbook.


416
417
418
# File 'lib/fast_excel.rb', line 416

def workbook
  @workbook
end

Instance Method Details

#<<(values) ⇒ Object


511
512
513
# File 'lib/fast_excel.rb', line 511

def <<(values)
  append_row(values)
end

#add_text_width(value, format, cell_number) ⇒ Object


479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
# File 'lib/fast_excel.rb', line 479

def add_text_width(value, format, cell_number)
  font_size = 0
  if format
    font_size = format.font_size
  end

  if font_size == 0
    if @col_formats[cell_number] && @col_formats[cell_number].font_size
      font_size = @col_formats[cell_number].font_size
    end
  end

  if font_size == 0
    font_size = workbook.default_format.font_size
  end

  font_size = 13 if font_size == nil || font_size == 0

  scale = 0.08
  new_width = (scale * font_size * value.to_s.length )
  @column_widths[cell_number] = if new_width > (@column_widths[cell_number] || 0)
    new_width
  else
    @column_widths[cell_number]
  end
end

#append_row(values, formats = nil) ⇒ Object


506
507
508
509
# File 'lib/fast_excel.rb', line 506

def append_row(values, formats = nil)
  @last_row_number += 1
  write_row(last_row_number, values, formats)
end

#auto_width=(v) ⇒ Object


441
442
443
444
# File 'lib/fast_excel.rb', line 441

def auto_width=(v)
  @auto_width = v
  @column_widths = {}
end

#auto_width?Boolean

Returns:

  • (Boolean)

437
438
439
# File 'lib/fast_excel.rb', line 437

def auto_width?
  defined?(@auto_width) && @auto_width
end

#calculated_column_widthsObject


446
447
448
# File 'lib/fast_excel.rb', line 446

def calculated_column_widths
  @column_widths || {}
end

#closeObject


542
543
544
545
546
547
548
# File 'lib/fast_excel.rb', line 542

def close
  if auto_width?
    @column_widths.each do |num, width|
      set_column_width(num, width + 0.2)
    end
  end
end

#enable_filters!(start_col: 0, end_col:) ⇒ Object


538
539
540
# File 'lib/fast_excel.rb', line 538

def enable_filters!(start_col: 0, end_col:)
  autofilter(start_col, 0, @last_row_number, end_col)
end

#initialize(struct) ⇒ Object


420
421
422
423
424
425
# File 'lib/fast_excel.rb', line 420

def initialize(struct)
  @is_open = true
  @col_formats = {}
  @last_row_number = -1
  super(struct)
end

#last_row_numberObject


515
516
517
# File 'lib/fast_excel.rb', line 515

def last_row_number
  @last_row_number
end

#set_column(start_col, end_col, width = nil, format = nil) ⇒ Object


519
520
521
522
523
524
525
526
# File 'lib/fast_excel.rb', line 519

def set_column(start_col, end_col, width = nil, format = nil)
  super(start_col, end_col, width || DEF_COL_WIDTH, format)

  return unless format
  start_col.upto(end_col) do |i|
    @col_formats[i] = format
  end
end

#set_column_width(col, width) ⇒ Object


528
529
530
# File 'lib/fast_excel.rb', line 528

def set_column_width(col, width)
  set_column(col, col, width, @col_formats[col])
end

#set_columns_width(start_col, end_col, width) ⇒ Object


532
533
534
535
536
# File 'lib/fast_excel.rb', line 532

def set_columns_width(start_col, end_col, width)
  start_col.upto(end_col) do |i|
    set_column_width(i, width)
  end
end

#write_row(row_number, values, formats = nil) ⇒ Object


427
428
429
430
431
432
433
434
435
# File 'lib/fast_excel.rb', line 427

def write_row(row_number, values, formats = nil)
  values.each_with_index do |value, index|
    format = if formats
      formats.is_a?(Array) ? formats[index] : formats
    end

    write_value(row_number, index, value, format)
  end
end

#write_value(row_number, cell_number, value, format = nil) ⇒ Object


450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
# File 'lib/fast_excel.rb', line 450

def write_value(row_number, cell_number, value, format = nil)

  if workbook.constant_memory? && row_number < @last_row_number
    raise ArgumentError, "Can not write to saved row in constant_memory mode (attempted row: #{row_number}, last saved row: #{last_row_number})"
  end

  if value.is_a?(Numeric)
    write_number(row_number, cell_number, value, format)
  elsif defined?(Date) && value.is_a?(Date)
    write_datetime(row_number, cell_number, FastExcel.lxw_datetime(value.to_datetime), format)
  elsif value.is_a?(Time)
    write_number(row_number, cell_number, FastExcel.date_num(value), format)
  elsif defined?(DateTime) && value.is_a?(DateTime)
    write_number(row_number, cell_number, FastExcel.date_num(value), format)
  elsif value.is_a?(TrueClass) || value.is_a?(FalseClass)
    write_boolean(row_number, cell_number, value ? 1 : 0, format)
  elsif value.is_a?(FastExcel::Formula)
    write_formula(row_number, cell_number, value.fml, format)
  elsif value.is_a?(FastExcel::URL)
    write_url(row_number, cell_number, value.url, format)
    add_text_width(value.url, format, cell_number) if auto_width?
  else
    write_string(row_number, cell_number, value.to_s, format)
    add_text_width(value, format, cell_number) if auto_width?
  end

  @last_row_number = row_number > @last_row_number ? row_number : @last_row_number
end