How to factor the codes

Hi all,
I have 3 2D-arrays(@data, @mean,@sd). I need to import all of them into
Excel in the same sheet and each array is separated by one empty row.
Here are my codes:

def import_to_excel

row_start1=1
[email protected]
column_start1=1
column_end1=@data[1].size
worksheet1.Range(worksheet1.Cells(
row_start1,column_start1),worksheet1.Cells(row_end1,column_end1)).Value=@data

#write mean to excel
row_start2= row_end1+2
[email protected]
column_start2=1
column_end2=@mean[1].size

worksheet1.Range(worksheet1.Cells(row_start2,
column_start2),worksheet1.Cells(row_end2,column_end2)).Value=@mean
#write
sd to excel
row_start3= row_end2+2
[email protected]
column_start3=1
column_end3=@sd[1].size

worksheet1.Range(worksheet1.Cells( row_start3,
column_start3),worksheet1.Cells( row_end3,column_end3)).Value=@sd

end

So far these codes work fine for me. But what if I have 10 2D-array
data, Am i going to write ten similar code chunks? Is it possible to
factor my codes?

Thanks,

Li

Li Chen wrote:

column_end1=@data[1].size
column_start2),worksheet1.Cells(row_end2,column_end2)).Value=@mean

Li

Hey Li

I think this’ll work, I haven’t tested it though

def import_to_excel
…populate @data, @mean and @sd

row_ended = actual_import(@data, worksheet1)
row_ended = actual_import(@mean, worksheet1, row_ended)
actual_import(@sd, worksheet1, row_ended)

…further code
end

def actual_import(dataset, sheet, previous_row_ended = -1)
row_start = previous_row_ended + 2
row_end = row_start + dataset.size - 1
column_end = ‘A’
(dataset.first.size - 1).times {column_end.succ!}
sheet.Range(“A#{row_start}:#{column_end}#{row_end}”).Value = dataset
return row_end
end

I found a beautiful page in the rubygarden that is filled to the brim
with great excel scripts! Much thanks and respect to the author! There
are links to one or two other resources to other people’s articles, but
I couldn’t track down the name of the author him- or herself.

http://wiki.rubygarden.org/Ruby/page/show/ScriptingExcel

Hope this help,
Cheery-o
Paul, Gustav