elements.each{|e| # Loop fo each
p 'Name - ' + e.name #Prinitng the Name of the Fields
p 'Value - ' + e.value # Printing the Value of the Fields
}
I don't know what to say about the other stuff, but let's look at just
this part above.
I think elements is an array of hashes, right? So it could be something
like this: elements = [{:name => 'John', :value => 35}, {:name =>
'Henry', :value
=> 45}, {:name => 'Sue', :value => 23}]
I used File.open - this method is like File.new where a new file is
created. Except I have associated a block with File.open. The block is
involked with File as parameter. This method will close your file when
the block is done.
See what file you get when you do this:
irb(main):006:0> File.open('test.txt', 'w') do |file|
irb(main):007:1* file << "Hello"
irb(main):008:1> file << " How are you?"
irb(main):009:1> file << "\n"
irb(main):010:1> file << "I am fine"
irb(main):011:1> end
You should get a txt file that has this in it:
Hello How are you?
I am fine
All I am doing is appending strings to the file.
So with your example, you can iterate on your array elements using the
'Array.each' command, where you grab the name and value on each element
of the array using a block - which is done by grabbing the value of each
hash key.
I iterating the Loop with different User id, so the Name and value
differs...now i have to export it to the Excel.
1. Let me know where to Start the Excel Coding, opening the Excel, how
to load the contents to Excel,how to name with Header (Name,whet we are
getting)
Then you make sure your file has '.csv' behind it. Excel can read csv
files. there is no Excel coding. All you do is create the file
2. Put the Label names or how to map the Fields name in the Excel
I forced the name and value on the first row in the spreadsheet file by
doing
row << "Name, Value\n"
If those are not changing, that should work.
Are you shooting for a table like this:
Name Value
John 35
Henry 45
Sue 23
in Excel?
Sorry to bother you a lot, awaiting your feedback
------------------------
Jason said:
Well, actually you may not need the csv library yet. But working with
Excel means you may want to take a look at it.
Anyway, maybe you could do something like this:
elements = [{:name => 'John', :value => 35}, {:name => 'Henry', :value
=> 45}, {:name => 'Sue', :value => 23}]
File.open("out.csv", "w") do |row|
row << "Name, Value\n"
elements.each do |e|
row << "#{e[:name]}, #{e[:value]}\n"
end
end
that should return a csv file in your directory that looks like this
when opened in a spreadsheet:
Name Value
John 35
Henry 45
Sue 23
I am a newbie too, so someone else might have a way better plan. PS -
I'm assuming your 'elements' is an array of hashes.