Class: SimpleXlsxReader::Loader::WorkbookParser

Inherits:
Struct
  • Object
show all
Defined in:
lib/simple_xlsx_reader/loader/workbook_parser.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#file_ioObject

Returns the value of attribute file_io

Returns:

  • (Object)

    the current value of file_io



5
6
7
# File 'lib/simple_xlsx_reader/loader/workbook_parser.rb', line 5

def file_io
  @file_io
end

Class Method Details

.parse(file_io) ⇒ Object



6
7
8
9
# File 'lib/simple_xlsx_reader/loader/workbook_parser.rb', line 6

def self.parse(file_io)
  parser = new(file_io).tap(&:parse)
  [parser.sheet_toc, parser.base_date]
end

Instance Method Details

#base_dateObject

Returns the base_date from which to calculate dates. Defaults to 1900 (minus two days due to excel quirk), but use 1904 if it’s set in the Workbook’s workbookPr. msdn.microsoft.com/en-us/library/ff530155(v=office.12).aspx



28
29
30
31
32
33
34
35
36
# File 'lib/simple_xlsx_reader/loader/workbook_parser.rb', line 28

def base_date
  return DATE_SYSTEM_1900 if @xml.nil?

  @xml.xpath('//workbook/workbookPr[@date1904]').each do |workbookPr|
    return DATE_SYSTEM_1904 if workbookPr['date1904'] =~ /true|1/i
  end

  DATE_SYSTEM_1900
end

#parseObject



11
12
13
# File 'lib/simple_xlsx_reader/loader/workbook_parser.rb', line 11

def parse
  @xml = Nokogiri::XML(file_io.read).remove_namespaces!
end

#sheet_tocObject

Table of contents for the sheets, ex. => 0, …



16
17
18
19
20
21
22
# File 'lib/simple_xlsx_reader/loader/workbook_parser.rb', line 16

def sheet_toc
  @xml.xpath('/workbook/sheets/sheet')
    .each_with_object({}) do |sheet, acc|
      acc[sheet.attributes['name'].value] =
        sheet.attributes['sheetId'].value.to_i - 1 # keep things 0-indexed
    end
end