[ruby/win32ole] [DOC] Move sample to toplevel

70ea60c4d2
This commit is contained in:
Nobuyoshi Nakada 2023-12-31 17:44:20 +09:00 committed by git
parent 703ad99bf8
commit 3ad54239b5
10 changed files with 0 additions and 0 deletions

37
sample/win32ole/excel1.rb Normal file
View file

@ -0,0 +1,37 @@
# frozen_string_literal: false
require 'win32ole'
application = WIN32OLE.new('Excel.Application')
application.visible = true
workbook = application.Workbooks.Add();
worksheet = workbook.Worksheets(1);
=begin
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;
=end
worksheet.Range("A1:B2").value = [["North","South"],
[5.2, 10]];
vals = WIN32OLE_VARIANT.new([["East","West"],
[8, 20]],
WIN32OLE::VARIANT::VT_ARRAY)
worksheet.Range("C1:D2").value = vals
range = worksheet.Range("A1:D2");
range.Select
chart = workbook.Charts.Add;
workbook.saved = true;
print "Now quit Excel... Please enter."
gets
application.ActiveWorkbook.Close(0);
application.Quit();

31
sample/win32ole/excel2.rb Normal file
View file

@ -0,0 +1,31 @@
# frozen_string_literal: false
require 'win32ole'
# -4100 is the value for the Excel constant xl3DColumn.
ChartTypeVal = -4100;
# Creates OLE object to Excel
excel = WIN32OLE.new("excel.application")
# Create and rotate the chart
excel.visible = true;
excel.Workbooks.Add();
excel.Range("a1").value = 3;
excel.Range("a2").value = 2;
excel.Range("a3").value = 1;
excel.Range("a1:a3").Select();
excelchart = excel.Charts.Add();
excelchart.type = ChartTypeVal;
i = 0
i.step(180, 10) do |rot|
excelchart.rotation=rot;
sleep 0.1
end
# Done, bye
print "Now quit Excel... Please enter."
gets
excel.ActiveWorkbook.Close(0);
excel.Quit();

21
sample/win32ole/excel3.rb Normal file
View file

@ -0,0 +1,21 @@
# frozen_string_literal: false
require 'win32ole'
#application = WIN32OLE.new('Excel.Application.5')
application = WIN32OLE.new('Excel.Application')
application.visible = true
workbook = application.Workbooks.Add();
sheet = workbook.Worksheets(1);
sheetS = workbook.Worksheets
puts "The number of sheets is #{sheetS.count}"
puts "Now add 2 sheets after of `#{sheet.name}`"
sheetS.add({'count'=>2, 'after'=>sheet})
puts "The number of sheets is #{sheetS.count}"
print "Now quit Excel... Please enter."
gets
application.ActiveWorkbook.Close(0);
application.Quit();

12
sample/win32ole/ie.rb Normal file
View file

@ -0,0 +1,12 @@
# frozen_string_literal: false
require 'win32ole'
url = 'http://www.ruby-lang.org/'
ie = WIN32OLE.new('InternetExplorer.Application')
ie.visible = true
ie.gohome
print "Now navigate Ruby home page... Please enter."
gets
ie.navigate(url)
print "Now quit Internet Explorer... Please enter."
gets
ie.Quit()

View file

@ -0,0 +1,33 @@
# frozen_string_literal: false
require 'win32ole'
ie = WIN32OLE.new('InternetExplorer.Application')
=begin
WIN32OLE.const_load(ie)
WIN32OLE.constants.sort.each do |c|
puts "#{c} = #{WIN32OLE.const_get(c)}"
end
=end
module IE_CONST
end
WIN32OLE.const_load(ie, IE_CONST)
IE_CONST.constants.sort.each do |c|
puts "#{c} = #{IE_CONST.const_get(c)}"
end
#------------------------------------------------------------
# Remark!!! CONSTANTS has not tested enoughly!!!
# CONSTANTS is alpha release.
# If there are constants which first letter is not [a-zA-Z],
# like a '_Foo', then maybe you can access the value by
# using CONSTANTS['_Foo']
#------------------------------------------------------------
IE_CONST::CONSTANTS.each do |k, v|
puts "#{k} = #{v}"
end
puts WIN32OLE::VERSION
ie.quit

41
sample/win32ole/ienavi.rb Normal file
View file

@ -0,0 +1,41 @@
# frozen_string_literal: false
require 'win32ole'
$urls = []
def navigate(url)
$urls << url
end
def stop_msg_loop
puts "Now Stop IE..."
$LOOP = false;
end
def default_handler(event, *args)
case event
when "BeforeNavigate"
puts "Now Navigate #{args[0]}..."
end
end
ie = WIN32OLE.new('InternetExplorer.Application')
ie.visible = true
ie.gohome
ev = WIN32OLE_EVENT.new(ie, 'DWebBrowserEvents')
ev.on_event {|*args| default_handler(*args)}
ev.on_event("NavigateComplete") {|url| navigate(url)}
ev.on_event("Quit") {|*args| stop_msg_loop}
$LOOP = true
while ($LOOP)
WIN32OLE_EVENT.message_loop
end
puts "You Navigated the URLs ..."
$urls.each_with_index do |url, i|
puts "(#{i+1}) #{url}"
end

