Upgrade to Pro — share decks privately, control downloads, hide ads and more …

HelloRubyWin32OLE.pdf

F2f9d54e8fafad3fce62bbf072cdab1e?s=47 Kazunori Otani
July 16, 2013
34

 HelloRubyWin32OLE.pdf

F2f9d54e8fafad3fce62bbf072cdab1e?s=128

Kazunori Otani

July 16, 2013
Tweet

Transcript

  1. だいたいの流れ • Rubyのインストール – http://rubyinstaller.org/downloads/ – Ruby 2.0.0-p247 を保存して実行 •

    Windowsオブジェクトの操作の仕方 – http://magazine.rubyist.net/?0003-Win32OLE – http://magazine.rubyist.net/?0004-Win32OLE
  2. None
  3. ↓のコマンドを打って、 何が起こるか見てみよう C:¥Users¥k-ohtani>irb DL is deprecated, please use Fiddle irb(main):001:0>

    require 'win32ole' => true irb(main):002:0> ie = WIN32OLE.new('InternetExplorer.Application') => #<WIN32OLE:0x61ecd8> irb(main):004:0> ie.Visible = true => true irb(main):003:0> ie.Navigate("http://www.google.co.jp/") => nil irb(main):005:0> ie.document.all.Item("q").Value = "voyage group" => "voyage group" irb(main):006:0> ie.document.all.Item("btnG").click() => nil irb(main):007:0>
  4. ファイルに書いて、 実行してみよう require 'win32ole' ie = WIN32OLE.new('InternetExplorer.Application') ie.Visible = true

    ie.Navigate("http://www.google.co.jp/") while ie.busy sleep 1 end ie.document.all.Item("q").Value = "voyage group" sleep 1 ie.document.all.Item("btnG").click() C:¥Users¥k-ohtani>ruby Desktop¥hello_ie.rb
  5. Excelファイルを扱う: ファイルに書いて、実行してみよう require 'win32ole' def getAbsolutePath filename fso = WIN32OLE.new('Scripting.FileSystemObject')

    return fso.GetAbsolutePathName(filename) end filename = getAbsolutePath(ARGV[0]) xl = WIN32OLE.new('Excel.Application') book = xl.Workbooks.Open(filename) begin book.Worksheets.each do |sheet| sheet.UsedRange.Rows.each do |row| record = [] row.Columns.each do |cell| record << cell.Value end puts record.join(",") end end ensure book.Close xl.Quit end C:¥Users¥k-ohtani>Desktop C:¥Users¥k-ohtani¥Desktop>ruby to_csv.rb Book1.xlsx C:¥Users¥k-ohtani¥Desktop>ruby to_csv.rb Book1.xlsx > Book1.csv