0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

IronRubyで起動中のExcelのシートを編集するGUIつきマクロを作る

Posted at

概要

IronRubyで起動中のExcelのシートを編集するマクロを作ります。
GUI画面を持ち、パラメータを指定することができます。

前提

RubyとExcelのマクロについてある程度知っている方向けです。
本記事のテーマはRubyやExcelマクロの文法やコーディング作法ではなく、Ruby(IronRuby)とExcelを連携させるときのテクニックです。

環境

Windows 8.1(64ビット)
IronRuby 1.1
Excel 2010

開始ボタンのみの例

最初は最もシンプルな例として、開始ボタンのみの画面のあるコードを紹介します。

コード

excel_set_random_with_gui_01.rb
# coding: utf-8

require 'PresentationFramework'

include System::Windows
include System::Windows::Controls

# 起動中のExcelのプロセスにアタッチする
begin
	excel = System::Runtime::InteropServices::Marshal::GetActiveObject("Excel.Application")
rescue
	abort "No Excel process."
end


window = Window.new
window.title = "IronRuby"
window.AllowDrop = true
window.Width="300"
window.Height="300"

button = Button.new
button.MinWidth = 150
button.Content = "乱数書き込み"
window.Content = button

button.Click do |sender, e|
	# カーソルが当たっているセルに1~100の乱数を書き込む
	excel.ActiveCell.Value = rand(100) + 1
end

app = Application.new
app.run(window)

実行方法

[1] Excelを起動します

[2] DOS窓を開き、excel_set_random_with_gui_01.rbを実行します

> "C:\Program Files (x86)\IronRuby 1.1\bin\ir.exe" excel_set_random_with_gui_01.rb

[3] 実行すると"乱数書き込み"というボタンのあるウィンドウが表示されます

[4] ボタンを押すとA1のセルに1~100の乱数が入力されます(カーソルがA1のセルにあるため)
excel_set_random_with_gui_01.PNG

[5] ボタンを連打するとA1のセルの値が次々と変化するはずです

パラメータを指定できる例

次に、ボタンに加えてテキストボックスも配置して、パラメータを指定できるコードを紹介します。

コード

excel_set_random_with_gui_02.rb
# coding: utf-8

require 'PresentationFramework'

include System::Windows
include System::Windows::Controls

# 起動中のExcelのプロセスにアタッチする
begin
	excel = System::Runtime::InteropServices::Marshal::GetActiveObject("Excel.Application")
rescue
	abort "No Excel process."
end


window = Window.new
window.title = "IronRuby"
window.Width="200"
window.Height="100"

label = TextBlock.new
label.Text = "↓接頭辞を入力"

textbox = TextBox.new

button = Button.new
button.Content = "乱数書き込み"
button.Click do |sender, e|
	# カーソルが当たっているセルに1~100の乱数を書き込む
	excel.ActiveCell.Value = "#{textbox.Text}#{rand(100) + 1}"
end

panel = StackPanel.new
panel.Children.Add(label)
panel.Children.Add(textbox)
panel.Children.Add(button)

window.Content = panel


app = Application.new
app.run(window)

実行方法

[1] Excelを起動します

[2] DOS窓を開き、excel_set_random_with_gui_02.rbを実行します

> "C:\Program Files (x86)\IronRuby 1.1\bin\ir.exe" excel_set_random_with_gui_02.rb

[3] 実行するとテキストボックスとボタンが1つずつあるウィンドウが表示されます

[4] テキストボックスに適当な文字を入力します

[5] ボタンを押すとA1のセルに[4]で入力した文字と乱数が入力されます(カーソルがA1のセルにあるため)
exec_result_02.PNG

[6] ボタンを連打するとA1のセルの値が次々と変化するはずです

何がうれしいの?

Excelのマクロ実行環境のGUIエディタを使わずに、GUIつきマクロを作ることができます。
WPFで作った画面はリサイズが可能であるなど柔軟性があります。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?