Module: FastExcel

Defined in:
lib/fast_excel.rb

Defined Under Namespace

Modules: WorkbookExt, WorksheetExt

Constant Summary collapse

DEF_COL_WIDTH =

include Libxlsxwriter

8.43
XLSX_DATE_DAY =

seconds in 1 day

86400.0
XLSX_DATE_EPOCH_DIFF =

days between 1970-jan-01 and 1900-jan-01

25567

Class Method Summary collapse

Class Method Details

.date_num(time, offset = nil) ⇒ Object

Convert time to number of days, and change beginning point from 1st jan 1970 to 1st jan 1900 Offset argument should be number of seconds, if not specified then it will use Time.zone.utc_offset || 0

support.microsoft.com/en-us/help/214330/differences-between-the-1900-and-the-1904-date-system-in-excel



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/fast_excel.rb', line 55

def self.date_num(time, offset = nil)
  unless offset
    # Try use Rails' app timezone
    if Time.respond_to?(:zone)
      offset = Time.zone.utc_offset
    else
      offset = 0 # rollback to UTC
    end
  end

  time.to_f / XLSX_DATE_DAY + XLSX_DATE_EPOCH_DIFF + offset / XLSX_DATE_DAY
end

.lxw_datetime(time) ⇒ Object

Creates internal Libxlsxwriter::Datetime from Datetime object



22
23
24
25
26
27
28
29
30
31
# File 'lib/fast_excel.rb', line 22

def self.lxw_datetime(time)
  date = Libxlsxwriter::Datetime.new
  date[:year] = time.year
  date[:month] = time.month
  date[:day] = time.day
  date[:hour] = time.hour
  date[:min] = time.minute
  date[:sec] = time.second
  date
end

.lxw_time(time) ⇒ Object

Creates internal Libxlsxwriter::Datetime from Time object



34
35
36
37
38
39
40
41
42
43
# File 'lib/fast_excel.rb', line 34

def self.lxw_time(time)
  date = Libxlsxwriter::Datetime.new
  date[:year] = time.year
  date[:month] = time.month
  date[:day] = time.day
  date[:hour] = time.hour
  date[:min] = time.min
  date[:sec] = time.sec
  date
end

.open(filename, constant_memory: false) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/fast_excel.rb', line 8

def self.open(filename, constant_memory: false)
  filename = filename.to_s if defined?(Pathname) && filename.is_a?(Pathname)

  workbook = if constant_memory
    opt = Libxlsxwriter::WorkbookOptions.new
    opt[:constant_memory] = 1
    Libxlsxwriter.workbook_new_opt(filename, opt)
  else
    Libxlsxwriter.workbook_new(filename)
  end
  Libxlsxwriter::Workbook.new(workbook)
end