Class: Konjac::Office::Mac::Excel

Inherits:
Shared show all
Defined in:
lib/konjac/office/mac/excel.rb

Overview

Excel for Mac

Instance Attribute Summary

Attributes inherited from Base

#document

Instance Method Summary collapse

Methods inherited from Shared

#close, #open, #path

Methods inherited from Base

#[], #[]=, #export, #import, #read, #shape_at, #tags, #write

Constructor Details

#initialize(path = nil) ⇒ Excel

Creates a new Excel object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/konjac/office/mac/excel.rb', line 8

def initialize(path = nil)
  super "Microsoft Excel", path
  @strippable = //
  @delimiter = "\r"
  @item_opts.merge!({
    :ref_path     => [:sheet, :row, :cell],
    :content_path => [:formula],
    :strippable   => /[\r\n\a]+$/
  })
  @shape_opts.merge!({
    :ref_path     => [:sheet, :shape],
    :content_path => [:text_frame, :characters, :content],
    :strippable   => /[\r\n\a]+$/
  })
end

Instance Method Details

#active_documentObject

Retrieves the active document and caches it



25
26
27
# File 'lib/konjac/office/mac/excel.rb', line 25

def active_document
  @active_document ||= @application.active_workbook
end

#dataObject

Creates a dump of the spreadsheet’s data in Tag form



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/konjac/office/mac/excel.rb', line 30

def data
  tags = []
  @document.sheets.get.each_with_index do |sheet, s|
    sheet.used_range.rows.get.each_with_index do |row, r|
      row.cells.get.each_with_index do |cell, c|
        temp = Tag.new
        temp.indices = [s + 1, r + 1, c + 1]
        temp.removed = temp.added = read(s + 1, r + 1, c + 1)
        tags << temp unless temp.blank?
      end
    end

    # TODO: I should optimize this later like above, to prevent large
    # shapes from getting out of hand
    sheet.shapes.get.each_with_index do |shape, index|
      temp = Tag.new
      temp.indices = [s + 1, index + 1]
      temp.removed = temp.added =
        clean(shape.text_frame.characters.content.get, :shape)
      temp.type = :shape
      tags << temp unless temp.blank?
    end rescue NoMethodError  # ignore sheets without shapes
  end
  tags
end

#sizeObject Also known as: length

Retrieves the number of cells in the document. Note that this method fetches all row and column elements and can thus be very expensive for large spreadsheets.



59
60
61
62
63
64
# File 'lib/konjac/office/mac/excel.rb', line 59

def size
  @document.sheets.get.inject(0) do |result, sheet|
    range   = sheet.used_range 
    result += range.rows.get.size * range.columns.get.size
  end
end