ctpfaff
ctpfaff

Reputation: 59

Rails write a csv file column wise

I like to export a dataset from my rails application as a csv file using the builtin csv library of rails. Usually a csv file is written row wise like in my example below which comes from my datasets_controller.rb:

require 'csv'

dataset = Dataset.find(6)
dataset_headers = dataset.datacolumns.collect { |dc| dc.columnheader }

csv_file = CSV.generate do  |csv|                                                                                                           
   csv << dataset_headers
end

And now my question is if I could also write my csv files column wise like this?

require 'csv'    

dataset_columns = Datacolumn.all(:conditions => ["dataset_id = ?", 6], :order => "columnnr ASC").uniq 

csv_file = CSV.generate do  |csv|                                                                                                           
   csv << "here put one after another all my data columns"
end

EDIT:

Based on Douglas suggestion I came up with the colde below.

 data_columns=Datacolumn.all(:conditions => ["dataset_id = ?", dataset.id], :order => "columnnr ASC").uniq

    CSV.generate do |csv|
        value=Array.new 
        data_columns.each do |dc|
          value << dc.columnheader
          dc.sheetcells.each do |sc|
            if sc.datatype && sc.datatype.is_category? && sc.category 
              value << sc.category.short
            elsif sc.datatype && sc.datatype.name.match(/^date/) && sc.accepted_value
              value << sc.accepted_value.to_date.to_s
            elsif sc.accepted_value
              value << sc.accepted_value
            else
              value << sc.import_value
            end 
          end 
          csv << value
          value = Array.new 
        end 
      end 

The output is not transposed for this case and looks like this:

height,10,2,<1,na
fullauthor,Fortune,(Siebold & Zucc.) Kuntze,Fortune,(Siebold & Zucc.) Kuntze
Year,1850,1891,1850,1891
fullname,Mahonia bealei,Toxicodendron sylvestre,Mahonia bealei,Toxicodendron sylvestre

But when I change the line which writes the csv to

csv << value.transpose

I get an error which tells me that it could not convert a string to array to do that. Anybody an Idea how to fix this?

Any help with this would be appreciated.

Best Claas

Upvotes: 0

Views: 2634

Answers (1)

Douglas F Shearer
Douglas F Shearer

Reputation: 26488

You could use Array#transpose, which will flip your rows to columns. A simple example:

> a = [['name', 'charles', 'dave'],['age', 24, 36],['height', 165, 193]]
=> [["name", "charles", "dave"], ["age", 24, 36], ["height", 165, 193]]
> a.transpose
=> [["name", "age", "height"], ["charles", 24, 165], ["dave", 36, 193]]

Thus, assuming dataset_columns is an array:

require 'csv'    

dataset_columns = Datacolumn.all(:conditions => ["dataset_id = ?", 6], :order => "columnnr ASC").uniq 

csv_file = CSV.generate do  |csv|                                                                                                           
   csv << dataset_columns.transpose
end

Upvotes: 5

Related Questions