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


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

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
  if options[:remote]
    zipfile = Tempfile.new("file")
    zipfile.binmode
    zipfile.write(HTTP.get(path).to_s)
    zipfile.close
    path = zipfile.path
  end
  @files = Zip::File.open(path)
  @shared_strings = SharedStrings.new(self)
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

Instance Method Details

#base_dateObject


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/creek/book.rb', line 62

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


58
59
60
# File 'lib/creek/book.rb', line 58

def close
  @files.close
end

#style_typesObject


54
55
56
# File 'lib/creek/book.rb', line 54

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