View file

@ -0,0 +1,41 @@
# frozen_string_literal: false
require 'win32ole'
class IEHandler
attr_reader :loop
def initialize
@urls = []
@loop = true
end
def method_missing(event, *args)
case event
when "BeforeNavigate2"
puts "Now Navigate #{args[1]}..."
end
end
def onNavigateComplete2(pdisp, url)
@urls << url
end
def onOnQuit
puts "Now Stop IE..."
@loop = false
end
def put_urls
puts "You Navigated the URLs ..."
@urls.each_with_index do |url, i|
puts "(#{i+1}) #{url}"
end
end
end
ie = WIN32OLE.new('InternetExplorer.Application')
ie.visible = true
ie.gohome
ev = WIN32OLE_EVENT.new(ie)
ev.handler = IEHandler.new
while (ev.handler.loop)
WIN32OLE_EVENT.message_loop
end
ev.handler.put_urls

View file

@ -0,0 +1,24 @@
# frozen_string_literal: false
#
# You need WSH(Windows Scripting Host) to run this script.
#
require "win32ole"
def listup(items)
# items.each do |i|
for i in items
puts i.name
end
end
fs = WIN32OLE.new("Scripting.FileSystemObject")
folder = fs.GetFolder(".")
puts "--- folder of #{folder.path} ---"
listup(folder.SubFolders)
puts "--- files of #{folder.path} ---"
listup(folder.Files)

348
sample/win32ole/olegen.rb Normal file
View file

@ -0,0 +1,348 @@
# frozen_string_literal: false
#-----------------------------
# olegen.rb
# $Revision$
#-----------------------------
require 'win32ole'
class WIN32COMGen
def initialize(typelib)
@typelib = typelib
@receiver = ""
end
attr_reader :typelib
def ole_classes(typelib)
begin
@ole = WIN32OLE.new(typelib)
[@ole.ole_obj_help]
rescue
WIN32OLE_TYPE.ole_classes(typelib)
end
end
def generate_args(method)
args = []
if method.size_opt_params >= 0
size_required_params = method.size_params - method.size_opt_params
else
size_required_params = method.size_params - 1
end
size_required_params.times do |i|
if method.params[i] && method.params[i].optional?
args.push "arg#{i}=nil"
else
args.push "arg#{i}"
end
end
if method.size_opt_params >= 0
method.size_opt_params.times do |i|
args.push "arg#{i + size_required_params}=nil"
end
else
args.push "*arg"
end
args.join(", ")
end
def generate_argtype(typedetails)
ts = ''
typedetails.each do |t|
case t
when 'CARRAY', 'VOID', 'UINT', 'RESULT', 'DECIMAL', 'I8', 'UI8'
# raise "Sorry type\"" + t + "\" not supported"
ts << "\"??? NOT SUPPORTED TYPE:`#{t}'\""
when 'USERDEFINED', 'Unknown Type 9'
ts << 'VT_DISPATCH'
break;
when 'SAFEARRAY'
ts << 'VT_ARRAY|'
when 'PTR'
ts << 'VT_BYREF|'
when 'INT'
ts << 'VT_I4'
else
if String === t
ts << 'VT_' + t
end
end
end
if ts.empty?
ts = 'VT_VARIANT'
elsif ts.end_with?(?|)
ts += 'VT_VARIANT'
end
ts
end
def generate_argtypes(method, proptypes)
types = method.params.collect{|param|
generate_argtype(param.ole_type_detail)
}.join(", ")
if proptypes
types += ", " if types.size > 0
types += generate_argtype(proptypes)
end
types
end
def generate_method_body(method, disptype, types=nil)
" ret = #{@receiver}#{disptype}(#{method.dispid}, [" +
generate_args(method).gsub("=nil", "") +
"], [" +
generate_argtypes(method, types) +
"])\n" +
" @lastargs = WIN32OLE::ARGV\n" +
" ret"
end
def generate_method_help(method, type = nil)
str = " # "
if type
str += type
else
str += method.return_type
end
str += " #{method.name}"
if method.event?
str += " EVENT"
str += " in #{method.event_interface}"
end
if method.helpstring && method.helpstring != ""
str += "\n # "
str += method.helpstring
end
args_help = generate_method_args_help(method)
if args_help
str += "\n"
str += args_help
end
str
end
def generate_method_args_help(method)
args = []
method.params.each_with_index {|param, i|
h = " # #{param.ole_type} arg#{i} --- #{param.name}"
inout = []
inout.push "IN" if param.input?
inout.push "OUT" if param.output?
h += " [#{inout.join('/')}]"
h += " ( = #{param.default})" if param.default
args.push h
}
if args.size > 0
args.join("\n")
else
nil
end
end
def generate_method(method, disptype, io = STDOUT, types = nil)
io.puts "\n"
io.puts generate_method_help(method)
if method.invoke_kind == 'PROPERTYPUT'
io.print " def #{method.name}=("
else
io.print " def #{method.name}("
end
io.print generate_args(method)
io.puts ")"
io.puts generate_method_body(method, disptype, types)
io.puts " end"
end
def generate_propputref_methods(klass, io = STDOUT)
klass.ole_methods.select {|method|
method.invoke_kind == 'PROPERTYPUTREF' && method.visible?
}.each do |method|
generate_method(method, io)
end
end
def generate_properties_with_args(klass, io = STDOUT)
klass.ole_methods.select {|method|
method.invoke_kind == 'PROPERTYGET' &&
method.visible? &&
method.size_params > 0
}.each do |method|
types = method.return_type_detail
io.puts "\n"
io.puts generate_method_help(method, types[0])
io.puts " def #{method.name}"
if klass.ole_type == "Class"
io.print " OLEProperty.new(@dispatch, #{method.dispid}, ["
else
io.print " OLEProperty.new(self, #{method.dispid}, ["
end
io.print generate_argtypes(method, nil)
io.print "], ["
io.print generate_argtypes(method, types)
io.puts "])"
io.puts " end"
end
end
def generate_propput_methods(klass, io = STDOUT)
klass.ole_methods.select {|method|
method.invoke_kind == 'PROPERTYPUT' && method.visible? &&
method.size_params == 1
}.each do |method|
ms = klass.ole_methods.select {|m|
m.invoke_kind == 'PROPERTYGET' &&
m.dispid == method.dispid
}
types = []
if ms.size == 1
types = ms[0].return_type_detail
end
generate_method(method, '_setproperty', io, types)
end
end
def generate_propget_methods(klass, io = STDOUT)
klass.ole_methods.select {|method|
method.invoke_kind == 'PROPERTYGET' && method.visible? &&
method.size_params == 0
}.each do |method|
generate_method(method, '_getproperty', io)
end
end
def generate_func_methods(klass, io = STDOUT)
klass.ole_methods.select {|method|
method.invoke_kind == "FUNC" && method.visible?
}.each do |method|
generate_method(method, '_invoke', io)
end
end
def generate_methods(klass, io = STDOUT)
generate_propget_methods(klass, io)
generate_propput_methods(klass, io)
generate_properties_with_args(klass, io)
generate_func_methods(klass, io)
# generate_propputref_methods(klass, io)
end
def generate_constants(klass, io = STDOUT)
klass.variables.select {|v|
v.visible? && v.variable_kind == 'CONSTANT'
}.each do |v|
io.print " "
io.print v.name.sub(/^./){$&.upcase}
io.print " = "
io.puts v.value
end
end
def class_name(klass)
klass_name = klass.name
if klass.ole_type == "Class" &&
klass.guid &&
klass.progid
klass_name = klass.progid.gsub(/\./, '_')
end
if /^[A-Z]/ !~ klass_name || Module.constants.include?(klass_name)
klass_name = 'OLE' + klass_name
end
klass_name
end
def define_initialize(klass)
<<STR
def initialize(obj = nil)
@clsid = "#{klass.guid}"
@progid = "#{klass.progid}"
if obj.nil?
@dispatch = WIN32OLE.new @progid
else
@dispatch = obj
end
end
STR
end
def define_include
" include WIN32OLE::VARIANT"
end
def define_instance_variables
" attr_reader :lastargs"
end
def define_method_missing
<<STR
def method_missing(cmd, *arg)
@dispatch.method_missing(cmd, *arg)
end
STR
end
def define_class(klass, io = STDOUT)
io.puts "class #{class_name(klass)} # #{klass.name}"
io.puts define_include
io.puts define_instance_variables
io.puts " attr_reader :dispatch"
io.puts " attr_reader :clsid"
io.puts " attr_reader :progid"
io.puts define_initialize(klass)
io.puts define_method_missing
end
def define_module(klass, io = STDOUT)
io.puts "module #{class_name(klass)}"
io.puts define_include
io.puts define_instance_variables
end
def generate_class(klass, io = STDOUT)
io.puts "\n# #{klass.helpstring}"
if klass.ole_type == "Class" &&
klass.guid &&
klass.progid
@receiver = "@dispatch."
define_class(klass, io)
else
@receiver = ""
define_module(klass, io)
end
generate_constants(klass, io)
generate_methods(klass, io)
io.puts "end"
end
def generate(io = STDOUT)
io.puts "require 'win32ole'"
io.puts "require 'win32ole/property'"
ole_classes(typelib).select{|klass|
klass.visible? &&
(klass.ole_type == "Class" ||
klass.ole_type == "Interface" ||
klass.ole_type == "Dispatch" ||
klass.ole_type == "Enum")
}.each do |klass|
generate_class(klass, io)
end
begin
@ole.quit if @ole
rescue
end
end
end
require 'win32ole'
if __FILE__ == $0
if ARGV.size == 0
$stderr.puts "usage: #{$0} Type Library [...]"
exit 1
end
ARGV.each do |typelib|
comgen = WIN32COMGen.new(typelib)
comgen.generate
end
end

7307
sample/win32ole/xml.rb Normal file

File diff suppressed because it is too large Load diff