Okay so I’ve been messing around with Ruby quite a bit but am still fairly new at it. I am running into a problem with my current program. What I want it to do is take a CSV file which contains a list of terms, I then take the csv file and create an array of those terms. Once I have those terms in an array I want to search an Excel workbook and find all occurances of that term…in all sheets. If a term is found it will be appended to a csv file with the sheet name and cell location it is found at. I think I have a fairly good start at it.
EDIT: I am no longer recieving any errors. It just simply recreates the given file. It does not seem to be dropping into the workbook and doing the compares? Any ideas?
EDIT 2: Used print statements it is something wrong with my if strings are equals condition.
EDIT 3: I isololated the issue to my compare. When I use the term variable it gets the term as ["theterm"] and then trys to compare it to a string which will always be false…
My code is below:
require 'roo'
$termsarray = Array.new
puts "Please enter filepath of file of terms (C:/path/to/file.csv)"
#termspath = gets.chomp.to_s
termspath = "C:/Ruby/termslist.csv"
CSV.foreach( "#{termspath}") do |row|
$termsarray << row
end
puts "Added terms to array successfully"
puts "Please enter filepath of file to cross-reference (C:/path/to/file.xlsx)"
#filepath = gets.chomp.to_s
filepath = "C:/Ruby/file_to_crossreference.xlsx"
puts "Please enter filepath for output file (C:/path/to/file.csv)"
output = gets.chomp.to_s
w1 = Excelx.new ( "#{filepath}" )
CSV.open( "#{output}", "w") do |csv|
$termsarray.each do |term|
csv << term
w1.sheets.each do |sheet|
w1.default_sheet = sheet
if w1.first_row && w1.first_column
eval(w1.to_s).each do |index, value|
if term.to_s.chomp.eql? value.to_s.chomp
csv << [sheet, convert_index(index.to_s), value]
end
end
end
end
end
print "File Indexed Successfully!"
end
I fixed and found my problem. When creating the list of terms I was accidently creating an array of arrays. The code is below however it may not be the best, especially if the data is in multiple columns and such.