Sha256: b7ede5888f8c4be17425f4e1d3d5614ded48097376b88b514788f4f7fe5c80ab

Contents?: true

Size: 1.43 KB

Versions: 5

Compression:

Stored size: 1.43 KB

Contents

require 'roo/excelx/extractor'

module Roo
  class Excelx::Workbook < Excelx::Extractor
    class Label
      attr_reader :sheet, :row, :col, :name

      def initialize(name, sheet, row, col)
        @name = name
        @sheet = sheet
        @row = row.to_i
        @col = ::Roo::Utils.letter_to_number(col)
      end

      def key
        [@row, @col]
      end
    end

    def initialize(path)
      super
      if !doc_exists?
        raise ArgumentError, 'missing required workbook file'
      end
    end

    def sheets
      doc.xpath("//sheet")
    end

    # aka labels
    def defined_names
      Hash[doc.xpath("//definedName").map do |defined_name|
        # "Sheet1!$C$5"
        sheet, coordinates = defined_name.text.split('!$', 2)
        col,row = coordinates.split('$')
        name = defined_name['name']
        [name, Label.new(name, sheet,row,col)]
      end]
    end

    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.new(1899,12,30) # default
          doc.css("workbookPr[date1904]").each do |workbookPr|
            if workbookPr["date1904"] =~ /true|1/i
              result = Date.new(1904,01,01)
              break
            end
          end
          result
        end
    end
  end
end

Version data entries

5 entries across 5 versions & 3 rubygems

Version Path
culturecode-roo-2.0.2 lib/roo/excelx/workbook.rb
culturecode-roo-2.0.1 lib/roo/excelx/workbook.rb
roo-2.0.0 lib/roo/excelx/workbook.rb
roo-2.0.0beta1 lib/roo/excelx/workbook.rb
roo-andyw8-2.0.0 lib/roo/excelx/workbook.rb