Collect field names in spreadsheet

Ruby code posted by Alex
created at 11 Aug 10:03

Edit | Back
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
require 'win32ole'

DATAHOME = 'D:\My Documents\Projects\ruby\pca\08_shipping_rec'

xl = WIN32OLE.new('excel.application')
all_fields = []

Dir.foreach(DATAHOME) do |f|
   fn_xls = File.join(DATAHOME, f)
   next if not File.file? fn_xls
   wb = xl.workbooks.open(fn_xls, false, true)
   wb.worksheets.each do |ws|
      next if ws.name !~ /^\d+\./
      all_fields |= ws.usedrange.value[1]
   end
   wb.close(false)
end
all_fields.compact!
puts all_fields.inspect
puts all_fields.size

puts 'cleaning...'

# the open excel process has to be closed this way due to a bug in the ole lib
xl.quit
xl = nil
GC.start
638 Bytes in 9 ms with coderay