Module: Konjac::Office

Defined in:
lib/konjac/office.rb,
lib/konjac/office/os.rb,
lib/konjac/office/mac.rb,
lib/konjac/office/tag.rb,
lib/konjac/office/xml.rb,
lib/konjac/office/base.rb,
lib/konjac/office/item.rb,
lib/konjac/office/windows.rb,
lib/konjac/office/mac/word.rb,
lib/konjac/office/mac/excel.rb,
lib/konjac/office/mac/shared.rb,
lib/konjac/office/xml/shared.rb,
lib/konjac/office/mac/power_point.rb

Overview

A singleton for dealing with extracting and importing tags from Microsoft Office documents. It also has abstract some of the lengthier calls to the different applications of different environments by detecting the user’s operating system and using ghost methods. For example, the following are equivalent for opening the document at ~/text.doc on a Mac:

doc = Konjac::Office::Mac::Word.new("~/text.doc")
doc = Konjac::Office.word("~/text.doc")

In addition, if the document is already open and is the active document, the following will work too:

doc = Konjac::Office.word

From there, you can send some basic reading and writing instructions to the document:

doc.read 1
doc.read :paragraph => 1
doc.read 1, :type => :shape

doc.write "First paragraph", 1

doc.export
doc.import

Defined Under Namespace

Modules: Mac, OS, Windows, XML Classes: Base, Tag

Class Method Summary collapse

Class Method Details

.environmentObject

The user’s environment. Currently, this just detects whether an OS is Windows, Mac or other (simply because Microsoft Office is unavailable on Linux, so scripting something like LibreOffice or OpenOffice is more long-term).



58
59
60
61
62
63
64
65
66
# File 'lib/konjac/office.rb', line 58

def environment
  if OS.mac?
    Mac
  elsif OS.windows?
    Windows
  else
    nil 
  end
end

.new(path) ⇒ Object

Creates a new object inheriting from Base. The application chosen is based on the user’s environment and the type of the document. For example, a file with a .docx extension will default to Microsoft Word, and on OSX it will default to Konjac::Office::Mac::Word



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/konjac/office.rb', line 40

def new(path)
  env = environment
  return nil if env.nil?

  case File.basename(path)
  when /\.docx?/
    env::Word.new(path)
  when /\.pptx?/
    env::PowerPoint.new(path)
  when /\.xlsx?/
    env::Excel.new(path)
  end
end