Class: Creek::Book

Inherits:
Object
  • Object
show all
Defined in:
lib/creek/book.rb

Constant Summary collapse

DATE_1900 =
Date.new(1899, 12, 30).freeze
DATE_1904 =
Date.new(1904, 1, 1).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, options = {}) ⇒ Book

Returns a new instance of Book.



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/creek/book.rb', line 18

def initialize path, options = {}
  check_file_extension = options.fetch(:check_file_extension, true)
  if check_file_extension
    extension = File.extname(options[:original_filename] || path).downcase
    raise 'Not a valid file format.' unless (['.xlsx', '.xlsm'].include? extension)
  end
  path = download_file(path) if options[:remote]
  @files = Zip::File.open(path)
  @shared_strings = SharedStrings.new(self)
  @with_headers = options.fetch(:with_headers, false)
end

Instance Attribute Details

#filesObject (readonly)

Returns the value of attribute files.



10
11
12
# File 'lib/creek/book.rb', line 10

def files
  @files
end

#shared_stringsObject (readonly)

Returns the value of attribute shared_strings.



10
11
12
# File 'lib/creek/book.rb', line 10

def shared_strings
  @shared_strings
end

#sheetsObject (readonly)

Returns the value of attribute sheets.



10
11
12
# File 'lib/creek/book.rb', line 10

def sheets
  @sheets
end

#with_headersObject (readonly)

Returns the value of attribute with_headers.



10
11
12
# File 'lib/creek/book.rb', line 10

def with_headers
  @with_headers
end

Instance Method Details

#base_dateObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/creek/book.rb', line 68

def base_date
  @base_date ||=
  begin
    # Default to 1900 (minus one day due to excel quirk) but use 1904 if
    # it's set in the Workbook's workbookPr
    # http://msdn.microsoft.com/en-us/library/ff530155(v=office.12).aspx
    result = DATE_1900 # default

    doc = @files.file.open "xl/workbook.xml"
    xml = Nokogiri::XML::Document.parse doc
    xml.css('workbookPr[date1904]').each do |workbookPr|
      if workbookPr['date1904'] =~ /true|1/i
        result = DATE_1904
        break
      end
    end

    result
  end
end

#closeObject



64
65
66
# File 'lib/creek/book.rb', line 64

def close
  @files.close
end

#style_typesObject



60
61
62
# File 'lib/creek/book.rb', line 60

def style_types
  @style_types ||= Creek::Styles.new(self).style_types
end