ruby-LEDの日記

2006-10-14

win32oleを使ってみた

#windowsパスの取得
def getAbsolutePath filename
	fso = WIN32OLE.new('Scripting.FileSystemObject')
	fso.GetAbsolutePathName(filename)
end

#テンプレートに関数名を記入
def makeFunctionBook funcname
	filename = getAbsolutePath($template)

	excel = WIN32OLE.new('Excel.Application')
	excel.visible = TRUE
	book = excel.Workbooks.Open(filename)
	sheet = book.Worksheets(1)
	sheet.Name = funcname
	sheet.Range('C3').value = funcname

	filename = funcname + '.xls'
	book.SaveAs(funcname)
	book.Close
	excel.Quit
end

#関数名リストの受け取り
while gets.to_s.chomp!
	if $_.length > 0
		puts $_ + " start"
		makeFunctionBook($_)
	end
end

改造

require 'win32ole'
require 'csv'
$template='template1.xlt'

#windowsパスの取得
def getAbsolutePath filename
	fso = WIN32OLE.new('Scripting.FileSystemObject')
	fso.GetAbsolutePathName(filename)
end

#テンプレートに関数名を記入
def makeFunctionBook funcinfo
	filename = getAbsolutePath($template)

	excel = WIN32OLE.new('Excel.Application')
	excel.visible = TRUE
	excel.displayAlerts = FALSE #同名のファイルがあったら強制的に上書きする
	book = excel.Workbooks.Open(filename)
	sheet = book.Worksheets(1)
	sheet.Name = funcinfo[0]
	sheet.Range('C3').value = funcinfo[0]
	sheet.Range('C15').value = funcinfo[1]

	filename = funcinfo[0] + '.xls'
	book.SaveAs(funcinfo[0])
	book.Close
	excel.Quit
end

CSV.foreach(ARGV[0]){|row|
	puts row[0]
	makeFunctionBook(row)
}

ゲスト



トラックバック - http://rubyist.g.hatena.ne.jp/ruby-LED/20061014