Win32OLE

最近やりはじめた。
ちょっと作ってみた。Excelで1人チャットの履歴をためる。

#! usr/local/bin/ruby -Ku
require 'win32ole'
require 'kconv'

class XlsChat
  @@instance = nil
  
  #Singltonもっと綺麗にできるだろう
  def initialize 
    unless @@instance
      @@instance = "ON"
      @@instance = self.class.new 
    end
    return @@instance
  end
  
  def change_point(now_point,comand)
    result = 
      case comand
        when "次へ"
          if now_point == "F"
            "A"
          else
            (now_point[0] + 1).chr
          end
        else
          unless now_point == "A"
            (now_point[0] - 1).chr
          else 
            now_point
          end
      end
    return result
  end 

  def init
    explanation = <<-EXP
      Hallo! Guys.
      This is Excel Chat!
      If you want to use this, please type [Enter].
    EXP
    puts explanation
    gets
  end

  def usage
    usage = <<-USAGE
      Please type anywords!
      If you want to chat in Japanese. type [Alt + 半角/全角].
      If you type [次へ OR 戻る], you can change rows.
      If you want to quit, please type only [Enter] without words.

    USAGE
    puts usage.tosjis
  end

  def start_xls
    excel = WIN32OLE.new("Excel.Application")
    excel.Visible = true

    wbook = excel.WorkBooks.Add();
    wsheet = excel.WorkSheets(1);
    wsheet.Range('A1').value = "詳しくはコマンドプロンプトの説明へ".tosjis
    return wsheet
  end

  def xls_chat(wsheet)
    i = 3
    begin
      print ">"
      text = (gets || "").chomp.toutf8
      point = (now_point ||= "A") + i.to_s
      now_point = change_point(now_point,text) if (text == "次へ"|| text == "戻る")
      wsheet.Range(point).value = text.tosjis;
      i+=1
    end while text != ""
    wsheet.Range(point).value = "終わりー丶(´▽`)ノ".tosjis
  end

  def start
    begin
      init
      usage
      wsheet = start_xls
      xls_chat(wsheet)
    rescue => e
      puts "その動作はだめぽ(>_<)".tosjis
      exit
    end
  end
end
#-------------------------------

xls_chat = XlsChat.new
xls_chat.start


http://jp.rubyist.net/magazine/?0003-Win32OLE