Skip to content

Instantly share code, notes, and snippets.

@vmi
Created December 13, 2011 06:56
Show Gist options
  • Save vmi/1470977 to your computer and use it in GitHub Desktop.
Save vmi/1470977 to your computer and use it in GitHub Desktop.
[snippet] WIN32OLE Excel (ruby)
#!/usr/bin/ruby
# -*- coding: utf-8 -*-
require 'win32ole'
WIN32OLE.codepage = WIN32OLE::CP_UTF8
$fso = WIN32OLE.new('Scripting.FileSystemObject')
$xl = WIN32OLE.new('Excel.Application')
at_exit {
$xl.Quit
}
def get_absolute_path filename
return $fso.GetAbsolutePathName(filename)
end
def dump_excel filename
path = get_absolute_path(filename)
book = $xl.Workbooks.Open(path, { 'ReadOnly' => true })
begin
count = book.Worksheets.Count
puts "#{filename},#{count}"
$stdout.flush
ensure
book.Saved = true
book.Close
end
end
ARGV.each do |filename|
dump_excel(filename)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment