id,
game.id])
loss_count = Play.count(:conditions=>[
"player_id = ? AND
game_id= ? AND
won=false",
player.id,
game.id])
worksheet.write(current_row, 0, [game.name, win_count, loss_count])
current_row=current_row+1
end
end
workbook.close
Save this code as spreadsheet_team_performance.rb. You can run it by issuing the
following command:
ruby spreadsheet_team_perfomance.rb
The script creates a file called spreadsheet_report.xls. Open the file with Microsoft
Excel or OpenOffice.org, as shown in Figure 4-1.
CHAPTER 4 n CREATING REPORTS ON THE DESKTOP 55
Figure 4-1. Player win/loss spreadsheet in OpenOffice.org
nTip You can use the spreadsheet/excel library to create spreadsheets dynamically in a Rails application,
and then send them to the user. This lets you have a ???Download this as Excel??? link in your views, for
example. You can see a scheme for quickly sending binary files in Rails at http://wiki.rubyonrails.
org/rails/pages/HowtoSendFilesFast.
Dissecting the Code
The first few lines of Listing 4-1 create a connection to your database and set up your
models, similar to the code in the previous examples.
Pages:
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98