2017-10-10 4 views
0

Railsアプリケーションからスプレッドシートを出力するのにRoo gemを使用しています。私の列の1つはハッシュです(Postgres DB)。私は、より読みやすいものにセルの内容をフォーマットしたいと思います。私は人間が読めるセルを返すメソッドを使用しています。Rails Roo gem .xlsx出力にはメソッドの出力ではないオブジェクトが含まれています

列のデータは次のようになります。

Inspection.first.print_results 

Soiled:Oil on back 
Assigned To:Warehouse 
Contaminated:Blood on left cuff 
Inspection Date:01/01/2017 
Physical Damage Seam Integrity: 
Physical Damage Thermal Damage: 
Physical Damage Reflective Trim: 
Physical Damage Rips Tears Cuts:Small tear on right sleeve 
Correct Assembly Size Compatibility Of Shell Liner And Drd: 
Physical Damage Damaged Or Missing Hardware Or Closure Systems: 
=> {"soiled"=>"oil on back", 
"assigned_to"=>"Warehouse", 
"contaminated"=>"blood on left cuff", 
"inspection_date"=>"01/01/2017", 
"physical_damage_seam_integrity"=>"", 
"physical_damage_thermal_damage"=>"", 
"physical_damage_reflective_trim"=>"", 
"physical_damage_rips_tears_cuts"=>"small tear on right sleeve", 
"correct_assembly_size_compatibility_of_shell_liner_and_drd"=>"", 
"physical_damage_damaged_or_missing_hardware_or_closure_systems"=>""} 

:だからコンソールで、私はこれを取得

def print_results 
    self.results.each do |k,v| 
    puts "#{k.titleize}:#{v.humanize}\r\n" 
    end 
end 

:私は次のメソッドを定義し、私の検査モデルで

Inspection.first.results 
=> {"soiled"=>"oil on back", 
"assigned_to"=>"[email protected]", 
"contaminated"=>"blood on left cuff", 
"inspection_date"=>"01/01/2017", 
"physical_damage_seam_integrity"=>"", 
"physical_damage_thermal_damage"=>"", 
"physical_damage_reflective_trim"=>"", 
"physical_damage_rips_tears_cuts"=>"small tear on right sleeve", 
"correct_assembly_size_compatibility_of_shell_liner_and_drd"=>"", 
"physical_damage_damaged_or_missing_hardware_or_closure_systems"=>""} 

しかし、私はこれをindex.xlsx.axlsxファイルに入れます。

wb = xlsx_package.workbook 
wb.add_worksheet(name: "Inspections") do |sheet| 
    sheet.add_row ['Serial Number', 'Category', 'Inspection Type', 'Date', 
    'Pass/Fail', 'Assigned To', 'Inspected By', 'Inspection Details'] 
    @inspections.each do |inspection| 
    sheet.add_row [inspection.ppe.serial, inspection.ppe.category, 
    inspection.advanced? ? 'Advanced' : 'Routine', 
    inspection.results['inspection_date'], 
    inspection.passed? ? 'Pass' : 'Fail', 
    inspection.ppe.user.last_first_name, 
    inspection.user.last_first_name, 
    inspection.print_results] 
    end 
end 

スプレッドシートの出力は、printステートメントの結果ではなく元のハッシュです。

{"soiled"=>"oil on back", 
"assigned_to"=>"Warehouse", 
"contaminated"=>"blood on left cuff", "inspection_date"=>"01/01/2017", 
"physical_damage_seam_integrity"=>"", 
"physical_damage_thermal_damage"=>"", 
"physical_damage_reflective_trim"=>"", 
"physical_damage_rips_tears_cuts"=>"small tear on right sleeve", 
"correct_assembly_size_compatibility_of_shell_liner_and_drd"=>"", 
"physical_damage_damaged_or_missing_hardware_or_closure_systems"=>""} 

メソッドの出力をハッシュオブジェクトではなくセルに入れることはできますか?

答えて

1

問題は、print_resultsメソッドが標準出力(つまりコンソール)を出力するが、依然として元のハッシュを返すという点です。このメソッドの戻り値は、Rooにとって重要なものです。

def print_results 
    self.results.map do |k,v| 
    "#{k.titleize}:#{v.humanize}\r\n" 
    end.join 
end 

これは、文字列(.mapで返される文字列の配列を組み合わせること.joinの使用に注意してください)を返しますあなたが投げることができます:あなたが何をしたいか

は書き換えprint_resultsがフォーマットされた文字列を返すことですあなたの望む出力を得ることができます。

+0

パーフェクト。 upvotedと受け入れられます。 'puts'の戻り値はコンソールに出力されないことを忘れています。行の折り返しは機能しませんが、折り返しの行を行に適用する方法を理解する必要があるためです。 – Beartech

関連する問題