rubyでエクセル

時間もあったしjavaは飽きたからrubyでエクセルからエラーコードの情報を抜いてみた。
書き捨てのスクリプトだからわりと適当。

やっぱ高階関数いるよ。いるって。javaにも入れろよ。
高階関数には無名関数が必要だよ。

# -*- coding: utf-8 -*-
require "win32ole"
require "kconv"

def writeExcel path
  excel = WIN32OLE.new('EXCEL.Application')
  excel.visible = false
  excel.displayAlerts = false
  wb = excel.workbooks.open path
  sheet = wb.worksheets.item(1)
  begin
    yield sheet
  ensure
    wb.save
    wb.close
    excel.quit
  end
end

writeExcel "C:/Temp/xxx/xxx.xls" do |os|
  whidx = 1
  excel = WIN32OLE.new('EXCEL.Application')
  excel.visible = false
  excel.displayAlerts = false
  dir=Dir.glob "C:/xxx/xxx/**/*xls"
  dir.map do |e|
    wb=excel.workbooks.open e
    begin
      file_name = File.basename wb.name , ".xls"
      1.upto wb.sheets.count do |idx|
        sheet = wb.sheets.item(idx)
        next unless sheet.name.toutf8 == "xxxx"
        
        1.upto 100 do |hidx|
          cell=sheet.cells.item(hidx,1)
          no=cell.value
          next unless no
          next unless no.to_s=~/^[0-9.]+$/
          puts no.to_s
          cell=sheet.cells.item(hidx,3)
          title=cell.value
          cell=sheet.cells.item(hidx,18)
          check=cell.value
          cell=sheet.cells.item(hidx,48)
          error_code=cell.value
          cell=sheet.cells.item(hidx,52)
          msg_code=cell.value
          break unless title

          puts "file:#{file_name},title:#{title},check:#{check},errorcode:#{error_code},msgcode:#{msg_code}"
          cell=os.cells.item(whidx,1)
          cell.value=file_name
          cell=os.cells.item(whidx,2)
          cell.value=title
          cell=os.cells.item(whidx,3)
          cell.value=check
          cell=os.cells.item(whidx,4)
          cell.value=error_code
          cell=os.cells.item(whidx,5)
          cell.value=msg_code
          whidx = whidx+1
        end
      end
    ensure
      wb.close
      excel.quit
    end
  end
end