0

I have the following code:

$excel = WIN32OLE.new('Excel.Application')
$excel.visible = true

puts 'Filepath?'
$filepath = $stdin.gets.chomp
$workbook = $excel.Workbooks.Open($filepath)
puts $workbook

puts 'Sheet?' 
$worksheet = $workbook.Worksheets
$count = $worksheet.Count
for i in 1..$count do 
    $array = $workbook.Worksheets(i).Name
    puts i.to_s + "." + " " + $array
end

It's a part of a Ruby script that I'm using to manage and edit Excel files. I went with 'win32ole' because the files I'm interacting with are fairly large and I find that it's much faster than something like 'roo'. So my code does what I need it to do and that's go out and get all the sheet names within a specified spreadsheet. However for some reason it adds these values before and after my results:

#<WIN32OLE:0x291a878>
Sheet?"
1. Sheet1
2. Sheet2
3. Sheet3
1..3

I have no idea where #<WIN32OLE:0x291a878> or 1..3 are coming from but any help to trim these out would be greatly appreciated. Thank you

1
  • 2
    The first line comes from puts $workbook. As for the 1..3, nothing in the code you provided prints that. Commented Oct 8, 2015 at 20:38

1 Answer 1

1

The first line #<WIN32OLE:0x291a878> is the output of puts $workbook. It is the inspection string of a WIN32OLE instance.

You probably ran this in a REPL, and 1..3 is the return value of the last expression for i in 1..$count do ... end, which is printed by the REPL.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.