WIN32OLE
WIN32OLE
WIN32OLE 객체는 Ruby 안의 OLE Automation 객체를 나타내요. WIN32OLE을 사용하면 VBScript처럼 OLE 서버에 접근할 수 있어요. 다음은 예시 스크립트예요.
require 'win32ole'
excel = WIN32OLE.new('Excel.Application')
excel.visible = true
workbook = excel.Workbooks.Add();
worksheet = workbook.Worksheets(1);
worksheet.Range("A1:D1").value = ["North","South","East","West"];
worksheet.Range("A2:B2").value = [5.2, 10];
worksheet.Range("C2").value = 8;
worksheet.Range("D2").value = 20;
range = worksheet.Range("A1:D2");
range.select
chart = workbook.Charts.Add;
workbook.saved = true;
excel.ActiveWorkbook.Close(0);
excel.Quit();
아쉽게도 Win32OLE은 참조(reference)로 전달되는 인자를 직접 지원하지 않아요. 대신 Win32OLE은 WIN32OLE::ARGV나 WIN32OLE_VARIANT 객체를 제공하죠. 참조로 전달한 인자의 결과 값을 받고 싶다면 WIN32OLE::ARGV 또는 WIN32OLE_VARIANT를 사용하면 돼요.
oleobj.method(arg1, arg2, refargv3)
puts WIN32OLE::ARGV[2] # the value of refargv3 after called oleobj.method
또는:
refargv3 = WIN32OLE_VARIANT.new(XXX,
WIN32OLE::VARIANT::VT_BYREF|WIN32OLE::VARIANT::VT_XXX)
oleobj.method(arg1, arg2, refargv3)
p refargv3.value # the value of refargv3 after called oleobj.method.
출처: Ruby 3.3 API
본문
클래스 메서드
codepage → integer
현재 코드페이지를 돌려줘요.
WIN32OLE.codepage # => WIN32OLE::CP_ACP
codepage = CP
현재 코드페이지를 설정해요. WIN32OLE.codepage는 Encoding.default_internal에 따라 초기화돼요. Encoding.default_internal이 nil이면 WIN32OLE.codepage는 Encoding.default_external에 따라 초기화돼요.
WIN32OLE.codepage = WIN32OLE::CP_UTF8
WIN32OLE.codepage = 65001
connect(ole) → aWIN32OLE
moniker에서 실행 중인 OLE Automation 객체 또는 WIN32OLE 객체를 돌려줘요. 첫 인자는 OLE program id, class id, 또는 moniker여야 해요.
WIN32OLE.connect('Excel.Application') # => WIN32OLE object which represents running Excel.
const_load(ole, mod = WIN32OLE)
OLE Automation 서버의 상수들을 mod의 상수로 정의해요. 첫 인자는 WIN32OLE 객체 또는 타입 라이브러리 이름이에요. 두 번째 인자를 생략하면 기본값은 WIN32OLE이에요. Ruby 상수 변수 이름의 첫 글자는 대문자라서, WIN32OLE 객체의 상수 변수 이름은 대문자로 시작해요. 예를 들어 Excel의 xlTop 상수는 WIN32OLE에서는 XlTop으로 바뀌죠. 상수 변수의 첫 글자가 [A-Z]가 아니면, 그 상수는 CONSTANTS 해시 요소로 정의돼요.
module EXCEL_CONST
end
excel = WIN32OLE.new('Excel.Application')
WIN32OLE.const_load(excel, EXCEL_CONST)
puts EXCEL_CONST::XlTop # => -4160
puts EXCEL_CONST::CONSTANTS['_xlDialogChartSourceData'] # => 541
WIN32OLE.const_load(excel)
puts WIN32OLE::XlTop # => -4160
module MSO
end
WIN32OLE.const_load('Microsoft Office 9.0 Object Library', MSO)
puts MSO::MsoLineSingle # => 1
create_guid
GUID를 만들어요.
WIN32OLE.create_guid # => {1CB530F1-F6B1-404D-BCE6-1959BF91F4A8}
locale → locale id.
현재 로케일 id(lcid)를 돌려줘요. 기본 로케일은 WIN32OLE::LOCALE_SYSTEM_DEFAULT예요.
lcid = WIN32OLE.locale
locale = lcid
현재 로케일 id(lcid)를 설정해요.
WIN32OLE.locale = 1033 # set locale English(U.S)
obj = WIN32OLE_VARIANT.new("$100,000", WIN32OLE::VARIANT::VT_CY)
new(server, [host]) → WIN32OLE object
새 WIN32OLE 객체(OLE Automation 객체)를 돌려줘요. 첫 인자 server는 OLE Automation 서버를 지정하며, CLSID 또는 PROGID여야 해요. 두 번째 인자 host를 지정하면 host 위의 OLE Automation 객체를 돌려줘요. :license 키워드 인자를 제공하면 IClassFactory2::CreateInstanceLic을 사용해서 라이선스 서버의 인스턴스를 만들어요.
WIN32OLE.new('Excel.Application') # => Excel OLE Automation WIN32OLE object.
WIN32OLE.new('{00024500-0000-0000-C000-000000000046}') # => Excel OLE Automation WIN32OLE object.
ole_free(aWIN32OLE) -→ number
WIN32OLE 객체의 Dispatch 인터페이스의 Release 메서드를 호출해요. 이 메서드는 WIN32OLE 디버깅을 위해서만 존재하므로 직접 사용하지 않는 게 좋아요. 반환값은 OLE 객체의 참조 카운터예요.
ole_reference_count(aWIN32OLE) -→ number
WIN32OLE 객체의 Dispatch 인터페이스의 참조 카운터를 돌려줘요. 이 메서드도 WIN32OLE 디버깅 전용이므로 직접 사용하지 않는 게 좋아요.
ole_show_help(obj [,helpcontext])
도움말 파일을 표시해요. 첫 인자는 WIN32OLE_TYPE 객체, WIN32OLE_METHOD 객체, 또는 도움말 파일을 지정해요.
excel = WIN32OLE.new('Excel.Application')
typeobj = excel.ole_type
WIN32OLE.ole_show_help(typeobj)
인스턴스 메서드
WIN32OLE[a1,a2,...]
a1, a2,... 가 지정한 Collection의 값을 돌려줘요.
dict = WIN32OLE.new('Scripting.Dictionary')
dict.add('ruby', 'Ruby')
puts dict['ruby'] # => 'Ruby' (same as `puts dict.item('ruby')')
주의: 이 메서드로 프로퍼티(property)를 가져올 수는 없어요.
excel = WIN32OLE.new('Excel.Application')
# puts excel['Visible'] This is error !!!
puts excel.Visible # You should to use this style to get the property.
WIN32OLE[a1, a2, ...]=val
a1, a2,... 가 지정한 WIN32OLE 객체에 값을 설정해요.
dict = WIN32OLE.new('Scripting.Dictionary')
dict.add('ruby', 'RUBY')
dict['ruby'] = 'Ruby'
puts dict['ruby'] # => 'Ruby'
주의: 이 메서드로 프로퍼티 값을 설정할 수는 없어요.
excel = WIN32OLE.new('Excel.Application')
# excel['Visible'] = true # This is error !!!
excel.Visible = true # You should to use this style to set the property.
_getproperty(dispid, args, types)
프로퍼티를 가져오는 early binding 메서드를 실행해요. 첫 인자는 dispatch ID, 두 번째 인자는 인자 배열, 세 번째 인자는 인자 타입 배열을 지정해요.
excel = WIN32OLE.new('Excel.Application')
puts excel._getproperty(558, [], []) # same effect as puts excel.visible
_invoke(dispid, args, types)
early binding 메서드를 실행해요. 첫 인자는 dispatch ID, 두 번째 인자는 인자 배열, 세 번째 인자는 인자 타입 배열을 지정해요.
excel = WIN32OLE.new('Excel.Application')
excel._invoke(302, [], []) # same effect as excel.Quit
_setproperty(dispid, args, types)
프로퍼티를 설정하는 early binding 메서드를 실행해요. 첫 인자는 dispatch ID, 두 번째 인자는 인자 배열, 세 번째 인자는 인자 타입 배열을 지정해요.
excel = WIN32OLE.new('Excel.Application')
excel._setproperty(558, [true], [WIN32OLE::VARIANT::VT_BOOL]) # same effect as excel.visible = true
each {|i|...}
IEnumVARIANT 인터페이스를 가진 OLE 컬렉션의 각 항목에 대해 반복해요.
excel = WIN32OLE.new('Excel.Application')
book = excel.workbooks.add
sheets = book.worksheets(1)
cells = sheets.cells("A1:A5")
cells.each do |cell|
cell.value = 10
end
invoke(method, [arg1,...]) → return value of method
OLE 메서드를 실행해요. 첫 인자는 OLE Automation 객체의 메서드 이름을 지정하고, 나머지는 메서드의 인자를 지정해요. 메서드를 직접 실행할 수 없을 때 이 메서드를 대신 사용하면 돼요.
excel = WIN32OLE.new('Excel.Application')
excel.invoke('Quit') # => same as excel.Quit
method_missing(id [,arg1, arg2, ...])
WIN32OLE#invoke 메서드를 호출해요.
methods
Object#methods를 오버라이드해서, WIN32OLE이 did_you_mean 젬과 잘 동작하게 해요. 실험적이에요.
require 'win32ole'
dict = WIN32OLE.new('Scripting.Dictionary')
dict.Ade('a', 1)
#=> Did you mean? Add
ole_activex_initialize() → Qnil
IPersistMemory::InitNew를 호출해서 WIN32OLE 객체(ActiveX Control)를 초기화해요. OLE 메서드를 호출하기 전에, MFC로 만든 어떤 종류의 ActiveX 컨트롤은 IPersistXXX::InitNew를 호출해서 초기화해야 해요. "HRESULT error code: 0x8000ffff catastrophic failure" 예외를 정확히 받은 경우에만, 어떤 ole_method를 호출하기 전에 이 메서드를 시도해 보세요.
obj = WIN32OLE.new("ProgID_or_GUID_of_ActiveX_Control")
obj.ole_activex_initialize
obj.method(...)
ole_free
WIN32OLE 객체의 Dispatch 인터페이스의 Release 메서드를 호출해요. 보통은 WIN32OLE 객체가 가비지 컬렉션될 때 Release 메서드가 자동으로 호출되므로, 직접 호출할 필요는 없어요.
ole_func_methods
WIN32OLE_METHOD 객체의 배열을 돌려줘요. 배열의 요소는 WIN32OLE 객체의 프로퍼티(settable)예요.
excel = WIN32OLE.new('Excel.Application')
properties = excel.ole_func_methods
ole_get_methods
WIN32OLE_METHOD 객체의 배열을 돌려줘요. 배열의 요소는 WIN32OLE 객체의 프로퍼티(gettable)예요.
excel = WIN32OLE.new('Excel.Application')
properties = excel.ole_get_methods
ole_method_help(method) → WIN32OLE_METHOD
첫 인자가 지정한 method에 대응하는 WIN32OLE_METHOD 객체를 돌려줘요.
excel = WIN32OLE.new('Excel.Application')
method = excel.ole_method_help('Quit')
ole_methods
WIN32OLE_METHOD 객체의 배열을 돌려줘요. 요소는 WIN32OLE 객체의 OLE 메서드예요.
excel = WIN32OLE.new('Excel.Application')
methods = excel.ole_methods
ole_put_methods
WIN32OLE_METHOD 객체의 배열을 돌려줘요. 배열의 요소는 WIN32OLE 객체의 프로퍼티(settable)예요.
excel = WIN32OLE.new('Excel.Application')
properties = excel.ole_put_methods
ole_query_interface(iid) → WIN32OLE object
iid가 지정한 특정 dispatch 또는 dual 인터페이스에 대한 WIN32OLE 객체를 돌려줘요.
ie = WIN32OLE.new('InternetExplorer.Application')
ie_web_app = ie.ole_query_interface('{0002DF05-0000-0000-C000-000000000046}') # => WIN32OLE object for dispinterface IWebBrowserApp
ole_respond_to?(method) → true or false
OLE 객체에 OLE 메서드가 있으면 true를, 없으면 false를 돌려줘요.
ie = WIN32OLE.new('InternetExplorer.Application')
ie.ole_respond_to?("gohome") => true
ole_type → WIN32OLE_TYPE
WIN32OLE_TYPE 객체를 돌려줘요.
excel = WIN32OLE.new('Excel.Application')
tobj = excel.ole_type
ole_typelib → The WIN32OLE_TYPELIB object
WIN32OLE_TYPELIB 객체를 돌려줘요. 이 객체는 WIN32OLE 객체를 포함하는 타입 라이브러리를 나타내요.
excel = WIN32OLE.new('Excel.Application')
tlib = excel.ole_typelib
puts tlib.name # -> 'Microsoft Excel 9.0 Object Library'
setproperty('property', [arg1, arg2,...] val)
OLE 객체의 프로퍼티를 설정해요. 인자와 함께 프로퍼티를 설정하고 싶을 때 이 메서드를 써요.
excel = WIN32OLE.new('Excel.Application')
excel.Visible = true
book = excel.workbooks.add
sheet = book.worksheets(1)
sheet.setproperty('Cells', 1, 2, 10) # => The B1 cell value is 10.