Class: ExcelConnection

Inherits:
Object
  • Object
show all
Defined in:
lib/jruby_excelcom/excel_connection.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(use_active_instance = false) ⇒ ExcelConnection

initializes com and connects to an excel instance

use_active_instance

whether an existing excel instance should be used or a new isntance should be created. Default value is false



6
7
8
# File 'lib/jruby_excelcom/excel_connection.rb', line 6

def initialize(use_active_instance = false)
  @con = JavaExcelcom::ExcelConnection::connect(use_active_instance)
end

Class Method Details

.connect(use_active_instance = false) ⇒ Object

see new optional block possible, where ExcelConnection#quit gets called on blocks end e.g. ExcelConnection::connect{|con| con.workbook ... }



13
14
15
16
17
18
19
20
21
# File 'lib/jruby_excelcom/excel_connection.rb', line 13

def self.connect(use_active_instance = false)
  con = self.new(use_active_instance)
  if block_given?
    yield(con)
    con.quit
  else
    con
  end
end

.initialize_comObject

initializes com manually, not recommended! happens automatically when an instance is created



24
25
26
# File 'lib/jruby_excelcom/excel_connection.rb', line 24

def self.initialize_com
  JavaExcelcom::ExcelConnection::initialize_com
end

.uninitialize_comObject

uninitializes com manually, not recommended! should happen automatically when ExcelConnection#quit is called.



29
30
31
# File 'lib/jruby_excelcom/excel_connection.rb', line 29

def self.uninitialize_com
  JavaExcelcom::ExcelConnection::uninitialize_com
end

Instance Method Details

#active_workbookObject Also known as: getActiveWorkbook

gets the active workbook



57
58
59
# File 'lib/jruby_excelcom/excel_connection.rb', line 57

def active_workbook
  Workbook.new(@con.getActiveWorkbook)
end

#display_alerts=(da) ⇒ Object Also known as: setDisplayAlerts

whether dialog boxes should show up or not (e.g. when saving and overwriting a file)



40
41
42
# File 'lib/jruby_excelcom/excel_connection.rb', line 40

def display_alerts=(da)
  @con.setDisplayAlerts(da)
end

#new_workbook(file) ⇒ Object Also known as: add_workbook

creates a new workbook. If block is given, workbook will be saved and closed at end



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/jruby_excelcom/excel_connection.rb', line 82

def new_workbook(file)
  if file.is_a? String
    wb = Workbook.new(@con.newWorkbook(java.io.File.new(file)))
  else
    wb = Workbook.new(@con.newWorkbook(java.io.File.new(file.path)))
  end
  if block_given?
    yield(wb)
    wb.close true
  else
    wb
  end
end

#quitObject

quits the excel instance and uninitializes com



52
53
54
# File 'lib/jruby_excelcom/excel_connection.rb', line 52

def quit
  @con.quit
end

#versionObject Also known as: getVersion

gets excel version



46
47
48
# File 'lib/jruby_excelcom/excel_connection.rb', line 46

def version
  @con.getVersion
end

#visible=(v) ⇒ Object Also known as: setVisible

whether the excel instance should be visible or not



34
35
36
# File 'lib/jruby_excelcom/excel_connection.rb', line 34

def visible=(v)
  @con.setVisible v
end

#workbook(file) ⇒ Object Also known as: openWorkbook, open_workbook

opens a workbook. Optional block possible where workbook gets closed on blocks end, e.g. con.workbook{|wb| puts wb.name }

file

workbook to be opened. Can be a string or a file object



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/jruby_excelcom/excel_connection.rb', line 65

def workbook(file)
  if file.is_a? String
    wb = Workbook.new(@con.openWorkbook(java.io.File.new(file)))
  else
    wb = Workbook.new(@con.openWorkbook(java.io.File.new(file.path)))
  end
  if block_given?
    yield(wb)
    wb.close
  else
    wb
  end
end