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


411
412
413
# File 'lib/fast_excel.rb', line 411

def workbook
  @workbook
end

Instance Method Details

#<<(values) ⇒ Object


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

def <<(values)
  append_row(values)
end

#add_text_width(value, format, cell_number) ⇒ Object


474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
# File 'lib/fast_excel.rb', line 474

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


501
502
503
504
# File 'lib/fast_excel.rb', line 501

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

#auto_width=(v) ⇒ Object


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

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

#auto_width?Boolean

Returns:

  • (Boolean)

432
433
434
# File 'lib/fast_excel.rb', line 432

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

#calculated_column_widthsObject


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

def calculated_column_widths
  @column_widths || {}
end

#closeObject


537
538
539
540
541
542
543
# File 'lib/fast_excel.rb', line 537

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


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

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

#initialize(struct) ⇒ Object


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

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

#last_row_numberObject


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

def last_row_number
  @last_row_number
end

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


514
515
516
517
518
519
520
521
# File 'lib/fast_excel.rb', line 514

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


523
524
525
# File 'lib/fast_excel.rb', line 523

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

#set_columns_width(start_col, end_col, width) ⇒ Object


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

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


422
423
424
425
426
427
428
429
430
# File 'lib/fast_excel.rb', line 422

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


445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
# File 'lib/fast_excel.rb', line 445

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