Sha256: f89bc0465ea940f08761fce82b22becfba6d911119fad43364398045bf1dfef3

Contents?: true

Size: 1.32 KB

Versions: 5

Compression:

Stored size: 1.32 KB

Contents

require 'roo/font'
require 'roo/excelx/extractor'

module Roo
  class Excelx::Styles < Excelx::Extractor
    # convert internal excelx attribute to a format
    def style_format(style)
      id = num_fmt_ids[style.to_i]
      num_fmts[id] || Excelx::Format::STANDARD_FORMATS[id.to_i]
    end

    def definitions
      @definitions ||= extract_definitions
    end

    private

    def num_fmt_ids
      @num_fmt_ids ||= extract_num_fmt_ids
    end

    def num_fmts
      @num_fmts ||= extract_num_fmts
    end

    def fonts
     @fonts ||= extract_fonts
    end

    def extract_definitions
      doc.xpath("//cellXfs").flat_map do |xfs|
        xfs.children.map do |xf|
          fonts[xf['fontId'].to_i]
        end
      end
    end

    def extract_fonts
      doc.xpath("//fonts/font").map do |font_el|
        Font.new.tap do |font|
          font.bold = !font_el.xpath('./b').empty?
          font.italic = !font_el.xpath('./i').empty?
          font.underline = !font_el.xpath('./u').empty?
        end
      end
    end

    def extract_num_fmt_ids
      doc.xpath("//cellXfs").flat_map do |xfs|
        xfs.children.map do |xf|
          xf['numFmtId']
        end
      end
    end

    def extract_num_fmts
      Hash[doc.xpath("//numFmt").map do |num_fmt|
        [num_fmt['numFmtId'], num_fmt['formatCode']]
      end]
    end
  end
end

Version data entries

5 entries across 5 versions & 3 rubygems

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